Can \ExplSyntaxOn and \ExplSyntaxOff be nested?
No, you can't. There's no stack implemented.
When \ExplSyntaxOn
is encountered, some characters have their category code are changed, but their previous category code is stored in the definition of \ExplSyntaxOff
(that does nothing, by default).
When \ExplSyntaxOff
is next encountered, it resets the category codes to the previous states and redefines itself to do nothing.
Here's an example:
\documentclass{article}
\usepackage{expl3}
\ExplSyntaxOn
\tl_show:N \ExplSyntaxOff
\ExplSyntaxOn
\tl_show:N \ExplSyntaxOff
\ExplSyntaxOff
\tl_show:N \ExplSyntaxOff
\ExplSyntaxOff
\stop
And here's the output on the terminal: you see that the expl3
codes are not valid any longer after the first \ExplSyntaxOff
and \tl_show:N
raises an error.
> \ExplSyntaxOff=\char_set_catcode:nn {9}{10}\char_set_catcode:nn
{32}{10}\char_set_catcode:nn {34}{12}\char_set_catcode:nn
{38}{4}\char_set_catcode:nn {58}{12}\char_set_catcode:nn
{94}{7}\char_set_catcode:nn {95}{8}\char_set_catcode:nn
{124}{12}\char_set_catcode:nn {126}{13}\tex_endlinechar:D =13\scan_stop:
\bool_set_false:N \l__kernel_expl_bool \cs_set_protected:Npn \ExplSyntaxOff
{}.
<recently read> }
l.5 \tl_show:N \ExplSyntaxOff
?
> \ExplSyntaxOff=\char_set_catcode:nn {9}{10}\char_set_catcode:nn
{32}{10}\char_set_catcode:nn {34}{12}\char_set_catcode:nn
{38}{4}\char_set_catcode:nn {58}{12}\char_set_catcode:nn
{94}{7}\char_set_catcode:nn {95}{8}\char_set_catcode:nn
{124}{12}\char_set_catcode:nn {126}{13}\tex_endlinechar:D =13\scan_stop:
\bool_set_false:N \l__kernel_expl_bool \cs_set_protected:Npn \ExplSyntaxOff
{}.
<recently read> }
l.8 \tl_show:N \ExplSyntaxOff
?
! Undefined control sequence.
l.11 \tl
_show:N \ExplSyntaxOff
?
\ExplSyntaxOn
and \ExplSyntaxOff
are switches that change certain category codes to allow for a different coding syntax. They're synonymous to the \makeatletter
...\makeatother
pairs used to allow @
's in macros.
A showcase of something similar using font switches may be conclusive:
\documentclass{article}
\begin{document}
\bfseries
First % ... lots of stuff 1 ...
\bfseries
Second % ... lots of stuff 2 ...
\mdseries
Third % ... lots of stuff 3 ...
\mdseries
Last % ... lots of stuff last ...
\end{document}
One could consider \mdseries
reverting what \bfseries
has done, but that doesn't imply they are nestable.
Switches are not the same as grouping, which provides a scope as well as nesting.