Problem with nesting macros
Problem with nesting macros
I have written a macro to denote either the Fourier transformation (curly F by itself) or the Fourier transform of a function (curly F followed by a function and enclosed in left( and right) ). It does so by testing if the argument is equal to void or not:
newcommand*fourier[1]ensuremathmathscrFifthenelseequal#1!left(#1right)%
Therefore
fourier
or
fourierf(omega t)
give the expected results.
I wanted to apply fourier twice to a function and therefore wrote
fourierfourierf(x)
However, I'm getting a ! Missing endcsname inserted.
error. How should I modify my macro so that it allows nesting? I'm guessing it's because of the ifthenelse
construct?
! Missing endcsname inserted.
ifthenelse
2 Answers
2
Similar to Skillmon's original answer, but pretending to be mathtools
and sticking to the mandatory argument. The starred version uses left
/right
, the unstarred version has an optional parameter for big
/Big
/...
mathtools
left
right
big
Big
Taking the approach to left
and right
from Mateus Araújo's answer to Spacing around left and right with help from Philipp Stephani (thanks to Ruixi Zhang for the suggestion in the comments)
left
right
documentclass[british]article
usepackage[T1]fontenc
usepackage[utf8]inputenc
usepackagebabel
usepackageamsmath
usepackagemathrsfs
usepackageifthen
makeatletter
DeclareRobustCommandfourier%
mathscrF%
@ifstar
fourier@paren@star
fourier@paren@expl
newcommandfourier@paren@star[1]%
ifrelaxdetokenize#1relax
else
mathopenmathcloseleft(#1right)%
fi
newcommandfourier@paren@expl[2]%
ifrelaxdetokenize#2relax
else
mathopen#1(#2mathclose#1)%
fi
makeatother
begindocument
[ fourier ]
[ fourierf(omega t) ]
[ fourierfourierf(x) ]
[ fourier*fourier*fracf^22pi(x) ]
enddocument
!
left
right
mathopenmathcloseleft( #1 right)
fourier[big]...
bigl
bigr
@RuixiZhang Thanks for the hint. I had copied the
left
right
bit from the OP. I'll see if I can find something that works for big
->bigl
/bigr
, though I'm not sure if that is necessary when we already use mathopen
and mathclose
.– moewe
Sep 5 '18 at 16:06
left
right
big
bigl
bigr
mathopen
mathclose
@RuixiZhang Given that
bigl
is just defbiglmathopenbig
I don't think the conversion from big
to bigl
is really necessary here. If anyone is interested I came up with newcommandfrde@size@getlr[3]csnameexpandafter@gobblestring#1#2endcsname#3
which you can use as frde@size@getlrbigl(
to get bigl(
.– moewe
Sep 5 '18 at 19:26
bigl
defbiglmathopenbig
big
bigl
newcommandfrde@size@getlr[3]csnameexpandafter@gobblestring#1#2endcsname#3
frde@size@getlrbigl(
bigl(
You are right. Unlike the “simple wrapper” from
mathtools
which does @nameuse MH_cs_to_str:N ##1 l #2
and @nameuse MH_cs_to_str:N ##1 r #3
, going this extra mile to get bigl(
just seems convoluted.– Ruixi Zhang
Sep 5 '18 at 19:47
mathtools
@nameuse MH_cs_to_str:N ##1 l #2
@nameuse MH_cs_to_str:N ##1 r #3
bigl(
@FrédéricDelacroix Many expert users prefer the explicit size commands over the automatic sizing with
left
/right
because the automatic commands may pick out sizes that are unnecessarily large or too small in certain situations (there is even a quote from the TeXbook that acknowledges that and it is reproduced in an answer here, but I can't find that right now).– moewe
Sep 6 '18 at 8:13
left
right
I'd use the following:
ensuremath
ifthenelse
ifrelaxdetokenize#1relax
I've made a mistake therefore the former code grabbed the arguments wrong. The following uses xparse
to grab the arguments in a more robust way. It therefore doesn't use the ifrelaxdetokenize#1relax
test but xparse
's IfValueT
.
xparse
ifrelaxdetokenize#1relax
xparse
IfValueT
Results:
documentclassarticle
usepackagemathrsfs
usepackagexparse
NewDocumentCommand fourier o
%
mathscrFIfValueT#1!left(#1right)%
%
begindocument
$fourier[fourier[f]](x)$
enddocument
Thanks for contributing an answer to TeX - LaTeX Stack Exchange!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
That space
!
beforeleft
andright
though. Why not use the classic solution by Philipp Stephani:mathopenmathcloseleft( #1 right)
. For thefourier[big]...
variants, I was wondering if there is a possible direct use ofbigl
andbigr
. ;-)– Ruixi Zhang
Sep 5 '18 at 15:56