Tx · LaTeX & TikZ

How to Define a Custom Beamer Frame Command

Published By the 9to5Science editorial team

At a glance

  1. 1. Keep the frame end

    Define a command that starts the frame, but close it with the literal \end{frame}.

  2. 2. Pass title and options

    Use command arguments for the title and, if useful, frame options such as fragile.

  3. 3. Build in either mode

    Use beamer for slides or article with beamerarticle for an article-style version.

If you want a shorthand for starting frames, define a command that expands to \begin{frame} and its title. End each frame with the literal \end{frame}. This works with the Beamer class and with article plus the beamerarticle package.

Beamer has special handling for frame contents, so a custom environment that wraps both the beginning and end of a frame can fail: Beamer looks for the literal \end{frame} while collecting the frame body. A start command avoids replacing that end marker.

Define a frame-start command

Here is a minimal example. The first two lines select presentation mode. To make an article version, replace them with the alternative lines shown in the comments.

\documentclass{beamer}
% For article mode, use these two lines instead:
% \documentclass{article}
% \usepackage{beamerarticle}

\newcommand{\startframe}[2][]{%
  \begin{frame}[#1]{#2}%
}

\begin{document}

\startframe{A short example}
This content appears inside the frame.
\end{frame}

\startframe[fragile]{A frame with verbatim content}
\begin{verbatim}
Some verbatim text
\end{verbatim}
\end{frame}

\end{document}

The command takes two arguments. The optional first argument is the frame option list, and the required second argument is the frame title. For example, \startframe[fragile]{Code} begins a fragile frame. If you do not need frame options, a simpler definition is enough:

\newcommand{\startframe}[1]{\begin{frame}{#1}}

With that definition, write \startframe{Title}, put the frame contents next, and finish with \end{frame}.

Using beamerarticle

For an article-style build, use \documentclass{article} and load \usepackage{beamerarticle} instead of using the Beamer class. Keep the same frame-start command and frame source. The package provides article-mode behavior for Beamer commands, so you can maintain a shared source for slides and an article-style output.

The example uses \startframe only as a command name; you can choose another name that does not conflict with an existing command. Do not load beamerarticle in a document whose class is beamer: choose one document class for each build.

Why not define a wrapper environment?

A tempting alternative is to define a custom environment whose begin code runs \begin{frame} and whose end code runs \end{frame}. Beamer’s frame environment processes its body specially and searches for the frame-ending marker. Hiding that marker behind an outer environment can interfere with this processing, particularly when the frame has special options such as fragile.

A start command is intentionally asymmetric: it abbreviates the start but leaves Beamer’s expected closing marker in the document. If you need to standardize more than the title—for example, a recurring option—put that option in the command’s definition and still close the frame with \end{frame}.

Common mistakes

  • Using \end{startframe}: The command is not an environment, so the correct closing line remains \end{frame}.
  • Omitting the required title argument: With the two-argument definition, write \startframe{Title} or \startframe[option]{Title}.
  • Putting verbatim material in an ordinary frame: Use the fragile option when the frame contains verbatim text or code that requires verbatim processing.
  • Loading the wrong class combination: Use either beamer as the document class, or article with beamerarticle; do not combine the two approaches in one build.

References

  1. The Beamer Class User Guide

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.