3

Often in beamer I want to produce a build-in. A complicated version is when I want to start with <expr> and then build in an underbrace \underbrace{<expr>}_{foo}. If the slide is centered vertically, and I do so with \only<1>{...} and then \only<2->{...}, then I will see a jump in the equation because the height of the displayed equation with the underbrace is larger.

One solution is to put a \vphantom{...} in the \only<1>{...}, but this requires that I copy the larger expression (with, say, the underbrace) from \only<2->{...foo...} into the first \only<1>{... \vphantom{foo}...}. This is a rather ugly solution because my code has just doubled in size, usually.

I'd prefer an approach where I measure heights into the .aux and use these in the second compilation (or third, I don't mind) to just use simply \vspace*{\heightfoo}.

Any ideas out there?

Here's a MWE:

\[
\hat w = \arg\min_{w}
\only<1>{\frac 1 m \sum_{i=1}^{m} f(w) \vphantom{ \underbrace{\frac 1 m \sum_{i=1}^{m} f(w)}_{L(w)}}}%
\only<2->{\underbrace{\frac 1 m \sum_{i=1}^{m} f(w)}_{L(w)}}
\]
  • 5
    Your code is not an MWE. – Sveinung Oct 21 at 19:48
  • I've fixed a typo in the code. MWE now runs (in beamer environment). – Daniel Roy Oct 21 at 20:27
  • 1
    It is still not an MWE. An MWE contains the code that is necessary, so it will compile on it own. – Sveinung Oct 21 at 20:55
  • 1
    THis is an MWE: \documentclass{beamer} \begin{document} \begin{frame} [ \hat w = \arg\min_{w} \only<1>{\frac 1 m \sum_{i=1}^{m} f(w) \vphantom{ \underbrace{\frac 1 m \sum_{i=1}^{m} f(w)}_{L(w)}}}% \only<2->{\underbrace{\frac 1 m \sum_{i=1}^{m} f(w)}_{L(w)}} ] \end{frame} \end{document} – Sveinung Oct 21 at 21:02
  • 1
    please always post a complete document not a fragment. Your code doesn't need to double in size to repeat any part of the document \newcommand\foo{long equation} \foo .. \foo will use the equation in two contexts – David Carlisle Oct 21 at 21:21
3

The following code makes \underbrace and \overbrace overlay-aware and uses \alt<os>{<default>}{<alternative>} to print <default> on <os> and <alternative> elsewhere. So, \underbrace<os>{<stuff>}_{<label>} sets \underbrace{<stuff>}_{<label>} on <os> and \vphantom{\underbrace{<stuff>}_{<label>}}<stuff> elsewhere (similarly for \overbrace).

\documentclass{beamer}

\usepackage{xparse,letltxmacro}

\LetLtxMacro\oldunderbrace\underbrace
\LetLtxMacro\oldoverbrace\overbrace

\RenewDocumentCommand{\underbrace}{d<> m e{_^}}{%
  \alt<#1>%
    {\oldunderbrace{#2}_{\IfValueT{#3}{#3}}^{\IfValueT{#4}{#4}}}%
    {\vphantom{\oldunderbrace{#2}_{\IfValueT{#3}{#3}}^{\IfValueT{#4}{#4}}}#2}%
}%
\RenewDocumentCommand{\overbrace}{d<> m e{_^}}{%
  \alt<#1>%
    {\oldoverbrace{#2}_{\IfValueT{#3}{#3}}^{\IfValueT{#4}{#4}}}%
    {\vphantom{\oldoverbrace{#2}_{\IfValueT{#3}{#3}}^{\IfValueT{#4}{#4}}}#2}%
}%

\begin{document}

\begin{frame}
  \[
    \hat w = \arg\min_w
      \underbrace<2->{\frac{1}{m} \sum_{i = 1}^{m} f(w)}_{L(w)}
  \]
\end{frame}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.