0

This very helpful answer led me to define my own starred commands. However, in order to use my commands in, for example, headings, after a round of strange almost frightening thoughts, I managed to arrive at the following (MWE):

\documentclass{article}
\makeatletter
\def\testone{\@ifstar\@dothis\@@dothat}
\def\testtwo{\protect\@ifstar\protect\@dothis\protect\@@dothat}
\def\@dothis#1{#1}
\def\@@dothat#1{#1}
\makeatother
\begin{document}
\section{\testone{abc}} % does not work
\testone{abc} % works
\section{\testtwo{abc}} % works
\testtwo{abc} % works
\end{document}

I am still struggling to percolate TeX's expansion mechanisms and, hence, would be thankful for an answer about why my definition \test... requires these \protects to work in headings? Thanks.

mfg
  • 499

1 Answers1

2

\protect is wrong in those places.

If you want to make \testtwo robust, you need to use

\newcommand{\testtwo}{}% for safety against redefining
\DeclareRobustCommand{\testtwo}{\@ifstar\@dothis\@@dothat}

But it's simpler to use \NewDocumentCommand:

\NewDocumentCommand{\test}{sm}{%
  \IfBooleanTF{#1}{% there is a *
    called #1 with *%
  }{% no star
    called #1 without *%
  }%
}

See texdoc usrguide for more information about \NewDocumentCommand.

egreg
  • 1,121,712
  • Thx, @egreg, works smoothly. The usrguide helps me to revise my "old" LaTeX practices. – mfg Feb 13 '24 at 22:11