[Tex/LaTex] minted environment in frame causes error

beamerminted

There is an overleaf template for pythonTEX. I change it's document class into beamer and then I find a strange error with the following code:

\begin{frame}{Example}
\begin{minted}{latex}
\py{2+2}
\end{minted}
\end{frame}

error information:

Runaway argument?
 \py {2+2} \end {minted} 
Paragraph ended before \FV@BeginScanning was complete.
<to be read again> 
                   \par 
l.44 \end{frame}

A simple change can make it work well:

\begin{frame}[fragile]
\frametitle{Example}
\begin{minted}{latex}
\py{2+2}
\end{minted}
\end{frame}

Why can't I set the frame title directly?

Best Answer

minted is one of the many verbatim environments you will find when using source code in LaTeX. Those verbatim environments need special treatment (the fragile option) by design which is usually not needed for "simple things", e.g. itemize etc.

Hence it is also by design that you need to tell LaTeX to handle your frame specially by using the option. You do not need to set the frametitle using another command, \begin{frame}[fragile]{Title} will suffice.

Related Question