# Use mathematical equation in caption with onlyamsmath

Apparently it seems to raise a warning »LaTeX Warning: Label(s) may have changed. Rerun to get cross-references right.« if I want to use a mathematical equation in a figure's caption and I use the onlyamsmath and the hyperref packages in the document's preamble as follows:

\documentclass{scrartcl}

\usepackage{amsmath}
\usepackage[all,error]{onlyamsmath}
\usepackage{hyperref}

\begin{document}

Reference~\ref{fig:foobar}.

\begin{figure}[htb!]
\caption{Foo Bar $f=2$}
\label{fig:foobar}
\end{figure}

\end{document}

I have currently no clue why this happens and what I should change ...

-
The warning is perfectly normal at the first run, because LaTeX doesn't yet know what number the label fig:foobar refers to. It will be issued every time LaTeX finds a new label or some reference has changed. A new run of LaTeX will fix the "problem". –  egreg Jun 15 '12 at 12:58
For more complicated documents even three, four,... compilation runs can be necessary. –  Stephen Jun 15 '12 at 17:19
I think you may have found a bug. The .aux file is unchanged from run 1 to run 2 of LaTeX, but the warning keeps coming up no matter how many times I run it. Disabling either onlyamsmath or hyperref makes the problem go away, so I imagine the problem is a conflict between those two packages (both of which do some scary low-level stuff). –  zwol Jun 17 '12 at 17:31
See my answer tex.stackexchange.com/a/78757/10777 how to remove the $feature from amsmath which is the source of many problems. – jofel Oct 23 '12 at 9:12 ## 3 Answers The right solution, in my opinion, is not using onlyamsmath at all: it makes more damages than it solves. However, if you really prefer using it, then accustom yourself to write $$a=b$$ for all formulas, instead of$a=b$. And remember to load also the fixltx2e package, otherwise formulas in captions will result in disasters when trying to use \listoffigures or \listoftables. \documentclass{scrartcl} \usepackage{fixltx2e} \usepackage{amsmath} \usepackage[all,error]{onlyamsmath} \usepackage{hyperref} \begin{document} Reference~\ref{fig:foobar}. \begin{figure}[htb!] \caption{Foo Bar $$f=2$$} \label{fig:foobar} \end{figure} \end{document} - This is solved when you use the LaTeX inline math environment $$...$$ instead of the commonly used (though plain TeX directive)$ ... $. - True, since the problem is in the catcode of$ (as I explained in my answer). –  mbork Jun 17 '12 at 17:49
Unfortunately this doesn't really solve the problem: look at the generated .aux file. –  egreg Jun 17 '12 at 18:02
@egreg: Gee wizz - just as long as you don't create a contents for your floats. –  Werner Jun 17 '12 at 18:08
@Werner This works fine, but how do you deal with a latex expression that involves braces, e.g., \caption{ $$\lambda_{\boldmath k}$$} produces an error –  Leo Simon Jan 9 at 19:34
@LeoSimon: You're probably looking for \caption{ $$\lambda_{\boldsymbol{k}}$$} - some macros need \protection in order to survive being written to the .lof. In those cases use \protect\<macro>. –  Werner Jan 9 at 19:49

The explanation is quite simple - but only once you know it;). Actually, I've been able to run into a similar issue without hyperref, but I suspect the problem is the same.

When you run LaTeX on a source file, first the aux file is read and after that the \AtBeginDocument commands are executed. And since onlyamsmath says

\AtBeginDocument{\catcode‘\$=13 } it means that labels etc. are read under normal catcode regime of$, and checked during compilation with $'s catcode set to "active". Therefore for all labels using this character LaTeX will always complain. (And what makes the problem tricky is that the character codes of everything in the aux file stay the same, i.e. the aux file itself stays identical in the sense of diff. The remedy to this problem is thus quite simple: just say \catcode`\$=13

somewhere in your preamble (best right before edit: not before, but after, since onlyamsmath uses \$ with its normal catcode too! \usepackage{onlyamsmath}, or at least take care so that between this line and loading of that package you never actually use the dollar sign).

-