9

All documentations I've read about starred/non starred versions of commands recommend more or less the following way of doing (following for instance the UK List of TeX FAQ):

\newcommand{\mycommand}{\@ifstar\mycommandStar\mycommandNoStar}
\newcommand{\mycommandStar}{%
  <few lines of code only for starred mycommand>
  <many lines of code common to starred/non starred mycommand>
}
\newcommand{\mycommandNoStar}{%
  <few lines of code only for non starred mycommand>
  <many lines of code common to starred/non starred mycommand>
}

For environments, the scheme is similar (following for instance this answer):

\newenvironment{myenvironment}{%
  <few lines of code only for non starred myenvironment>
  <many lines of code common to starred/non starred myenvironment>
}{%
  <few lines of code only for non starred myenvironment>
  <many lines of code common to starred/non starred myenvironment>
}
\newenvironment{myenvironment*}{%
  <few lines of code only for starred myenvironment>
  <many lines of code common to starred/non starred myenvironment>
}{%
  <few lines of code only for starred myenvironment>
  <many lines of code common to starred/non starred myenvironment>
}

But, most of the case, there are only slight differences between starred and non starred versions and this way of doing requires to copy all changes in the common code between the two versions, which is a pain of maintenance (particularly if the common code is long).

Is there a more efficient way of doing?

9

It mostly depends on what the commands should do. If \mycommand* differs from \mycommand just because some different code has to be executed at the start, the following approach should work:

\newcommand{\mycommand}{\@ifstar{\@tempswatrue\@mycommand}{\@tempswafalse\@mycommand}}
\newcommand{\@mycommand}{%
  \if@tempswa
    <few lines of code only for starred mycommand>
  \else
    <few lines of code only for non starred mycommand>
  \fi
  <many lines of code common to starred/non starred mycommand>
}

With xparse this becomes easier:

\usepackage{xparse}
\NewDocumentCommand{\mycommand}{s}
 {\IfBooleanTF{#1}
    {<few lines of code only for starred mycommand>}
    {<few lines of code only for non starred mycommand>}%
  <many lines of code common to starred/non starred mycommand>%
 }

For the environment form there are additional complications, because there's no \@ifstar available.

\newenvironment{myenvironment}{%
  <few lines of code only for non starred myenvironment>
  \@myenvironmentstart
}{%
  <few lines of code only for non starred myenvironment>
  \@myenvironmentfinish
}
\newenvironment{myenvironment*}{%
  <few lines of code only for starred myenvironment>
  \@myenvironmentstart
}{%
  <few lines of code only for starred myenvironment>
  \@myenvironmentfinish
}

\newcommand{\@myenvironmentstart}{%
  <many lines of code common to starred/non starred myenvironment>
}
\newcommand{\@myenvironmentfinish}{%
  <many lines of code common to starred/non starred myenvironment>
}

No real simplification is possible with xparse.

  • It's not the first time I'm advised to make use of xparse package. The LaTeX class I'm currently working on makes use of standard TeX/LaTeX2e tools (including etoolbox package): is there a drawback to mix xparse package with non LaTeX3 tools? – Denis Bitouzé Jul 13 '13 at 14:31
  • 1
    @DenisBitouzé The only drawback is that it requires a fairly up-to-date TeX distribution. – egreg Jul 13 '13 at 14:40
4

The xparse package, part of the LaTeX3 bundle, is very handy in this regard. For example:

\documentclass{article}
\usepackage{xparse}
\NewDocumentCommand{\foo}{s}{This is the \IfBooleanTF{#1}{starred }{}foo command.}
\begin{document}
\foo{}

\foo*{}
\end{document}

enter image description here

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.