2

I have the following code:

\documentclass{article}

\usepackage{amsmath}

\def\cma#1{$A_{c}(#1)$}

\def\cmbs#1{B_{c_{\mathcal{L}}}^{#1}}
\def\cmb#1{\ifmmode\cmbs{#1}\else$\cmbs{#1}$\fi}

\begin{document}

\listoffigures

\begin{figure}[h]
\caption{\cma{\cmb{+}}}
\end{figure}

\end{document}

When trying to compile it (the second time to get the list of figures), I get:

! Missing $ inserted.

<inserted text>

l.1 ...es $A_{c}($B_{c_{\mathcal {L}}}^{+}$)$}}{1}

If I comment \listoffigures, everything works fine. When I look at the .lof file, I see:

\contentsline {figure}{\numberline {1}{\ignorespaces $A_{c}($B_{c_{\mathcal {L}}}^{+}$)$}}{1}

Which seems to imply that the \ifmmode command did not work properly...

Is there any way to work around that without having to explicitly use \cma{\cmbs{+}}?

1
  • @campa Thanks, \ensuremath did the trick!
    – Holt
    Commented May 3, 2018 at 10:17

1 Answer 1

3

When LaTeX writes the line for the table of contents it is not in math mode.

You could define

\newcommand{\cma}[1]{\ensuremath{A_{c}(#1)}}
\newcommand{\cmb}[1]{\ensuremath{B_{c_{\mathcal{L}}}^{#1}}}

and then

\caption{\cma{\cmb{+}}}

will work. On the other hand, I see no improvement over

\newcommand{\cma}[1]{A_{c}(#1)}
\newcommand{\cmb}[1]{B_{c_{\mathcal{L}}}^{#1}}

and the more semantic

\caption{$\cma{\cmb{1}}$}

Avoid \def.

1
  • Thanks, it worked with ensuremath. I don't want to use $\cma{\cmb{+}}$ because \cma and \cmb are basically placeholders for algorithm names, and I have yet to decide their final value (and I don't want to have to replace $\cma{\cmb{1}}$ by \cma{\cmb{1}} everywhere if I decide not to use math-mode for the algorithm names).
    – Holt
    Commented May 3, 2018 at 10:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .