42

I want to put some PHP code in my slide with the package listings but I have this error message:

Argument of \frame has an extra }.<inserted text>\par }

My latex code :

\documentclass{beamer}

\usepackage[utf8]{inputenc} 
\usepackage[T1]{fontenc}
\usepackage[frenchb]{babel}
\usepackage{setspace}
\usepackage{color}
\usepackage{listings}

\usetheme{CambridgeUS}


\begin{frame}
\begin{large}
    Contexte : \newline \pause
\end{large}

\begin{itemize}
    \item Avertir Drupal
\end{itemize}
\begin{lstlisting}

// Gestion du contexte ete2013
// Utilisation d'un nouveau template
if (%variables['ctpage'] == "ete2013") {
    variables['template_files']=array('page-ete');
}
\end{lstlisting}
\end{frame}

migrated from stackoverflow.com Aug 26 '13 at 1:02

This question came from our site for professional and enthusiast programmers.

  • 4
    Note that your LaTeX code is not compilable, as it is missing \begin{document} and \end{document}. Whenever a frame constains a listing, you should start the frame environment by \begin{frame}[fragile]; that does the trick, here. A suggestion somewhat peripheral to the problem: pass the key=value pair language=PHP to your lstlisting environement to get syntax highlighting of your code. – jub0bs Aug 26 '13 at 1:23
  • 1
    You can also spend a little bit time seeing a precise manual from en.wikibooks.org/wiki/LaTeX/Source_Code_Listings – Say OL Aug 26 '13 at 5:56
55

Your example has two problems:

  • No \begin/end{document}
  • Missing fragile in \begin{frame} (explanation see below)

This example works:

\documentclass{beamer}

\usepackage[utf8]{inputenc} 
\usepackage[T1]{fontenc}
\usepackage[frenchb]{babel}
\usepackage{setspace}
\usepackage{color}
\usepackage{listings}

\usetheme{CambridgeUS}
\begin{document}

\begin{frame}[fragile]
\begin{large}
    Contexte : \newline \pause
\end{large}

\begin{itemize}
    \item Avertir Drupal
\end{itemize}
\begin{lstlisting}

// Gestion du contexte ete2013
// Utilisation d'un nouveau template
if (%variables['ctpage'] == "ete2013") {
    variables['template_files']=array('page-ete');
}
\end{lstlisting}
\end{frame}
\end{document}

Explanation: beamer-frames can't handle verbatim environment (and listings is a kind of verbatim). If a frame contains a verbatim (or listings)-environment, the frame must contain the option fragile. You can find some details in a blog article.

  • 5
    (To clarify) The reason for the error is that without the fragile option then the contents of the page are "frozen" when it is read in. But lstlisting needs to do some technical stuff to ensure that TeX's special characters are interpreted correctly inside the environment. In the "frozen" state this doesn't work and TeX's special characters are still special. So the % character is interpreted as a comment character and { .. } as grouping. But the % means that TeX doesn't see the { so it is left with an extra } which it can't match, hence the specific error. – Loop Space Aug 26 '13 at 7:52
  • Thanks for your answers ! But when i use the option [fragile], i have this error message : ! File ended while scanning use of \next.<inserted text>\par<> slides.texI suspect you have forgotten a }', causing meto read past where you wanted me to stop.I'll try to recover; but if the error is serious,you'd better type E' or `X' now and fix your file.! Emergency stop.<> slides.tex (job aborted, no legal \end found)Here is how much of TeX's memory you used:18268 strings out of 493921334380 string characters out of 3144864670976 words of memory out of [...] – Juanwolf Aug 26 '13 at 19:14
  • @JuanWolf Can you provide a full MWE, including \begin{document}.... Or can you try my example. With TeXLive 2013 it works correct. – knut Aug 26 '13 at 19:58
  • Yes, your example works ! – Juanwolf Aug 26 '13 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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