josephwright / beamer

A LaTeX class for producing presentations and slides
Other
1.41k stars 142 forks source link

`\ExplSyntaxOn`/`\ExplSyntaxOff` don't work inside frames #804

Closed dbitouze closed 1 year ago

dbitouze commented 1 year ago

Context

beamer 2022/09/13 v3.68

Issue

In the document body of a beamer file, \ExplSyntaxOn/\ExplSyntaxOff:

Indeed, the following MCE:

\documentclass{beamer}
\begin{document}
% \begin{frame}
  \ExplSyntaxOn
  \tl_set:Nn \l_tmpa_tl { Foo }
  \tl_use:N  \l_tmpa_tl
  \ExplSyntaxOff
% \end{frame}
\end{document}

compiles like a charm except if the frame environment is uncommented, the error message being:

! Undefined control sequence.
\beamer@doifinframe ...ameslide}\ExplSyntaxOn \tl 
                                                  _set:Nn \l _tmpa_tl { Foo ...
samcarter commented 1 year ago

You can use the same approach as with using \makeatletter ... \makeatother in frames: define some wrapper macro outside of the frame environment and then use the wrapper inside the frame.

\documentclass{beamer}
\begin{document}

\ExplSyntaxOn
\newcommand{\foo}{
\tl_set:Nn \l_tmpa_tl { Foo }
\tl_use:N  \l_tmpa_tl
}
\ExplSyntaxOff
\begin{frame}
\foo
\end{frame}
\end{document}
dbitouze commented 1 year ago

I didn't notice the same issue arises with \makeatletter/\makeatother: good to know, thanks!

And, sorry, I should have said that I knew there are such workarounds but if many wrapper macros have to be defined, it becomes tedious.

What is so specific to the frame environment that is responsible of these issues?

samcarter commented 1 year ago

What is so specific to the frame environment that is responsible of these issues?

I don't know for the expl case, but for \makeatletter ... \makeatother the problem is catcodes, see https://tex.stackexchange.com/questions/161174/makeatletter-within-a-beamer-frame-how#comment368752_161174

davidcarlisle commented 1 year ago

don't know for the expl case, but for \makeatletter ... \makeatother the problem is catcodes,

expl3 is same just with_ and : rather than @

What is so specific to the frame environment

It is evaluated multiple times so grabbed as a macro argument so \verb fails as do all related catcode changes (sometimes fixable with [fragile] which grabs the body in a more catcode tolerant way).

Using \ExplSyntaxOn mid document is always somewhat suspicious abuse of the intended layering: expl3 is intended for low level code in which class/package layer commands can define document level functions. So @samcarter's solution should be prefered even though this works without error

\documentclass{beamer}
\begin{document}
 \begin{frame}[fragile]
  \ExplSyntaxOn
  \tl_set:Nn \l_tmpa_tl { Foo }
  \tl_use:N  \l_tmpa_tl
  \ExplSyntaxOff
 \end{frame}
\end{document}