I've got a problem with nested macro definitions. While my use case is a bit different, what follows is a minimum (non-)working example:
What I want to do:
I want to dynamically define a group of macros \One
, \Two
, and \Three
.
They shall take the same number of arguments each, but this number of arguments should also be variable. By calling
\docsvlist{mm, One(#1), Two(#1 #2)}
I want all macros to be defined such that they take two mandatory arguments and
\One{Hi}{Hello}
\Two{Hi}{Hello}
\Three{Hi}{Hello}
to 'return'
One: One(Hi)
Two: Two(Hi Hello)
Three: One(Hi) + Two(Hi Hello)
The code:
\usepackage{xparse}
\usepackage{etextools}
\DeclareDocumentCommand{\myDeclare}{mmm}{
\DeclareDocumentCommand{\One}{#1}{One: #2\par}
\DeclareDocumentCommand{\Two}{#1}{Two: #3\par}
\DeclareDocumentCommand{\Three}{#1}{Three: #2 + #3\par}
}
\DeclareDocumentCommand{\do}{m}{
\DeclareDocumentCommand{\do}{m}{
%This would work for \One:
%\DeclareDocumentCommand{\One}{#1}{One: ##1\par}
\DeclareDocumentCommand{\do}{m}{
%This also works for \Two:
%\DeclareDocumentCommand{\Two}{#1}{Two: ####1\par}
\myDeclare{#1}{##1}{####1}
%This also does not work for \Three:
%\DeclareDocumentCommand{\Three}{#1}{Three: ##1 + ####1\par}
}
}
}
\docsvlist{mm, One(#1), Two(#1 #2)}
\One{Hi}{Hello}
\Two{Hi}{Hello}
\Three{Hi}{Hello}
This produces the following output:
One: One(Two(Hi Hello))
Two: Two(Hi Hello)
Three: One(Two(Hi Hello)) + Two(Hi Hello)
What I would actually want:
One: One(Hi)
Two: Two(Hi Hello)
Three: One(Hi) + Two(Hi Hello)
My question:
What happens? I was surprised that it works at all. \Two
gets defined as I want it to, but \One
only gets defined correctly if I define it in the way that i have commented out. I can't find a way to define \Three
correctly at all.
In the call to
\myDeclare{#1}{##1}{####1}
the ##1
seems to get changed from One(#1)
to One(####1)
and then subsequently to One(Two(#1 #2))
in the definition of \One
.
But why? And how could I prevent this?
Edit: The actual use case
I want to simultaneously define several mathematical functions or equalities (or something similar), as for (mock) example as follows:
\MyDefine{
aSet, {},
\Omega, \subset \Reals,
{a nice set},
aFunction, O{i},
f^{#1}, :\aSet \to \aSet,
{some $#1$-th function},
anEquality, O{i}O{j},
\aFunction[#1] \leq \aFunction[#2], \text{for $#1 \leq #2$},
{a nice inequality}
}
This should define (among others, updated) :
\DeclareDocumentCommand{\aSet}{}{
\Omega
}
\DeclareDocumentCommand{\aSetDefinition}{}{
\subset \Reals
}
\DeclareDocumentCommand{\aSetDescription}{}{
a nice set
}
\DeclareDocumentCommand{\aSetLabel}{}{def:aSet}
\DeclareDocumentCommand{\aFunction}{O{i}}{
f^{#1}
}
\DeclareDocumentCommand{\aFunctionDefinition}{O{i}}{
:\aSet \to \aSet
}
\DeclareDocumentCommand{\aFunctionDescription}{O{i}}{
some $#1$-th function
}
\DeclareDocumentCommand{\aFunctionLabel}{}{def:aFunction}
\DeclareDocumentCommand{\anEquality}{O{i}O{j}}{
\aFunction[#1] \leq \aFunction[#2]
}
...
for later use and also generate (e.g.):
\begin{align}
\aSet& \aSetDefinition&& \text{\aSetDescription}\label{\aSetLabel}\\
\aFunction& ...\\
\anEquality& ...
\end{align}
\docsvlist{mm, One(##1), Two(#1 #2)}
– Andrew Swann Apr 1 '17 at 18:41\aSet
,\aFunction
and\anEquality
be? And what's the role of “some parameters”? – egreg Apr 2 '17 at 13:09