# New command and the corresponding star counterpart with xparse

The following works as expected:

\newcommand{\engl}[2]{#1 (en. \textsl{#2})\index{en-ro}{#2}}


However, what I want to achieve would be a * version of it which only adds to the indices, without the visible text, something like:

\newcommand{\engl*}[2]{#1 (en. \textsl{#2})\index{en-ro}{#2}\term{#1!#2}\term{#2!#1}}


Of course, this does not work (the pdf is compiled, but with lots of warnings which were not there with the original definition), so I've tried something equivalent with xparse:

\usepackage{xparse}
\NewDocumentCommand\engl{sm}{%
\IfBooleanTF#1
{\index{en-ro}{#1}\term{#1}}
{#1 (en. \textsl{#2})\index{en-ro}{#2}\term{#1!#2}\term{#2!#1}}%
}


which doesn't work either.

How to properly create such a command and its * counterpart?

-
At least related: Defining starred versions of commands –  cgnieder Jan 13 '13 at 17:27

## 2 Answers

From the xparse 'point of view', a star is an additional optional argument. Thus #1 is a boolean representing 'there is a star', while #2 and #3 are the two mandatory arguments. This leads to

\documentclass{article}
\usepackage{xparse}
\NewDocumentCommand{\engl}{smm}{%
#2 (en. \textsl{#3})\index{en-ro}{#3}%
\IfBooleanT#1
{\textrm{#2!#3}\textrm{#3!#2}}%
}
\begin{document}

\engl{X}{aaa}

\engl*{X}{aaa}

\end{document}


where I've shared code as far as possible and only provided a TRUE case for the presence of a star.

-
+1. Really enlightening. –  Flavius Jan 13 '13 at 17:34

This is tagged and but doesn't depend on xetex, and you don't really need xparse for star forms which are reasonably well supported by latex. I changed \term to \textrm just so it ran.

Just define the nostar and star forms, then define your top level command to switch between them

\documentclass{article}

\newcommand{\englnostar}[2]{#1 (en. \textsl{#2})\index{en-ro}{#2}}

\newcommand{\englstar}[2]{#1 (en. \textsl{#2})\index{en-ro}{#2}\textrm{#1!#2}\textrm{#2!#1}}

\makeatletter
\newcommand\engl{\@ifstar\englstar\englnostar}
\makeatother

\begin{document}

\engl{X}{aaa}

\engl*{X}{aaa}

\end{document}

-
What if the two versions would have different number of parameters? –  Flavius Jan 13 '13 at 17:25
It would be against the latex syntax guidelines to make the number of arguments depend on a star (or an optional argument) no standard latex command does that. However there is no connection between the two comnands they can have any definition and argument structure. \engl itself just looks for a * (not the pther arguments) and then calls one or other command. –  David Carlisle Jan 13 '13 at 17:48