I am trying to define a macro that has an optional argument that can take a macro with an optional argument as an argument. A minimal example to explain the problem more clearly:
\documentclass{article}
\newcommand*{\inn}[1][a]{#1}
\newcommand*{\out}[1][\inn]{\underline{#1}}
% \newcommand*{\out}[1][\inn]{\underline{#1}} % this also doesn't work (1)
% \newcommand*{\out}[1][\inn]{{#1}} % this also doesn't work (2)
\begin{document}
$\out[\inn[b]]$
% $\out[{\inn[b]}]$ % this works (3)
\end{document}
I get the error ! Argument of \\inn has an extra }
. As indicated, I can fix the issue on a case-by-case basis, but not in general (using the right definition of the macros).
I first thought this was an issue caused by \underline
being fragile. But using \ensuremath
or \protected
didn't lead me to a solution.
How can I fix the macros, so that they do what I want without errors?
]
, so you need to use the commented syntax when there is an optional argument here.{ }
is necessary. This is not really tedious.