When writing a beamer presentation with LaTeX, I organize my presentation into sections and subsections. Frequently, the title of the first frame (slide) in a subsection has the same name as the subsection. Let’s say I start off with the following structure:
\section[corpora]{Accessing text corpora}
\subsection[gutenberg]{The Gutenberg Corpus}
\subsection[chat]{The web and chat Corpus}
\subsection[brown]{The Brown Corpus}
\subsection[reuters]{The Reuters Corpus}
\subsection[inaugural]{The Inaugural address Corpus}
\subsection[annotated]{Annotated corpora}
\subsection[foreign]{Corpora in other languages}
\subsection[DIY]{Loading your own corpora}
For each subsection, I want to put in one frame, with the name of the subsection being the name of the frame. Regular expressions to the rescue! In vim, all I have to is use V to select each line with subsection, then I hit :, which allows me to operate on those lines only. '<,'> is automatically inserted after the colon, which stands for “from the beginning of the highlighted section to the end of it”. Then I use s to perform my substitution. \r inserts a new line.
:'<,'>s/{\(.*\)}/{\1}\r\\begin{frame}\r\\frametitle
The result is:
\section[corpora]{Accessing text corpora}
\begin{frame}
\frametitle
\end{frame}
\subsection[gutenberg]{The Gutenberg Corpus}
\begin{frame}
\frametitle
\end{frame}
\subsection[chat]{The web and chat Corpus}
\begin{frame}
\frametitle
\end{frame}
\subsection[brown]{The Brown Corpus}
\begin{frame}
\frametitle
\end{frame}
\subsection[reuters]{The Reuters Corpus}
\begin{frame}
\frametitle
\end{frame}
\subsection[inaugural]{The Inaugural address Corpus}
\begin{frame}
\frametitle
\end{frame}
\subsection[annotated]{Annotated corpora}
\begin{frame}
\frametitle
\end{frame}
\subsection[foreign]{Corpora in other languages}
\begin{frame}
\frametitle
\end{frame}
\subsection[DIY]{Loading your own corpora}
\begin{frame}
\frametitle
\end{frame}

3 responses to “Vim regex-fu for LaTeX”
[…] !Vim regex-fu for !LaTeX: http://robfelty.com/2009/09/24/vim-regexp-latex/ […]
Instead of selecting lines, you can also do
:%g/\\subsection/s/{\(.*\)}/{\1}\r\\begin{frame}\r\\frametitle
The advantage is that it will work even if you have other text between the lines with \subsection.
[…] Petr Hlavacek. Another highlight of the week for me was taking a group of students to do an egg …robfelty :: Blog :: Archive :: Vim regex-fu for LaTeXJan Hlavacek wrote: Instead of selecting lines, you can also do […]