Tx · LaTeX & TikZ

How to Fix a Geometry Package Keyval Error in LaTeX

Published By the 9to5Science editorial team

At a glance

  1. 1. Read the error key

    Find the key named in the log, such as margin or margn; the package may not recognize it.

  2. 2. Check the option syntax

    Use comma-separated key=value options, spell keys correctly, and put lengths in braces when needed.

  3. 3. Test a minimal file

    Compile a small document with only geometry and its options to isolate the problem.

  4. 4. Check package loading

    Load geometry once, and distinguish an unknown-key error from an option-clash error.

Advertisement

Title: How to Fix a Geometry Package Keyval Error in LaTeX Category: latex

A keyval error from the geometry package usually points to an option that geometry cannot interpret. The log often names the offending key, which makes the quickest fix a spelling or syntax correction. The exact cause depends on the full error line, so first distinguish an unknown key from other package-loading errors.

What the error means

The geometry package uses key-value options to set page dimensions and margins. An option such as left=25mm has a key (left) and a value (25mm). If the key is misspelled, unsupported, or written in a form the package cannot parse, LaTeX may stop with a message like:

! Package keyval Error: margn undefined.

Here, margn is likely a typo for margin. The wording varies by LaTeX version and by the option that triggered the error. Read the complete log line, not just the phrase “keyval error.”

Check the option named in the log

Look for the key reported as undefined, then check it against your geometry settings. Common corrections include:

Incorrect settingLikely correction
margn=1inmargin=1in
leftmargin=1inleft=1in
top: 1intop=1in
left=1in right=1inleft=1in,right=1in

Geometry options are generally separated by commas, and a key-value option uses =. Do not assume that a descriptive English phrase is a valid option name: use the package’s documented key, such as left, right, top, bottom, or margin.

A minimal working example

This complete document sets all four margins to one inch:

\documentclass{article}
\usepackage[margin=1in]{geometry}

\begin{document}
This page has one-inch margins.
\end{document}

The package can also be loaded without options and configured later in the preamble:

\documentclass{article}
\usepackage{geometry}
\geometry{left=1in,right=1in,top=1in,bottom=1in}

\begin{document}
The margins are set by the geometry command.
\end{document}

If one form compiles but your full document does not, compare the working example with your document’s geometry settings. Add your other options back in small groups until the error returns. The most recently added setting is a useful place to look.

Check where and how geometry is loaded

Load geometry in the preamble, before \begin{document}. Normally, load it only once. You can set options in the package load, with a \geometry{...} command, or use both; a later \geometry{...} call can update settings established at load time. Avoid loading the package in one place with options and then loading it again elsewhere with a different option list.

An error saying Option clash for package geometry is not the same as an undefined-key error. It means the package was already loaded, and a later package load requested options that were not supplied or otherwise available on the first load. Put the desired options in the first \usepackage command, or use \geometry{...} to set dimensions after the package has been loaded.

If you use a document class or template that loads geometry for you, do not add a second package load without checking the template’s instructions. You may be able to configure the existing setup with \geometry{...} instead.

Isolate the problem

If the key’s spelling looks correct, create a temporary file containing only the document class, geometry load, settings, and a short line of text. Compile that file. If it works, another package, class, or template setting is involved; add the rest of the preamble back a few lines at a time. If the minimal file fails, test the options individually and consult the geometry manual for the supported names and values.

Also check for an accidental extra comma, missing =, or unmatched brace. A malformed value can produce a confusing error because the parser may treat the next piece of text as another key. For example, margin 1in is not the same syntax as margin=1in.

Common mistakes

  • Fixing a different error: Option clash points to repeated or inconsistent package loading, not necessarily an invalid geometry key.
  • Using an intuitive but nonexistent key: Use the exact option name from geometry’s documentation; for example, use left, not leftmargin.
  • Putting settings in the wrong place: Package options and \geometry{...} belong in the preamble for ordinary page-layout setup.
  • Changing many settings at once: Test one change at a time so you can identify which option causes the failure.
  • Ignoring a class or template: It may already load geometry or set page dimensions, so inspect its preamble before adding another configuration.

References

  1. The geometry package: Page Geometry in LaTeX

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.