2

I have a command defined as follows

\documentclass{article}
\makeatletter%
\newcommand{\@withstar}[3]{We have star #1, #2, #3}
\newcommand{\@withoutstar}[3]{We have #1, #2, #3}
\DeclareRobustCommand\mycmd{%
  \@ifnextchar *%
  {\@firstoftwo{\@withstar}} %
  {\@withoutstar}
}%
\makeatother%

\begin{document}
\mycmd{first}{second}{and third}
\mycmd*{first}{second}{and third}
\end{document}

I want to define another command that just passes the next character to \mycmd and sets specific arguments. Something along the lines of

\newcommand\newcmd[1]{\mycmd\nextchar{first}{,second}}

Then I can call it like

 \newcmd{and third}

or

 \newcmd*{and third}

I understand that I can repeat the test for next char (using @ifnextchar) inside the definition of \newcmd, but I would like to avoid that if possible because in my real situation \mycmd tests for many characters, not just *.

  • 1
    Please provide a compilable document, not just fragments, but your request might be easier to achieve with xparse and its \NewDocumentCommand and argument specifier features – user31729 Sep 4 '17 at 18:13
5

Something like

\def\newcmd#1#{\mycmd#1{first}{,second}}

#1 will be all tokens between \newcmd and { which will be empty or * in your examples.

  • This works! However, is there a way to manipulate the remaining arguments? For example, something like this \def\newcmd#1#{\mycmd#1{first}{,second}{#2 and #3}} – Tohiko Sep 5 '17 at 11:10
  • 1
    @Tohiko \def\newcmd#1#{\zzz{#1}} then \def\zzz#1#2#3{\mycmd#1{first}{,second}{#2 and #3}} – David Carlisle Sep 5 '17 at 11:20
  • Can you please explain how introducing \zzz helps? In the beginning I tried \def\newcmd#1#2#3{\mycmd#1{first}{,second}{#2 and #3}} but the mode \newcmd{third}{fourth} did not work. I am not 100% sure why introducing \zzz seems to fix everything. – Tohiko Sep 5 '17 at 11:35
  • the special form #1# (which was why I used def not newcommand) is only available for the last argument (it grabs up the the first { so you need a one-argument command to grab the * or nothing before the first { and after that can just use standard \def or \newcommand @Tohiko – David Carlisle Sep 5 '17 at 12:29
2

No code duplication with xparse and no auxiliary macros:

\documentclass{article}
\usepackage{xparse}

\NewDocumentCommand{\mycmd}{smmm}{%
  \IfBooleanTF{#1}
    {We have star #2, #3, #4}
    {We have #2, #3, #4}%
}

\NewDocumentCommand{\newcmd}{sm}{%
  \IfBooleanTF{#1}{\mycmd*}{\mycmd}{first}{second}{#2}%
}

\begin{document}

\mycmd{first}{second}{and third}

\mycmd*{first}{second}{and third}

\newcmd{and third}

\newcmd*{and third}

\end{document}
  • Doesn't this solution duplicate the character check using \IfBooleanTF? In my real application I have many checks that I need to do on the first character not just *. – Tohiko Sep 5 '17 at 11:37
  • @Tohiko I can't guess code you don't tell about, sorry – egreg Sep 5 '17 at 12:00
  • I do explain this particular point at the end of my original post. But no worries, thanks for directing me to xparse. – Tohiko Sep 5 '17 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.