6

I would like to pass e.g. a s or a t parameter when calling a new macro. I know this can be done by using \IfBooleanTF#1 etc. a couple of times, but I like to know whether it can be done without evaluating them.

My MWE

\documentclass{article}
\usepackage{expl3,xparse}

\newsavebox\mysavebox

\NewDocumentCommand\onehand{st+m}{%
  \sbox{\mysavebox}{\hand {#1} {#2} {#3}}%
}

\NewDocumentCommand\hand{st+m}{%
   \IfBooleanTF#1{1}{}%
   \IfBooleanTF#2{2}{}%
   #3%
}
\begin{document}
\hand{X}\\\hand*{X}\\\hand+{X}\\\hand*+{X}\\[2em]
\onehand{X}\usebox{\mysavebox}\\
\onehand*{X}\usebox{\mysavebox}\\
\onehand+{X}\usebox{\mysavebox}\\
\onehand*+{X}\usebox{\mysavebox}
\end{document}

I tried also without the braces like \hand#1#2{#3} But this yields

X
1 X
2X
12X
X
*X
+X
*+X
Jack
  • 1,137
  • #1 or #2 is either \BooleanFalse or \BooleanTrue, but not the * or + any longer you might expect. You have spurious spaces there as well –  Aug 01 '17 at 20:23
  • And bad boxes .... Never use \\ to break lines outside special contexts such as tabular. – cfr Aug 02 '17 at 01:29

2 Answers2

2

The #1 and #2 are evaluated by xparse's parser to be \BooleanFalse or \BooleanTrue and are not recognized any longer as * or +, so \hand sees {\BooleanTrue}{\BooleanFalse}{X} etc., which are transferred in to expl3 bool constants, i.e. displaying in Greek characters later on.

The easiest way is to use moving arguments, i.e. specify \onehand without arguments and let \hand do the evaluation.

\documentclass{article}
\usepackage{xparse}

\NewDocumentCommand\onehand{}{%
  \hand%
}

\NewDocumentCommand\hand{st+m}{%
  \IfBooleanTF{#1}{1}{}%
  \IfBooleanTF{#2}{2}{}%
  #3
}
\begin{document}
\hand{X}

\hand*{X}

\hand+{X}

\hand*+{X}\\[2em]

\onehand{X}

\onehand*{X}

\onehand+{X}

\onehand*+{X}
\end{document}

enter image description here

  • Note: One could check for \BooleanFalse etc. and generate a token, but what would we get then, it's effectively the same like asking with \IfBooleanTF. –  Aug 01 '17 at 20:52
  • 1
    My MWE was too minimal! I need to put the stuff into a savebox and then your solution doesn't work anymore. I edited my question to reflect this (and removed spurious spaces) – Jack Aug 01 '17 at 21:47
  • @Jack: Yes, but in this case you're changing the question... It's your responsibility of providing the correct MWE and providing a clear question –  Aug 01 '17 at 21:48
1

It's not generally a good idea to use xparse this way. Anyway, you have to pass * or + to \hand, not #1 or #2.

\documentclass{article}
\usepackage{expl3,xparse}

\NewDocumentCommand\onehand{st+m}{%
  \IfBooleanTF#1%
   {%
    \IfBooleanTF#2{\hand*+{#3}}{\hand*{#3}}%
   }%
   {%
    \IfBooleanTF#2{\hand+{#3}}{\hand{#3}}%
   }%
}

\NewDocumentCommand\hand{st+m}{%
   \IfBooleanTF#1{1}{}%
   \IfBooleanTF#2{2}{}%
   #3%
}
\begin{document}

\noindent
\hand{X}\\\hand*{X}\\\hand+{X}\\\hand*+{X}

\bigskip

\noindent
\onehand{X}\\\onehand*{X}\\\onehand+{X}\\\onehand*+{X}

\end{document}

enter image description here

egreg
  • 1,121,712
  • 2
    As I stated it in my question: I knew the solution with some \IfBooleanTF#N. I was just curious if it could be done otherwise. Thank you for pointing out I was misusing xparse. – Jack Aug 02 '17 at 14:26
  • @Jack No, the * or + are gobbled and you cannot know what token made the macro to return \BooleanTrue or \BooleanFalse. – egreg Aug 02 '17 at 14:28