Tx · LaTeX & TikZ

Why Does LaTeX Say \bfseries Is Invalid in Math Mode?

Published By the 9to5Science editorial team

At a glance

  1. 1. Check the error line

    Read the first LaTeX error in the log; TeXworks is usually reporting an error from the compiler, not causing it.

  2. 2. Inspect math delimiters

    Look for a citation, bold-text command, or other text command between an unmatched pair of math delimiters.

  3. 3. Move citations outside math

    Close the math expression before using \cite, even when the citation belongs to a table cell.

  4. 4. Compile a small example

    Test the corrected cell in a minimal document, then apply the same change to your full file.

TeXworks is the editor; the LaTeX engine it runs reports the error. The message LaTeX Error: Command \bfseries invalid in math mode means LaTeX tried to apply a text-style command while it was interpreting mathematical material. The command named in the message may come from a citation or formatting macro rather than from a \bfseries you typed yourself.

A frequent trigger is a citation placed between math delimiters, including inside a table cell. Another possibility is an earlier missing or extra $, \(, or \) that leaves LaTeX in math mode when it reaches otherwise ordinary text. The precise cause depends on the surrounding source, so use the log’s first error and check the nearby code.

Why this happens

LaTeX distinguishes between text mode and math mode. Commands such as \bfseries change the appearance of text; they are not mathematical notation and cannot be used directly as math commands. Citation commands such as \cite are also text-oriented. Depending on the citation setup, processing one inside math mode may lead to an error involving a text-font command such as \bfseries.

A tabular environment does not put every cell into math mode. Its cells are normally text, but a cell can enter inline math mode when you use $...$ or \(...\). Display-math commands such as \[...\] enter math mode too, but generally cannot be used directly inside a tabular cell. For example, Value: $x=1 \cite{source}$ puts the citation inside the math expression. Instead, close the math expression first: $x=1$~\cite{source}.

Check the source around the error

  1. In TeXworks, open the typesetting log and find the first error, not just the final message. Errors reported later may be consequences of the first one.
  2. Go to the indicated source line, then inspect the preceding lines too. TeX can discover that math mode is still open only after it encounters a later command.
  3. Match every math delimiter. Check for a missing $, an extra $, or an unclosed \(. Also check that \( is closed by \) and \[ by \].
  4. Move text commands, especially \cite, \textbf, and \bfseries, outside the math expression. If you need words within a formula, use \text{...} from amsmath for the words; keep citations outside the math delimiters.
  5. Recompile. If the error remains, temporarily comment out the table or citation and test a smaller example to isolate the command causing it.

Worked table example

This complete example places the citation after the formula, while keeping both in the same table cell. It uses only standard LaTeX commands and compiles with a normal LaTeX engine.

\documentclass{article}

\begin{document}

\begin{tabular}{lc}
Quantity & Value \\
Energy & $E=mc^2$~\cite{einstein}
\end{tabular}

\begin{thebibliography}{9}
\bibitem{einstein} A. Author, \emph{A Sample Reference}.
\end{thebibliography}

\end{document}

The $...$ delimiters contain only the formula. The ~ inserts a nonbreaking space, and \cite{einstein} follows the closing $, so LaTeX processes the citation in text mode. The bibliography entry is included so the example is self-contained. In your document, use your real citation key and bibliography setup.

If you intended bold math

Text bolding commands do not bold mathematical symbols. For bold Latin letters in a formula, use \mathbf, as in $\mathbf{x}$. For Greek letters and other symbols, \boldsymbol from amsmath is often suitable, or use the bm package’s \bm command. These are math-mode commands; they are different from \bfseries.

For example, with amsmath, \boldsymbol{\alpha} produces a bold alpha. Do not use \textbf{...} or \bfseries as a substitute for mathematical bolding inside $...$.

Common mistakes

  • Blaming TeXworks itself: The editor launches the compiler and displays its output. A LaTeX error usually points to something in the document source.
  • Changing the command named in the message without checking context: \bfseries may be triggered indirectly by a citation or another macro. Find where math mode was entered and whether it was properly closed.
  • Assuming table cells are all math: A table cell is text by default. Only the portion explicitly placed in math mode needs math commands.
  • Moving the citation but leaving a delimiter unmatched: Correcting one command will not fix an earlier unclosed $ or \(. Check the whole surrounding expression.
  • Treating an undefined citation as the same error: An undefined citation usually produces a warning about the citation key. It is a separate issue from using a text command in math mode.

If the delimiters are balanced and the citation is outside math mode but the error persists, make a minimal document containing the affected table cell and its packages. That often reveals whether another package or a custom macro is introducing the text command in math mode.

References

  1. LaTeX2e: An unofficial reference manual

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