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
  • 2
    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
    Commented Sep 4, 2017 at 18:13

2 Answers 2

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.

4
  • 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
    Commented Sep 5, 2017 at 11:10
  • 1
    @Tohiko \def\newcmd#1#{\zzz{#1}} then \def\zzz#1#2#3{\mycmd#1{first}{,second}{#2 and #3}} Commented Sep 5, 2017 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
    Commented Sep 5, 2017 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 Commented Sep 5, 2017 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}
3
  • 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
    Commented Sep 5, 2017 at 11:37
  • @Tohiko I can't guess code you don't tell about, sorry
    – egreg
    Commented Sep 5, 2017 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
    Commented Sep 5, 2017 at 12:05

You must log in to answer this question.

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