2

I have a command which takes multiple arguments, one of which is a star:

\NewDocumentCommand{\foo}{ s m m }{
    \IfBooleanTF{#1}{Asterisk is here.}{Asterisk is not here.} #2 #3.
}

I would like to define another command which works like the first one, but with some predifined arguments, like

\NewDocumentCommand{\baz}{ s m }{
    \IfBooleanTF{#1}{
        \foo*{#2}{a}
    }{
        \foo{#2}{a}
    }
}

\baz and \baz* work in the same way, so I would like to somehow define \baz without repeating the {#2}{a} part two times. I have tried

\NewDocumentCommand{\baz}{ s m }{
    \expandafter\foo\IfBooleanT{#1}{*}{#2}{a}
}

but it doesn't work. Complete MWE

\documentclass{article}

\usepackage{xparse}

\NewDocumentCommand{\foo}{ s m m }{
    \IfBooleanTF{#1}{Asterisk is here.}{Asterisk is not here.} #2 #3.
}

\NewDocumentCommand{\baz}{ s m }{
    \IfBooleanTF{#1}{
        \foo*{#2}{a}
    }{
        \foo{#2}{a}
    }
}

\begin{document}

    \baz{b}

    \baz*{c}

\end{document}
4
  • It's not clear to me that \foo here is a document command: is it intended for direct use too?
    – Joseph Wright
    Commented Aug 13, 2019 at 14:04
  • @JosephWright no, but in my document command \foo takes a lot of key-value options, and there is also a number of commands like \baz, with predeifned list of these options. Do you mean that I can just insert the defnition of \foo inside \baz? Commented Aug 13, 2019 at 14:07
  • I was getting at more that the LaTeX3 model is that document commands have stars, optional arguments, etc., whereas 'code level' commands which implement them have fixed mandatory arguments. I'm wary of passing on a star ...
    – Joseph Wright
    Commented Aug 13, 2019 at 14:12
  • @JosephWright, this is a good idea, I will probably implement it that way. Thanx! Commented Aug 13, 2019 at 14:13

1 Answer 1

4

As TeX works by macro expansion, there is no need to have all of the arguments for \foo in the conditional, so we can just have the unique part:

\documentclass{article}

\usepackage{xparse}

\NewDocumentCommand{\foo}{ s m m }{%
    \IfBooleanTF{#1}{Asterisk is here.}{Asterisk is not here.} #2 #3.%
}

\NewDocumentCommand{\baz}{ s m }{%
    \IfBooleanTF{#1}%
      {\foo*}%
      {\foo}%
        {#2}{a}%
}

\begin{document}

    \baz{b}

    \baz*{c}

\end{document}
1
  • 1
    I've tided up line ends here: we are not in \ExplSyntaxOn conditions so spaces are important.
    – Joseph Wright
    Commented Aug 13, 2019 at 14:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .