3

This question already has an answer here:

I want to show LaTeX code inside a Beamer presentation.

If I try the following MWE as an article, it works flawlessly:

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\lstset
{
    language=[LaTeX]TeX,
    breaklines=true,
    basicstyle=\tt\scriptsize,
    keywordstyle=\color{blue},
    identifierstyle=\color{magenta},
}

\begin{document}
\begin{lstlisting}
     \documentclass{article}
     \begin{document}
     Hello World
     \end{document}
\end{lstlisting}
\end{document}

However, the moment I want to use that code into a Beamer presentation like in the MWE below:

\documentclass{beamer}
\usepackage{lmodern}
\usepackage{listings}
\lstset
{
    language=[LaTeX]TeX,
    breaklines=true,
    basicstyle=\tt\scriptsize,
    keywordstyle=\color{blue},
    identifierstyle=\color{magenta},
}

\title{A Tiny Example}
\author{John Doe}
\date{\today}
\begin{document}
\maketitle
\begin{frame}
\frametitle{First Slide}
\begin{lstlisting}
    \documentclass{article}
    \begin{document}
    Hello World
    \end{document}
\end{lstlisting}
\end{frame}
\end{document}

I get the following errors:

 ! Missing \endcsname inserted.

 ! LaTeX Error: Can be used only in preamble.

marked as duplicate by jub0bs, darthbith, Andrew Swann, egreg, LaRiFaRi Jun 15 '15 at 13:20

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

5

use the optional argument fragile:

\begin{frame}[fragile]%%%%%%%%%%%%
\frametitle{First Slide}
\begin{lstlisting}
    \documentclass{article}
    \begin{document}
    Hello World
    \end{document}
\end{lstlisting}
\end{frame}

Not the answer you're looking for? Browse other questions tagged or ask your own question.