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.

4
  • 3
    Have you tried \NewDocumentCommand or \NewExpandableDocumentCommand and friends with an s-type (starred) argument? Commented Feb 13, 2024 at 13:13
  • Thanks for your hint. No, actually not. Haven't even been aware of these. Are these (La)TeX primitives and working with LuaTeX as well? Commented Feb 13, 2024 at 13:15
  • 3
    These are part of newer LaTeX kernels. See usrguide.pdf current version for more information. Commented Feb 13, 2024 at 13:17
  • Ah, cheers. Cool! Commented Feb 13, 2024 at 13:18

1 Answer 1

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.

1
  • Thx, @egreg, works smoothly. The usrguide helps me to revise my "old" LaTeX practices. Commented Feb 13, 2024 at 22:11

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.