46

I've got an underbrace (\underbrace{foo}_{bar}) which I want to uncover (i.e. foo should be there from the beginning, while the underbrace and the bar should be uncovered on the next slide. I've tried the following, which (not surprisingly) doesn't work:

\documentclass{beamer}

\begin{document}
\begin{frame}
  Without uncover (this is what it should look like on the second subframe):
  \begin{displaymath}
    \underbrace{foo}_{bar}
  \end{displaymath}
  With uncover (doesn't work):
  \begin{displaymath}
    \uncover<2>{\underbrace}{foo}\uncover<2>{_{bar}}
  \end{displaymath}
\end{frame}
\end{document}

In the second displaymath, the underbrace is displayed before foo and the bar as index to foo, and moreover the underbrace is immediately visible.

Note that \only wouldn't be a solution because on one hand in my actual document I use \setbeamercovered{transparent} so the underbrace and the text below should be lightly visible from the beginning, and on the other hand \only would also not keep the space for the underbrace.

So how can I achieve that?

celtschk
  • 2,438

6 Answers6

36

Here's a solution that just uses \onslide (so that there's no need to know the precise definition of transparency):

\documentclass{beamer}
\setbeamercovered{transparent}
\begin{document}
\begin{frame}
    \begin{displaymath}
        \onslide<2-> \underbrace{ \onslide<1->
        foo
        \onslide<2-> }_{bar} \onslide<1->
        \text{some more stuff for slide 1}
    \end{displaymath}
\end{frame}
\end{document}
Hendrik Vogt
  • 37,935
  • It may be useful to stress that the last \onslide<1-> puts the settings back to the defaults. Otherwise, the other frames will appear transparent. In my beamer version, the last onslide lasts past the end of the frame. – Dimitri Lesnoff Jul 07 '23 at 11:40
25

One way to do that, is to use the definition of the transparency (15% Text foreground on the background and to avoid the “jumping” use as suggested here the overprint environment.

Though you have to define your term that's \underbraced twice and in my quick hack the transparency color is the one mentioned above, because i didn't find whether i can somehow get that by \usebeamercolor

\documentclass[transparent]{beamer}
\usepackage{amsmath}
\usepackage{cancel}
\begin{document}
    \setbeamercovered{transparent}
    \begin{frame}%
        \begin{overprint}
            \onslide<1>\begin{displaymath}
                \color{normal text.fg!15!normal text.bg}
                \underbrace{\usebeamercolor[fg]{text}foo}_{bar}
            \end{displaymath}
            \onslide<2>\begin{displaymath}
                \underbrace{foo}_{bar}
            \end{displaymath}
        \end{overprint}
    \end{frame}%
\end{document}

And (thanks to @Andrew Stacey) the even shorter version just switching colors using \only (because \color is sensitive to that) would be

\begin{frame}
    \begin{displaymath}
        \color{normal text.fg!15!normal text.bg}
        \only<2->{\color{normal text.fg}}
        \underbrace{\usebeamercolor[fg]{text}foo}_{bar}
    \end{displaymath}
\end{frame}
Ronny
  • 6,110
16

Based on Hendrik Vogt's answer, and for my needs, I created these commands to make my life easier:

\newcommand<>{\uncoverubrace}[2]{%
  \onslide#3 \underbrace{ \onslide<1->%
  #1%
  \onslide#3 }_{#2} \onslide<1->%
}
\newcommand<>{\uncoverobrace}[2]{%
  \onslide#3 \overbrace{ \onslide<1->%
  #1%
  \onslide#3 }^{#2} \onslide<1->%
}

Now the use is very simple:

\usepackage{mathtools}
...
\begin{displaymath}
\uncoverubrace<2->{a_i}{\mathclap{\text{$N$ elements}}} =
\frac{
  \uncoverobrace<3->{b_j-b_i}{\mathclap{\text{$2N$ items needed}}}
}{2h}
\end{displaymath}

Room for improvement: allow relative overlay specifications (<+->).

Jellby
  • 3,323
6

Based on Jellby's answer, I was not only able to simplify the code, but also patch the original underbrace using the xparse package such that it works with arbitrary <overlay-specification>.

\documentclass{beamer}
\setbeamercovered{transparent}
\usepackage{amsmath, xparse, letltxmacro}
\LetLtxMacro{\oldunderbrace}{\underbrace}

\DeclareDocumentCommand{\underbrace}{d<> m e{}}{% \IfValueTF{#1}{% IF <overlay-specification> given % using global onlside flag, cf. p82 beamer manual v3.59 \oldunderbrace{#2\onslide<#1>}{#3}\onslide% }{% ELSE \oldunderbrace{#2}_{#3} }% }%

\begin{document}

\begin{frame}{\alt<1,3>{LINKS}{ZWO}} \begin{block}<+->{} \begin{align} \underbrace<+->{\text{\color{green} LINKS!}}{\color{red} \text{ZWO!}} \underbrace<+->{\text{\color{green} LINKS!}}{\color{red} \text{DREI!}} \underbrace<+->{\text{\color{green} LINKS!}}_{\color{red} \text{VIER!}} \end{align} \end{block} \end{frame}

\end{document}

I guess if one wanted one could even built-in an \@ifclassloaded{beamer} in order to automagically ignore the <> when other classes are loaded.

For \overbrace it is a bit different, see my other post here: Beamer: Uncover underbrace & overbrace weird behaviour

  • 1
    I'd say this should be the preferred solution. – Bubaya May 23 '22 at 19:49
  • Weirdly enough, the equivalent with overbrace does not seem to work

    \LetLtxMacro{\oldoverbrace}{\overbrace} \DeclareDocumentCommand{\overbrace}{d<> m e{^}}{% \IfValueTF{#1}{% IF <overlay-specification> given % using global onlside flag, cf. p82 beamer manual v3.59 \oldoverbrace{#2\onslide<3->}^{#3}\onslide% }{% ELSE \oldoverbrace{#2}^{#3} }% }%

    – Laurent Jacques Jan 27 '23 at 16:31
  • the issue seems to come from \overbrace and its interaction with \onslide, it doesn't react similarly to \overbrace, for instance \underbrace{a\onslide<2->}_{b}\onslidebut not \overbrace{a\onslide<2->}^{b}\onslide – Laurent Jacques Jan 27 '23 at 16:36
  • ok the problem is explained here, weird effect – Laurent Jacques Jan 27 '23 at 16:39
1

Ok, from the proposition of Hyperplane, here is the solution for \overbrace. As explained at Beamer: Uncover underbrace & overbrace weird behaviour \overbrace behaves differently from \underbrace, the code had thus to be adapted a bit.

\LetLtxMacro{\oldoverbrace}{\overbrace}
 \DeclareDocumentCommand{\overbrace}{d<> m e{^}}{%
   \IfValueTF{#1}{% IF <overlay-specification> given
     % using global onlside flag, cf. p82 beamer manual v3.59
     \onslide<#1>\oldoverbrace{\onslide #2}^{#3}\onslide%
   }{% ELSE
     \oldoverbrace{#2}^{#3}
   }%
}%
0

This code is a litte bit simpler, so it might be worth sharing. The idea is to combine alt with vphantom. This is how the command is defined:

\newcommmand<>{\underbraceonslide}{\alt#3{\underbrace{#1}{#2}}{\vphantom{\underbrace{#1}{#2}}#1}}

It uses beamer's overlay specifications, e.g.

\underbraceonslide<2->{ \frac{1}{n} }{\to 0}