4

I'm creating a presentation using beamer and listings - when I end a section with a {lstlisting} call, the next section title appears in the slide. Is there a way that I can specify when the next section should come in?

Code:

\documentclass{beamer}

\mode<presentation>
{
  \usetheme{CambridgeUS}
  \setbeamercovered{transparent}
}

\usepackage{listings}

\lstset{frame=tb,
  language=Java,
  aboveskip=3mm,
  belowskip=3mm,
  showstringspaces=false,
  columns=flexible,
  basicstyle={\fontsize{9}{9}\ttfamily},
  numbers=none,
  numberstyle=\tiny\color{gray},
  breaklines=true,
  breakatwhitespace=true,
  tabsize=3
}

\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage{times}
\usepackage[T1]{fontenc} 

\usepackage{amsmath}

\title[The presentation]{The Presentation}

\begin{document}

\section[section 1]{section 1}

\begin{frame}
    This is the first section
\end{frame}

%%%%% Starts using 'Section 2' %%%%%

Hey! This is the wrong section!

\begin{lstlisting}[frame=single]

   public void hi(){
    system.out.println("Hello World!"); 
   }

\end{lstlisting}

%%%%%% Section 2 declared here %%%%%%

\section[section 2]{section 2}

\begin{frame}
  I should be the first with the title Section 2!
\end{frame}

\end{document}

Slides: (see section in top middle) First Slide Second Slide Third Slide

3

The contents in your lstlisting environment need to be put within a frame environment.

MWE

\documentclass{beamer}

\mode<presentation>
{
  \usetheme{CambridgeUS}
  \setbeamercovered{transparent}
}

\usepackage{listings}

\lstset{frame=tb,
  language=Java,
  aboveskip=3mm,
  belowskip=3mm,
  showstringspaces=false,
  columns=flexible,
  basicstyle={\fontsize{9}{9}\ttfamily},
  numbers=none,
  numberstyle=\tiny\color{gray},
  breaklines=true,
  breakatwhitespace=true,
  tabsize=3
}

\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage{times}
\usepackage[T1]{fontenc} 

\usepackage{amsmath}

\title[The presentation]{The Presentation}

\begin{document}

\section[section 1]{section 1}

\begin{frame}
    This is the first section
\end{frame}

%%%%% Starts using 'Section 2' %%%%%
\begin{frame}[fragile]
Hey! This is the correct section!

\begin{lstlisting}[frame=single]

   public void hi(){
    system.out.println("Hello World!"); 
   }

\end{lstlisting}
\end{frame}
%%%%%% Section 2 declared here %%%%%%

\section[section 2]{section 2}

\begin{frame}
  I should be the first with the title Section 2!
\end{frame}

\end{document}

Output

enter image description here

  • I tried using frame before, but it wouldn't compile -- the [fragile] tag is necessary! – schil227 Jan 14 '16 at 0:19
  • @schil227: Yes, you're right. See this answer for an explanation. – Herr K. Jan 14 '16 at 7:27

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.