Defining starred versions of commands (* macro)
See this entry in the UK TeX FAQ.
The "elegant" way is to use the suffix
package (which requires eTeX):
\documentclass{article}
\usepackage{suffix}
\newcommand\foo{blah}
\WithSuffix\newcommand\foo*{blahblah}
\begin{document}
\foo
\foo*
\end{document}
If you look at source2e you might see a lot of lines that look like
\def\foo{\@ifstar\@foo\@@foo}
\def\@foo#1{...}
\def\@@foo#1{...}
This makes \foo
a one-argument command that has regular and starred versions. The starred version is the expansion of \@foo
while the nonstarred version is that of \@@foo
. Using the @ sign in the auxiliary macros is a TeX convention which some authors embrace and some avoid.
There are higher-level ways to do it (as lockstep points out) but once you learn this pattern it's not too hard to use. Just make sure it's between \makeatletter...\makeatother
or in a .sty
file.
Edits removed some inaccuracies and editorializing.
LaTeX3 solution:
\documentclass{article}
\usepackage{xparse}
\NewDocumentCommand\foo{s}{%
\IfBooleanTF#1%
{blahblah}% If a star is seen
{blah}% If no star is seen
}