I would like to understand why defining new section heading would require function to have protection from expansion.
1 Answer
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.
-
Does this mean that even in the
L3 Layer
, the same breakage problem exists ? Would you discard theExpansion Protection
if you could ? That is, is the current use ofExpansion Protection
still intended as a solution to breakage problems, or would one actually need it as a useful feature in the Latex Language ?– VeakSep 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.– VeakSep 29 at 16:26 -
-
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
\section
is written into table of contents, which breaks things. Hardly anything that one can learn from. Then there is themoving in fragile
ridiculous speech.