I have defined a LaTeX command with an optional argument, e.g., \newcommand*{\commA}[1][opt]{A#1}
, and I cannot get it to be part of an expanded command defintion with \edef
, e.g., \edef\expcom{\commA}
.
I have tried different combinations to find out what is going on: first, if I define the command \commA
without optional arguments, the code compiles smoothly. Second, if I leave \commA
definition with optional arguments, but I define \expcom
with \def
, the code compiles smoothly, too. Thus, It is only the combination of optional argument definition and \edef
that it is faulty.
How can I pass a command with optional arguments to a \edef
definition?
See example of buggy code
\documentclass{article}
\begin{document}
%\newcommand*{\commA}{A} %This definition gives no error
\newcommand*{\commA}[1][opt]{A#1} %This definition raises error "! Argument of \reserved@a has an extra }."
\edef\expcomm{\commA}
%\def\expcomm{\commA} %The definition with \def gives no error
\def\noexpcomm{\commA}
\noindent
This is \verb|\expcomm|: \expcomm\\
This is \verb|\noexpcomm|; \noexpcomm\\
\renewcommand*{\commA}{B}
\noindent
This is \verb|\expcomm|: \expcomm\\
This is \verb|\noexpcomm|; \noexpcomm\\
\end{document}
The output I would expect with the \commA
definition with optional argument is following:
When I use the \commA
definition without optional argument I get following, as expected:
\edef
. Please, tell us what you really want to achieve, unless this is just an academic question.\NewExpandableDocumentCommand\commA{O{opt}}{A#1}
the new kernel will happily inform you thatLaTeX cmd Error: Argument specification 'O{opt}' for expandable command '\commA' ends with optional argument.
... so I suppose it can't be done.