[Tex/LaTex] Using beamer frame title in document text

beamerbeamerarticle

I use beamer to create a set of slides that are used to present the contents of a document. Both the slides and the printable document contents are contained in a single set of source files; I use the \mode commands to switch between presentation and printout mode. In contrast to some of the beamer examples I have seen so far, the printable version does not contain the complete slides as images. The slides and the printable version share some graphics and other contents, and I use a \marginpar to create a kind of link between the slides and the printable version, just to let the audience keep track. Here is a MWE, consisting of three files:

% === printable.tex =======================================
\documentclass{scrartcl}
\usepackage{beamerarticle}

\begin{document}
\input{contents}
\end{document}
% === end of printable.tex ================================

% === slides.tex ==========================================
\documentclass{beamer}

\begin{document}
\input{contents}
\end{document}
% === end of slides.tex ===================================

% === contents.tex =======================================
\mode*

Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
Fusce laoreet dui vestibulum tortor luctus porttitor. 
Aliquam dictum ipsum condimentum, auctor augue in, 
consectetur lacus. Morbi auctor posuere scelerisque.
Aenean vel enim sit met orci rutrum fermentum faucibus et 
mauris. Pellentesque tempor felis in imperdiet accumsan.
Mauris auctor pretium nulla et tincidunt.

\mode<presentation>
\begin{frame}
  \frametitle{Foo Bar}
  \begin{itemize}
    \item Foo
    \item Bar
    \item Baz
  \end{itemize}
\end{frame}
\mode*

Mauris\marginpar{Foo Bar} malesuada nisl sit amet nisl 
eleifend tincidunt. Sed sed libero scelerisque, ultrices 
elit a, lacinia mi. Mauris nec semper metus, eget 
pellentesque leo. Donec volutpat aliquam aliquam. Nunc 
vel neque scelerisque, aliquam augue eget, gravida felis. 
Suspendisse vel molestie libero. 

% === end of contents.tex =================================

As you can see, I have to manually repeat the frame title Foo Bar in the \marginpar. I tend to forget to do that, and I especially tend to forget to update the repetition when the title changes. Is there a way to specify the title only once and use it for the \frametitle as well for the \marginpar?

Best Answer

You just need to define different templates for article and presentation modes. If you include

\setbeamertemplate{frametitle}{\marginnote{\insertframetitle}}

in printable.tex preamble then frame title will appear on the margin if you are also processing frames in article version. (\marginnote is defined in marginnote package)

In your example, all frame contents is skipped because it is preceded by \mode<presentation>. Instead you can use something like:

\begin{frame}
  \frametitle{Foo Bar}
  \only<beamer>{%
     \begin{itemize}
       \item Foo
       \item Bar
       \item Baz
  \end{itemize}%
  } %<- \only<beamer> closing
\end{frame}

This way, only frame contents will be skipped while frametitle is processed and printed as a margin note. You don't need to include any \marginpar{Foo Bar} into next paragraph.

Please, look at Changing \mode in beamer makes a new paragraph. Is it possible to avoid it? and How to better adjust placement of frametitle converted to marginpar in beamerarticle? and you will see why I prefer to use \only and \marginnote.

Your complete example adapted with previous solution is

% === printable.tex =======================================
\documentclass{scrartcl}
\usepackage{beamerarticle}
\usepackage{marginnote}

\setbeamertemplate{frametitle}{\marginnote{\insertframetitle}}

\begin{document}
\input{contents}
\end{document}
% === end of printable.tex ================================
% 
% === slides.tex ==========================================
\documentclass{beamer}

\begin{document}
\input{contents}
\end{document}
% === end of slides.tex ===================================
% 
% === contents.tex =======================================

\mode*
Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
Fusce laoreet dui vestibulum tortor luctus porttitor. 
Aliquam dictum ipsum condimentum, auctor augue in, 
consectetur lacus. Morbi auctor posuere scelerisque.
Aenean vel enim sit met orci rutrum fermentum faucibus et 
mauris. Pellentesque tempor felis in imperdiet accumsan.
Mauris auctor pretium nulla et tincidunt.

\begin{frame}
  \frametitle{Foo Bar}
\only<beamer>{\begin{itemize}
    \item Foo
    \item Bar
    \item Baz
  \end{itemize}}
\end{frame}

\mode*
Mauris malesuada nisl sit amet nisl 
eleifend tincidunt. Sed sed libero scelerisque, ultrices 
elit a, lacinia mi. Mauris nec semper metus, eget 
pellentesque leo. Donec volutpat aliquam aliquam. Nunc 
vel neque scelerisque, aliquam augue eget, gravida felis. 
Suspendisse vel molestie libero. 

% === end of contents.tex =================================

Note: A very nice example to understand how to work with beamerarticle to produce presentations and printed version from same sources is a-lecture. It's written by Till Tantau and is part of beamer distribution, so probably it's in your computer. Take a look at it!

Addition by vwegert:

With the help of the package environ, I managed to define an environment that -- in contrast to the open \only<beamer>{ command -- does not wreak havoc with the syntax highlighting, pretty printer and other tools:

% === printable.tex =======================================
\documentclass{scrartcl}
\usepackage{beamerarticle}
\input{preamble}
\setbeamertemplate{frametitle}{\marginpar{\insertframetitle}}

\begin{document}
\input{contents}
\end{document}
% === end of printable.tex ================================

% === slides.tex ==========================================
\documentclass{beamer}
\input{preamble}

\begin{document}
\input{contents}
\end{document}
% === end of slides.tex ===================================

% === preamble.tex =======================================
\usepackage{environ}
\NewEnviron{onlybeamer}{\only<beamer>{\BODY}}
% === end of preamble.tex =================================

% === contents.tex =======================================
\mode*

Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
Fusce laoreet dui vestibulum tortor luctus porttitor. 
Aliquam dictum ipsum condimentum, auctor augue in, 
consectetur lacus. Morbi auctor posuere scelerisque.
Aenean vel enim sit met orci rutrum fermentum faucibus et 
mauris. Pellentesque tempor felis in imperdiet accumsan.
Mauris auctor pretium nulla et tincidunt.

\begin{frame}
  \frametitle{Foo Bar}
  \begin{onlybeamer}
    \begin{itemize}
      \item Foo
      \item Bar
      \item Baz
    \end{itemize}
  \end{onlybeamer}
\end{frame}
\mode*

Mauris malesuada nisl sit amet nisl 
eleifend tincidunt. Sed sed libero scelerisque, ultrices 
elit a, lacinia mi. Mauris nec semper metus, eget 
pellentesque leo. Donec volutpat aliquam aliquam. Nunc 
vel neque scelerisque, aliquam augue eget, gravida felis. 
Suspendisse vel molestie libero. 

\mode<all>
% === end of contents.tex =================================