Is there a way to specify fragile mode as default for all frames? Now I know how to define a fragile frame: \begin{frame}[fragile]
.
[Tex/LaTex] beamer’s fragile frame as default
beamerverbatim
Related Solutions
In my opinion, the cleanest approach here is to make \AtBeginSection
work in a beamerarticle
. The reason why it doesn't is that the \AtBeginSection
macro works by defining hooks to be executed at the beginning of theses sections. In article mode, however, the \section
macro as defined by the document class is used, so these hooks are never executed.
To overcome this, a redefinition of the \section
macro is necessary. This is a bit complicated because it calls \@startsection
internally, so simply adding the hook to the end of the macro won't work. Instead, a complete "wrapper" around the original command is necessary:
\mode<article>
\usepackage{xparse}
\makeatletter
\expandafter\let\expandafter\originalsection\expandafter=\csname @orig\string\section\endcsname
\RenewDocumentCommand{\section}{ D<>{} s o m }
{
\alt<#1>
{
\IfBooleanTF {#2}
{
\originalsection*{#4}
%\beamer@atbeginsections % breaks because section* is also used e.g. in the TOC
}
{
\IfNoValueTF {#3} { \originalsection{#4} } { \originalsection[#3]{#4} }
\beamer@atbeginsection
}
}
{
\beamer@secgobble
}
}
\makeatother
\mode<all>
Insert this code into the preamble of your document, after having loaded beamerarticle
. What it does is to save the original, beamer
-free meaning of the \section
macro stored in the macro with the "weird" name @orig\section
to \originalsection
. Afterwards, \section
is redefined to execute the original sectioning command and the hook \beamer@atbeginsection
afterwards. As \section
has a starred and non-starred version, an optional argument and an (optional) overlay specification, I used xparse
which allows you to define such macros in a very convenient way.
Now you can use \AtBeginSection
as usual to insert the section start frames. As you probably don't want them to actually show up in article mode, you should use \begin{frame}<article:0>
for these (the frame number is still incremented). Additionally, you need to \def\insertsection{}
because this command is unknown to beamerarticle
and would result in an error otherwise.
Note that using the optional argument of \AtBeginSection
to define a hook for starred sections won't work (the line executing it is commented out in the above code). The reason is that \section*
is also used for the table of contents etc., where such inserts would result in an error message.
Output of your MWE using the above code
In presentation mode (click on the image to see it full-size):
In article mode:
For completeness, the full source that produced the above results can be found on pastebin.com.
Edit: The xparse
package I used to redefine \section
relies on the experimental LaTeX3 kernel. If it is not available, you can use this plain LaTeX2e solution instead:
\mode<article>
\makeatletter
\expandafter\let\expandafter\originalsection\expandafter=\csname @orig\string\section\endcsname
\def\sectionwithhook{\@ifstar\@sectionwithhook\@@sectionwithhook}
\newcommand{\@sectionwithhook}[1]{\originalsection*{#1}}
\newcommand{\@@sectionwithhook}[2][]{\beamer@ifempty{#1}{\originalsection{#2}}{\originalsection[#1]{#2}}\beamer@atbeginsection}
\renewcommand<>{\section}{\alt#1{\sectionwithhook}{\beamer@secgobble}}
\makeatother
\mode<all>
This replaces the whole code given above, achieving the same as the \RenewDocumentCommand
with some helper macros.
there are no important drawbacks. It is also possible to define a new environment with the fragile
option:
\documentclass{beamer}
\newenvironment{Foo}[1]
{\begin{frame}[environment=Foo]{#1}}
{\end{frame}}
\newenvironment{FooBar}[1]
{\begin{frame}[fragile,environment=FooBar]{#1}}
{\end{frame}}
\begin{document}
\begin{Foo}{title}
\end{Foo}
\begin{FooBar}{title}
\end{FooBar}
\end{document}
With fragile every contents is written into an external file and read back. Not a big deal but makes compiling slow. However, fragile
is needed for verbatim material.
Best Answer
You could define your own frame environment like it is explained in the user manual p.62, e.g.:
The
environment
option does the trick. It tellsbeamer
that the frame ends with an\end{xframe}
now.Note that the
frame
has a complex set of optional arguments, including two< >
and to optional(!) braced arguments for the frame title and sub-title. Not all of these arguments might be supported by the above code. You might write it better as:which should support both optional title and sub-title arguments. You also can define the frame title using an explicit
\frametitle
command.Another, more complex alternative is to use the
xparse
package to definexframe
with the same arguments offrame
and pass it along.