I've been using a macro X
similar to
\newcommand{\X}[1]{{\ttfamily{\detokenize{#1}}}}
to format certain words that may contain an underscore, without having to escape with \_
.
This worked until I used it in a \caption
. This results in an error with both pdflatex and lualatex (! Missing $ inserted.
). Consider this MWE:
\documentclass{article}
\newcommand{\X}[1]{{\ttfamily{\detokenize{#1}}}}
\begin{document}
\listoffigures
\begin{figure}
\centering
\caption{One \X{a_b} Two}
\end{figure}
\end{document}
Do I have to use a different / more elaborate solution for X
or should I just escape the underscores generally? I am more interested in the best practices instead of a very clever hack.