3

Feeding

\documentclass{article}
\pagestyle{empty}
\usepackage{todonotes}
\begin{document}%
\listoftodos
\todo{\(\ifmmode MATH \else TEXT\fi\)}%%% $…$ instead of \(…\) fails as well.
\end{document}

to pdflatex yields

output

However, the expected outcome is printed in the Todo list, and not .

Why does this happen, who is the culprit, and what to do?

The same issue: https://github.com/henrikmidtiby/todonotes/issues/63 .

2 Answers2

3

The problem is that when TeX does a \write (for instance when adding the entry to the todolist) it is in no mode and doesn't typeset, but expands macros and conditionals.

You might define your own robust command:

\NewDocumentCommand{\MathModeTF}{mm}{\ifmmode #1\else #2\fi}

or

\DeclareRobustCommand{\MathModeTF}[2]{\ifmmode #1\else #2\fi}

(I'd prefer the former) so the evaluation of the conditional is delayed until typesetting is being done. Call it like

\MathModeTF{MATH}{TEXT}

On the other hand, the LaTeX kernel already has \TextOrMath{TEXT}{MATH} with just the arguments switched.

\documentclass{article}
\pagestyle{empty}
\usepackage{todonotes}
\begin{document}%
\listoftodos
\todo{\(\TextOrMath{TEXT}{MATH}\)}%%% $…$ instead of \(…\) fails as well.
\end{document}

enter image description here

egreg
  • 1,121,712
1

For the sake of a "general" solution, this answer demonstrates how to "solve" the issue with \protect etc..

\documentclass{article}
\pagestyle{empty}
\usepackage{todonotes}
\usepackage{etoolbox}
\begin{document}%
\listoftodos
\todo{\(\protect\ifmmode MATH \protect\else TEXT\protect\fi\)}
\todo{\protecting{\(\ifmmode MATH \else TEXT\fi\)}}  % need etoolbox

\end{document}

Both ways work.


General question: What is the difference between Fragile and Robust commands? When and why do we need \protect?

Other questions about moving argument in \todo: 1 2

user202729
  • 7,143