Tx · LaTeX & TikZ

Why Does LaTeX Say “Macro Parameter # in Vertical Mode”?

Published By the 9to5Science editorial team

At a glance

  1. 1. Find the hash

    Go to the line named in the error log and look for an unescaped # character.

  2. 2. Escape literal hashes

    Write \# when you want a visible hash mark in ordinary text.

  3. 3. Check nested definitions

    Use doubled hashes, such as ##1, when defining a parameterized command inside another command.

  4. 4. Compile a small test

    Correct the source around the reported line, then compile again to see whether another error remains.

Advertisement

The message You can't use 'macro parameter character #' in vertical mode usually points to a # that TeX found where it was not allowed. The fix depends on what you meant: escape a literal hash as \#, or use the correct parameter notation inside a macro definition.

The words “in vertical mode” describe TeX’s state when it found the character; they are usually not the underlying problem. The reported line may also be where TeX finally noticed the problem, rather than where it began, so inspect nearby macro definitions and included files too.

Why # is special

In a macro definition, #1, #2, and similar forms mark the macro’s arguments. For example, #1 refers to the first argument of the command being defined. Outside a macro definition, an unescaped # is not ordinary printable text, so TeX reports an error.

If you want a hash mark in prose, use \#. For example, write Issue \#12 to print “Issue #12.” In verbatim text, such as a verbatim environment, characters are usually printed literally, so # does not need escaping there.

Minimal working example

This document prints a literal hash mark and compiles with LaTeX:

\documentclass{article}

\begin{document}
Issue \#12 is about macro parameters.
\end{document}

The backslash changes the meaning of #: \# is the command that prints the symbol, rather than a marker for a macro argument.

Use #1 only in a definition

Here is a macro that takes one argument and prints it:

\documentclass{article}
\newcommand{\say}[1]{You entered: #1}

\begin{document}
\say{hello}
\end{document}

The #1 appears inside the definition of \say, where it means “the first argument.” When you call the macro, provide an argument in braces, as in \say{hello}. Do not write #1 directly in ordinary document text; write \#1 if you intend to show those two characters.

When a macro definition is nested

A common source of this error is defining a command inside the body of another command. The outer definition must protect the hash intended for the inner definition by doubling it:

\documentclass{article}
\newcommand{\maker}{%
  \newcommand{\inner}[1]{The value is ##1.}%
}

\begin{document}
\maker
\inner{42}
\end{document}

When \maker is defined, ##1 tells TeX to preserve a parameter marker for the inner command. When \maker runs, the inner definition receives #1 as its argument marker. If you used only #1 in the outer definition, TeX would interpret it while defining \maker; since \maker has no corresponding parameter, this would produce an illegal parameter-number error.

How to locate the cause

Check the first relevant error in the .log file and inspect the source line it names. Look for a bare #, especially in text copied from a URL, a programming example, a macro definition, or a table of symbols. Then decide which case applies:

  • A visible hash: change # to \#.
  • An argument in a macro definition: use #1, #2, and so on inside that definition.
  • An argument in a definition nested inside another definition: double the marker, for example ##1.
  • A hash inside verbatim material: check whether the text really is inside the intended verbatim environment; an earlier unmatched brace or environment boundary may have ended it.

Common mistakes

  • Escaping a parameter marker instead of defining it: \#1 prints the characters #1; it does not refer to a macro argument.
  • Using #1 in the document body: parameter markers belong in macro definitions, not normal prose.
  • Forgetting to double hashes in nested definitions: the outer macro reads the definition first, so the inner parameter marker must be written as ##1 there.
  • Fixing only the line number: TeX may report the error after an earlier malformed command has changed how it reads the input. If the obvious hash is already escaped, check the preceding lines for missing braces or incorrectly ended environments.

References

  1. LaTeX2e unofficial reference manual

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