0

I would like to understand why defining new section heading would require function to have protection from expansion.

8
  • as has already been explained protection makes things safe in a write, and section headings get written to the toc file for table of contents, "fragile commands in moving arguments" should be in any basic latex tutorial. Sep 29 at 14:42
  • Can you direct me on the basic latex tutorial. I would be glad to see an example for section heading where expansion protection would be required. Because I have not seen much in terms of example code.
    – Veak
    Sep 29 at 14:51
  • see tex.stackexchange.com/… Sep 29 at 14:59
  • Basically I get that stuff in \section is written into table of contents, which breaks things. Hardly anything that one can learn from. Then there is the moving in fragile ridiculous speech.
    – Veak
    Sep 29 at 15:19
  • 1
    it is not "ridiculous speech" they are specific terms defined in the latex book. A "fragile command" is one that is not protected and a "moving argument" is an argument where such commands fail. If you use a fragile command in a moving argument then expansion goes wrong and you get weird internal (more or less spurious) error messages as tex can't really recover in any clean way. Sep 29 at 15:39

1 Answer 1

1

It used to be the most Frequent of frequently asked question as almost all latex commands were fragile and failed by default in headings, but these days most are robust by default (as more memory allows that). So an example showing a command with a * form defined the classical way, so it is fragile:

\documentclass{article}

\makeatletter
\def\foo{\@ifstar{star}{not star}}
%\protected\def\foo{\@ifstar{star}{not star}}
\makeatother

\begin{document}

\tableofcontents

1 \foo

2 \foo*

\section{aaa \foo*  bbb}
xxx

\end{document}

produces the error

! Argument of \@sect has an extra }.
<inserted text> 
                \par 
l.16 \section{aaa \foo*  bbb}
                             
? 

as \@ifstar lookahead fails in the \write needed for the table of contents, but if you switch the definition to \protected\def then the \write is safe and the aux file has

\@writefile{toc}{\contentsline {section}{\numberline {1}aaa \foo * bbb}{1}{}\pro
tected@file@percent }

where \foo did not expand at all, but just wrote as itself.

6
  • Does this mean that even in the L3 Layer, the same breakage problem exists ? Would you discard the Expansion Protection if you could ? That is, is the current use of Expansion Protection still intended as a solution to breakage problems, or would one actually need it as a useful feature in the Latex Language ?
    – Veak
    Sep 29 at 16:19
  • @Fluffy yes basically when defining expl3 functions they should always be expandable (no internal assignments and no x arguments) or protected (declared with one of the _protected_ declarations that uses \protected\def then all functions are basically safe to use in any argument. But if you use the non protected definitions then you will hit the above issue. Sep 29 at 16:21
  • The conclusion is then that \cs_new_protected:Nn is there to solve specific problems rather than being a language feature.
    – Veak
    Sep 29 at 16:26
  • @Fluffy that isn't the conclusion I would make. Sep 29 at 16:27
  • 1
    \cs_new_protected_ is there to expose the e-tex primitive \protected\def feature. expl3 in general is written in e-tex and is designed to make all its features available, but with a more consistent syntax. @Fluffy Sep 29 at 16:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .