Tx · LaTeX & TikZ

How to Use \only in a Beamer Presentation

Published By the 9to5Science editorial team

At a glance

  1. 1. Choose an overlay

    Specify the slide or slides on which the material should appear.

  2. 2. Wrap the content

    Put the content in braces immediately after the overlay specification.

  3. 3. Check layout changes

    Remember that content hidden by \only takes up no space.

  4. 4. Use a reserved-space option

    Choose \uncover or \visible instead when later material should stay in place.

Beamer creates a sequence of slides, called overlays, from a single frame. The command \only displays its contents on the overlays you specify and omits them entirely from the others. This is useful when you want different content in the same part of a frame, but the lack of reserved space can affect alignment.

Basic syntax

Write \only followed by an overlay specification in angle brackets and then the content in braces:

\only<overlay specification>{content}

For example, \only<2>{This appears on overlay 2.} shows the sentence on the second overlay of the current frame. It is absent from the first overlay and any later overlays. Overlay numbers start at 1 within each frame.

Here is a complete minimal example that produces two slides from one frame:

\documentclass{beamer}

\begin{document}

\begin{frame}{Using \texttt{\textbackslash only}}
  This sentence appears on both slides.

  \only<1>{This sentence appears on the first slide only.}
  \only<2->{This sentence appears from the second slide onward.}
\end{frame}

\end{document}

Compile this with a standard LaTeX engine such as pdfLaTeX. The frame has two overlays: the first \only sentence appears on overlay 1, and the second appears on overlay 2. The shared sentence appears on both.

Common overlay specifications

The specification tells Beamer which overlays should contain the material. These are common forms:

SpecificationWhere the content appears
<2>Overlay 2 only
<2->Overlay 2 and every later overlay
<1,3>Overlays 1 and 3 only
<2-4>Overlays 2, 3, and 4
<1->Every overlay from overlay 1 onward

A frame only has as many overlays as its content requires. For example, <2-> makes content available starting on overlay 2, but does not by itself require a particular number of later overlays. Beamer determines the sequence from the overlay commands in the frame.

What happens to the layout?

On overlays where the specification does not match, \only removes the content rather than making it invisible. The omitted content takes up no space. Consequently, a paragraph, image, or other material below it may move up or change position between overlays.

For example, suppose a frame contains a large image inside \only<2>{...} followed by a paragraph. The paragraph is laid out without the image on overlay 1 and with it on overlay 2. That can be exactly what you want, but it may make the slide appear to jump.

When content should appear later without moving the material around it, use a command that reserves space:

\uncover<2->{This is hidden at first, but its space is reserved.}

You can also use \visible<2->{...} to keep the space while making the content visible only on the specified overlays. In ordinary use, both are alternatives to \only when stable positioning matters. If you want items to be revealed one at a time while keeping the list structure clear, Beamer’s overlay-aware list commands such as \item<2-> are often a better fit.

A practical example: replacing a statement

Use separate \only commands when one statement should be replaced by another in the same general location:

\begin{frame}{A changing statement}
  \only<1>{The estimate is preliminary.}
  \only<2->{The estimate has been checked.}
\end{frame}

The first statement is present on overlay 1, and the second is present from overlay 2 onward. Since their lengths differ, the layout can differ slightly. If keeping the text baseline or surrounding layout fixed is important, consider placing both versions in a fixed-size box or using a suitable Beamer block structure; merely using \only does not align their dimensions.

Common mistakes

  • Leaving out the braces: The content to show must be grouped as the argument, as in \only<2>{...}.
  • Expecting hidden content to leave a blank: \only removes it. Use \uncover or \visible if the layout should reserve its space.
  • Using an overlay number as a frame number: <2> means the second overlay of the current frame, not the second frame in the presentation.
  • Wrapping verbatim material directly: \only takes its contents as a command argument, and verbatim environments generally cannot be used inside such arguments. For code listings, use a listing package’s Beamer-aware options or another supported approach rather than assuming that putting a listing inside \only will compile.

Choose \only when absent content should have no effect on the layout. Choose a space-reserving command when consistent placement across overlays is more important.

References

  1. The Beamer class: User Guide

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