5

I am using the command \newcommand\emcpt[1]{{\bfseries #1}} to emphasize the headings of my figures. I also created a command to facilitate using percentages \newcommand\percent[1]{\qty{#1}{\percent}} using the siunitx package. While both commands alone work well (and \percent in \emcpt in textmode too), using \percent inside of \emcpt inside of \caption results in the errors ! Argument of \percent has an extra } and ! Paragraph ended before \percent was complete. The formatting however is as expected. How can I get rid of the errors?

\documentclass{article}

\usepackage{mwe}

\usepackage[detect-all]{siunitx}

\newcommand\emcpt[1]{{\bfseries #1}}
\newcommand\percent[1]{\qty{#1}{\percent}}

\begin{document}

\begin{figure}
\centering
\includegraphics{example-image}
\caption{\emcpt{Test: \percent{20} }}
\end{figure}

\end{document}
0

1 Answer 1

5

In normal text, \qty does several local redefinitions of commands, including \percent.

However, the argument to \caption is a moving argument, meaning that your \percent will also be expanded when writing the text in the .aux file, but at that moment the valid definition of \percent is your own, so you get garbage.

Use \DeclareRobustCommand.

Also I don't see a need to define \emcpt to do the same as \textbf. Better aliasing it.

\documentclass{article}
\usepackage[detect-all]{siunitx}
\usepackage{graphicx}

\NewCommandCopy{\emcpt}{\textbf}
\DeclareRobustCommand\percent[1]{\qty{#1}{\percent}}

\begin{document}

\begin{figure}
\centering
\includegraphics[width=4cm]{example-image}
\caption{\emcpt{Test: \percent{20} }}
\end{figure}

\end{document}

enter image description here

You must log in to answer this question.

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