TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to write a beamer template for pandoc. For that I need to convert

\section{Text 1}
Some text.


\begin{frame}[fragile]{Text 1}
Some text.

Is there a way to do this inside LaTeX, e.g. with a \renewcommand? I wouldn't mind multiple parses of the generated LaTeX file.

This question seems to look for a similar solution, but the question I am interested in doesn't really get answered.


A redefinition of \section like (pseudocode)

def \section{$title}:
    static has_preceeding_section = False

    has_preceeding_section = True

    if not has_preceeding_section:
        return \begin{frame}[fragile]{$title}
        return \end{frame}\begin{frame}[fragile}{$title}

        return \end{frame}

would be sufficient for my use case. How would one implement above pseudocode in LaTeX?

share|improve this question
I deleted my earlier attempt since it was not working for you, and posted a follow up question: undefined control sequence hywritebookmark upon patching section command in beamer – Peter Grill Sep 10 '11 at 15:19
How many levels of sectioning are you going to use? Should it just be the top-level \section command, or should \subsections also be split into frames? – Loop Space Sep 11 '11 at 20:12
@Andrew: I would only use sections to create frames, so one level would be sufficient. – Benjamin Bannier Sep 11 '11 at 21:20
up vote 2 down vote accepted

I have a sort of solution for you, but I'm not sure how robust it is. This may be okay in this particular circumstance as the LaTeX file is created from pandoc, so there are unlikely to be any "sneaky macros" in the output. In particular, I expect that things won't try to change the catcodes mid-document. Also, things like \section commands and the \end{document} command will be on their own lines.

The idea is that when we get a \section then we start gobbling lines. Each time we get a new line, we test the first token to see if it is either a \section command or a \end. If it is an \end, we test to see if it is an \end{document}. If either of those (eventual) tests is true, then we stop gobbling lines and put what we've gobbled so far in to a frame (with the section title as the frame title). If either of those is false, then we return to gobbling lines.

Note, we could probably deal with catcode stuff by then putting it in a fragile frame.

Here's the code, with an example document:








  \message{got here}%

\renewcommand{\section}[1]{\message{Called with #1}\def\sec@title{{#1}}\sec@line}






\section{Main Part}


\section{Main Theorem}

There is no largest prime.

We start by noting that a number is prime if and only if no prime less than it divides it.

\item Suppose that there were a largest prime.
\item Then the number of primes would be finite; label them (in order) as \(p_1, \dotsc, p_n\).
\item Consider \(q = p_1 \cdot p_2 \cdots p_n + 1\).
\item Then \(p_j \not| q\) so \(q\) is not divisible by any prime less than it.
\item Hence \(q\) is prime.
\item But \(q > p_n\) so \(q\) is not prime.




I put the theorem and proof in to show that it works with non-document ends. The lipsum package is to provide some dummy text for the other frames.

Note for anyone interested in technicalities: I looked at both the environ and beamer for ideas on how to gather in a large chunk of "stuff", but both work by gathering up to the next \end. We need to be able to look for both \end{document} and \section{Section Title}. I couldn't see any way to do that using the same mechanism so had to resort to gulping it line by line. If there's a better way, I'd love to know about it.

share|improve this answer
Thanks Andrew, that works very well. In the meantime I used something much simpler, along the lines of what @Peter initially suggested (define \section as \end{frame}\begin{frame}{%1}). This could work because I supplied a template to pandoc in which I opened an initial frame with the title slide and added the closing tag for the last frame just before \end{document}. – Benjamin Bannier Sep 13 '11 at 19:26
@honk: I must confess that I'm a bit surprised that that worked! Looking at beamer's frame-collecting stuff, then it really shouldn't have. I'd be curious to see a sample code. – Loop Space Sep 13 '11 at 19:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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