# How to refer to labels in math mode from math mode

If you refer to item labels that are written in math mode from some other math mode occurrence, then you get errors. I guess it's because it leads to nested math modes. Is there any way to avoid these errors?

Here's an example where referring to the first label in math mode gives one error and referring to the second label gives another:

\documentclass{article}

\usepackage{enumitem}
\usepackage{fixltx2e}% For the label with  to work

\begin{document}

\begin{enumerate}[label=$e'_{\arabic*}$]
\item Bla bla bla\label{e1}
\end{enumerate}

\begin{enumerate}[label=$$e'_{\arabic*}$$]
\item Bla bla bla\label{vare1}
\end{enumerate}

$$h \mid \ref{e1}$$

$$h \mid \ref{vare1}$$

\end{document}


You should be able to use ensuremath to help you out here. The following compiles for me. I replaced the dollar signs with the ensuremath environment.

\begin{enumerate}[label=\ensuremath{e'_{\arabic*}}]
\item Bla bla bla\label{e1}
\end{enumerate}

\ref{e1}
$$h \mid \ref{e1}$$


Alternatively you can use mbox

\begin{enumerate}[label=$e'_{\arabic*}$]
\item Bla bla bla\label{e1}
\end{enumerate}

\ref{e1}
$$h \mid \mbox{\ref{e1}}$$

• I'm curious why this works. If you know could you please include some explanation in your answer? – N.N. Nov 30 '11 at 14:47
• I assume it's because the ensuremath is only expanded when the label is referenced. That is a guess however, and when I tried this out I expected it to fail. – qubyte Nov 30 '11 at 14:49
• Maybe someone else knows and cares to elaborate. What did you mean by "I guess it'd be back to front..." in your comment to my question? – N.N. Nov 30 '11 at 14:52
• I expected ensuremath to be doing the same as $...$, so I was thinking about something to do the opposite. i.e. an ensurenotmath to put around the ref. Outside an equation the ref would be the usual \ref{e1}, and inside an equation it'd be $...\ensurenotmath{\ref{e1}}$. – qubyte Nov 30 '11 at 14:59
• Is there any reason to prefer either of your solutions? The one with \ensuremath seems better because it don't require to \mbox every occurence of \ref. – N.N. Nov 30 '11 at 15:06