5

I want to display a short latex code in my beamer. The code is also the about beamer. And I don't want to use the \lstinputlisting{file.tex}. So is there any way to do that? Here is the code without error. Anyway, when I the three disable lines are enable, error occur.

\documentclass{beamer}
\usetheme{Madrid}
\usepackage{listings}
\lstset{
    language=[latex]tex,
    breaklines=true}
\begin{document}
    \begin{frame}[fragile]
        \begin{lstlisting}
            \documentclass{beamer}
            \begin{document}
%               \begin{frame}
%                   Test
%               \end{frame}
            \end{document}
        \end{lstlisting}
    \end{frame}
\end{document}
10
  • See this thread: tex.stackexchange.com/q/7083/27635. Probably Daniel's answer is what you are looking for. Sep 4 '13 at 15:08
  • When I try the code you suggested, it produce nothing beside the title that say "Recursive Algorithm"
    – Say OL
    Sep 4 '13 at 15:22
  • I didn't mean to use that code, I mean that probably using boxes is the only alternative way to \lstinputlisting Sep 4 '13 at 15:25
  • @karlkoeller What did you mean? Create a box inside the frame? Create the listing inside box and inside the frame?
    – Say OL
    Sep 4 '13 at 15:32
  • Do you want an answer? Sep 4 '13 at 15:42
2

One way to avoid using \lstinputlisting is to "box" the lstlisting environment and then use the command \usebox inside the frame.

MWE

\documentclass{beamer}
\usetheme{Madrid}
\usepackage{listings}
\lstset{%
    language=[latex]tex,
    breaklines=true}

\newsavebox{\myLst}

\begin{document}

\begin{lrbox}{\myLst}
\begin{lstlisting}
    \documentclass{beamer}
    \begin{document}
%     \begin{frame}
%       Test
%     \end{frame}
    \end{document}
\end{lstlisting}
\end{lrbox}

\begin{frame}
\usebox{\myLst}
\end{frame}

\end{document} 

Result

enter image description here

4

The frame environment must not be indented for such kind of frames. The »beamer« manual says something about that on page 60:

To determine the end of the frame, the following rule is used: The first occurence of a single line containing exactly \end{⟨frame environment name⟩} ends the frame. The ⟨environment name⟩ is normally frame, but it can be changed using the environment option. This special rule is needed since the frame contents is, after all, not interpreted when it is gathered.

\documentclass{beamer}
\usetheme{Madrid}
\usepackage{listings}
\lstset{
  language=[latex]tex,
  breaklines=true
}

\begin{document}

\begin{frame}[fragile]
  \begin{lstlisting}
    \documentclass{beamer}
      \begin{document}
%        \begin{frame}
%          Test
%        \end{frame}
      \end{document}
  \end{lstlisting}
\end{frame}

\end{document}
16
  • "The frame environment must not be indented for such kind of frames." Why is that please?
    – Chris H
    Sep 4 '13 at 15:14
  • @ChrisH: I don't know the reason. Just read about that somewhere. But I don't remember where. Sep 4 '13 at 15:15
  • True. However, a \end{frame} inside the lstlisting environment must be indented. Sep 4 '13 at 15:15
  • @ChrisH please refer to page 60 of the beamer manual for an explanation on how material is processed when the fragile option is used. In fact, you (Thorsten), could add some of that explanation to your answer if you like :-) Sep 4 '13 at 15:18
  • The three disable line \begin{frame} Test \end{frame} are the code I want to display on my beamer screen. The indent is just make me easy to see and find common error only.
    – Say OL
    Sep 4 '13 at 15:20

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.