2

I have a weird problem with one equation in my document: whenever I clear the output files to do a fresh re-compilation, all bibliography-related files have to be regenerated too, and this piece of code fails to compile:

\begin{equation}
    A\stackrel{\cite{A01}}{=}B
\end{equation}

Error: Command \bfseries invalid in math mode. A\stackrel{\cite{A01}}{=}

I figured out that if I edit the file like this:

\begin{equation}
    A\stackrel{\cite{A01}}{=}B
%   A=B  %First compile with this, then with the other!!
\end{equation}

I just need to comment the first line, uncomment the second, compile, toggle back the commented lines and re-compile and it works.

Basically it fails only on fresh compilations, after that it's no problem. But still it's annoying since I'm working 24/7 on my thesis and have to do it at least a couple of times a day.
Also: when handing in my thesis (including the LaTeX code) someone will run into this problem, and I would like to not forcing them to figure out what is wrong to be able to compile my file.

Thanks for any help on this!

MWE:

\documentclass{scrreprt}
\usepackage{natbib}
\usepackage{hyperref}
\usepackage{filecontents}

\begin{filecontents*}{foo.bib}
    @misc{A01,
        author = {Author, A.},
        year = {2001},
        title = {Alpha},
    }
    @misc{B02,
        author = {Buthor, B.},
        year = {2002},
        title = {Bravo},
    }
\end{filecontents*}

\begin{document}
    \begin{equation}
    A\stackrel{\cite{A01}}{=}B
%   A=B  %First compile with this, then with the other!!
    \end{equation}

    \nocite{*} 
    \bibliography{foo}
    \bibliographystyle{plain}
\end{document}
  • You need to go back into text mode. Load \usepackage{amsmath} and then say A\stackrel{\text{\cite{A01}}}{=}B. – moewe Mar 15 '19 at 11:51
  • Oh I see! it has trouble printing the (??) it does during first compilations because that it bold :facepalm: Thanks! The error message was pretty abstract to me since 1) I wasn't asking for anything in bold and 2) only first compilations failed, after that it had no problems at all. But now it's clear! – Superuser27 Mar 15 '19 at 11:56
3

In

\begin{equation}
  A\stackrel{\cite{A01}}{=}B
\end{equation}

the \cite{A01} is in math mode. You can easily verify this by writing something like x^2 there.

When LaTeX tries to process the citation in math mode on the first run, when it is still unknown, the \reset@font\bfseries ? natbib wants to print causes the error you see: Command \bfseries invalid in math mode.

The way to avoid this is to make sure to go back to text mode. I'd do that by loading amsmath and saying

A\stackrel{\text{\cite{A01}}}{=}B

Incidentally, the error does not happen with the standard definition of \cite (i.e. if you don't load natbib), because that prints the question mark in a \hbox and thus breaks out of math mode

\hbox{\reset@font\bfseries ?}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.