How do you access macro arguments if the macros are nested by more than two levels?
To explain it in more detail: Creating a new command in another command (nesting command definitions), it is possible to implement something like partial application of a command. This is useful for example if a command, which is often called, has two arguments and one changes less frequently than the other:
\documentclass{minimal}
\begin{document}
\newcommand{\one}[1]{%
\newcommand{\two}[1]{#1, ##1}%
}
\one{a} % Partial application, nothing gets printed
\two{b} \quad \two{c}
\end{document}
Unfortunately this does not work, if there is another level of nesting involved:
\documentclass{minimal}
\begin{document}
\newcommand{\one}[1]{%
\newcommand{\two}[1]{%
\newcommand{\three}[1]{#1, ##1, ###1}%
}
}
\one{a} % First partial application -> Error
\two{b} % Second partial application
\three{c}
\end{document}
The error occurs in the marked line with the following message:
[LaTeX] Line 8: Illegal parameter number in definition of \two.<to be read again> a
l.0 \one{c}
% First partial application -> Error
As substituting \one{a}
by \one{1}
in the above code leads to the output 1, b, b
, I think that ###1
is interpreted as ##{#1}
. So ###1
is expanded to ##a
or ##1
, respectively. The first one obviously fails, where the second one compiles but leads to an unexpected result. I tested the commands from xparse
, too, and the problem seems to be identical.
To rephrase the question above: How do you have ###1
interpreted as {###1}
instead of ##{#1}
when using the nested command definitions? Why is ##1
in the first example interpreted as {##1}
instead of #{#1}
?
I know that it is possible to avoid the third level of nesting by some sort of currying as in the following example.
\documentclass{minimal}
\begin{document}
\newcommand{\one}[1]{%
\newcommand{\two}[1]{\helper{#1, ##1}}%
}
\newcommand{\helper}[1]{%
\newcommand{\three}[1]{%#1, ##1}%
}
\one{1} % First partial application
\two{b} % Second partial application by currying
\three{c}
\end{document}
It seems less intuitive and more complicated to me to use two levels of nesting twice instead of three levels of nesting once and I would like to understand how the third level of nesting is used.
#
s in the definition of\three
: something like\newcommand{\three}[1]{#1, ##1, ####1}
.##1
) in an argument?