[Tex/LaTex] highlight (soul package) in figure caption generates error

captionssoul

I have a short command that allows me to highlight some text for internal review purposes. Unfortunately, when I use it in a Caption of a figure it gives me the following error.

Argument of \@textcolor has an extra }. ...MA{This is my comment} More caption text.}
Paragraph ended before \@textcolor was complete. ...MA{This is my comment} More caption text.}

Here is my MWE that generates that error:

\documentclass{article}

\usepackage{graphicx}           % For figures and stuff
\usepackage{xcolor}             % Colors
\usepackage{soul}               % Highlight with \hl

%Colored comments for internal review purposes
\newcommand{\MA}[1]{{\sethlcolor{lime}\hl{#1}}}

\begin{document}

Highlighting \MA{in the text} works fine.


\begin{figure}[htbp!]
    \centering
    \includegraphics[width=3cm]{example-image}
    \caption{This is a picture. \MA{This is my comment} More caption text.}
    \label{fig:Examplefigure}
\end{figure}

\end{document}

Best Answer

Macro \MA is not robust for moving arguments like figure captions.

  • Use \protect:

    \protect\MA{...}
    
  • Or define a robust command:

    \newcommand{\MA}{}% Error, if \MA is already defined.
    \DeclareRobustCommand{\MA}[1]{{\sethlcolor{lime}\hl{#1}}}