Macro \foo
is defined with one argument. If it is called as \foo*{test}
, then the star becomes the argument (#1
); then \foo
is expanded and \@ifstar
is called. But the following token {
is not a star. The argument, the star, is then set as \textit{*}
. The processing of \foo
is finished and {test}
is interpreted as group with the word test
inside.
The example can be fixed the following way:
\documentclass{article}
\makeatletter
\newcommand*{\foo}{\@ifstar\textbf\textit}
\makeatother
\begin{document}
\foo{test}
\foo*{test}
\end{document}
Macro \foo
is defined without argument, thus that \@ifstar
can look for a following star. Then the code in the arguments for "with star" and "without star" of \@ifstar
read the following argument.
If the code in the argument of \@ifstar
is more complicate, then the more general answer of Mike helps.