LaTeX commands commonly have “versions” defined with an asterisk
tagged onto their name: for example \
newcommand
and
\
newcommand*
(the former defines a \
long
version of the
command).
The simple-minded way for a user to write such a command involves use of the ifthen package:
\newcommand{\mycommand}[1]{\ifthenelse{\equal{#1}{*}}% {\mycommandStar}% {\mycommandNoStar{#1}}% } \newcommand{\mycommandStar}{starred version} \newcommand{\mycommandNoStar}[1]{normal version}
This does the trick, for sufficiently simple commands, but it has
various tiresome failure modes, and it requires \
mycommandnostar
to take an argument.
Of course, the LaTeX kernel has something slicker than this:
\newcommand{\mycommand}{\@ifstar \mycommandStar% \mycommandNoStar% } \newcommand{\mycommandStar}[2]{starred version} \newcommand{\mycommandNoStar}[1]{normal version}
(Note that arguments to \
mycommandStar
and \
mycommandNoStar
are independent — either can have their own arguments, unconstrained
by the technique we’re using, unlike the trick described above.)
The \
@ifstar
trick is all very well, is fast and efficient, but
it requires the definition to be
\
makeatletter
protected.
A pleasing alternative is the suffix package. This elegant piece of code allows you to define variants of your commands:
\newcommand\mycommand{normal version} \WithSuffix\newcommand\mycommand*{starred version}
The package needs e-LaTeX, but any new enough
distribution defines LaTeX as e-LaTeX by default. Command
arguments may be specified in the normal way, in both command
definitions (after the “*
” in the \
WithSuffix
version). You can also use the TeX primitive commands, creating a
definition like:
\WithSuffix\gdef\mycommand*{starred version}
This question on the Web: http://www.tex.ac.uk/cgi-bin/texfaq2html?label=cmdstar