Search code examples
latexbeamertikz

How to have a fixed size number that depends on the slide in latex beamer / tikz?


I have a tikz picture with a circle node which has a number inside. I want this number to change with the slides but to be of fixed size. (-> not mess up the circle).

This one isn't working, the circle gets as big as if it has to hold all 3 numbers.

\tikz[baseline]
\node [fill=blue!20,draw,circle,anchor=base] (node1)
{
    \only<1-3>{3} \only<4-6>{8} \only<7->{11.5}
};

Here the values are 3 on slides 1-3, 8 on slides 4-6 and else 7.


Solution

  • I observed the following:

    • The spaces between \only{} are always rendered (\only, when not active, is a "zero-width character"; TeX does not discard spaces between characters)
    • Set the width of the text explicitly using "text width=...". This implicitly puts the content into a minipage.
    • Note that the "baseline" property does not work anymore as expected.
    • "text centered" ensures the alignment for the case that the content is not exactly "text-width"
    • The "overprint" environment only automatically determines the height of the content, not the width.

    To conclude, this works for me (I tried it):

    \tikz[baseline]
    \node [fill=blue!20,draw,circle,anchor=base,text width=4ex,text centered,inner sep=0] (node1)
    {
        \only<1-3>{3}\only<4-6>{8}\only<7->{11.5}
    };
    

    EDIT: This works with the correct baseline:

    \tikz[baseline=(node1.base)]
        \node [fill=blue!20,draw,circle] (node1)
        {
            \begin{minipage}{4ex}
                \centering
                \only<1-3>{3}\only<4-6>{8}\only<7->{11.5}
            \end{minipage}
        };