# Renew section command to wrap whole section in an environment

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

\section{Text 1}
Some text.


to

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


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.

EDIT

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}
else:
return \end{frame}\begin{frame}[fragile}{\$title}

finally:
return \end{frame}


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

-
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

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:

\documentclass{beamer}

\makeatletter

\begingroup
\toks@\expandafter\expandafter\expandafter{\expandafter#1#2}%
\xdef#1{\the\toks@}%
\endgroup}

\let\sec@this=\@empty
\def\sec@document{document}

\long\gdef\sec@line#1\par{%
\futurelet\sec@next\sec@linestart}%

\def\sec@linestart{%
\let\sec@do@next=\sec@line
\ifx\sec@next\section
\let\sec@do@next=\sec@new
\else
\ifx\sec@next\end
\let\sec@do@next=\sec@test@end
\fi
\fi
\sec@do@next}

\def\sec@new{%
\def\sec@frame{}%
\ifx\sec@this\@empty
\else
\fi
\let\sec@title=\@empty
\let\sec@this=\@empty
\sec@frame
}

\def\sec@test@end\end#1{%
\def\sec@test{#1}%
\ifx\sec@test\sec@document
\message{got here}%
\let\sec@do@next=\sec@new
\else
\let\sec@do@next=\sec@line
\fi
\sec@do@next\end{#1}}

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

\makeatother

\usepackage{lipsum}

\begin{document}

\section{Introduction}

\lipsum[1]

\section{Main Part}

\lipsum[1]

\section{Main Theorem}

\begin{theorem}[Euclid]
There is no largest prime.
\end{theorem}

\begin{proof}
We start by noting that a number is prime if and only if no prime less than it divides it.

\begin{enumerate}[<+->]
\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.
\end{enumerate}
\end{proof}

\section{Conclusion}

\lipsum[1]

\end{document}


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.

-
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