# Avoiding jumping nodes in a tree with forest and beamer

There is something in the manual about avoiding jumps and it is explained here Avoiding jumping frames in beamer as well. But I have maybe a different situation in trees. Is there a way to avoid the jumps between the two figures in the following code?

\documentclass{beamer}

\usepackage{forest}

% http://tex.stackexchange.com/questions/167690/presenting-a-forest-tree-from-bottom-to-top-in-beamer
% showing and hiding nodes and edges in forest
\tikzset{
invisible/.style={opacity=0,text opacity=0},
visible on/.style={alt=#1{}{invisible}},
alt/.code args={<#1>#2#3}{%
\alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}} % \pgfkeysalso doesn't change the path
},
}
\forestset{
visible on/.style={
for tree={
/tikz/visible on={#1},
edge={/tikz/visible on={#1}}}}}

\newcommand{\sliste}[1]{%
\mbox{%
$\left\langle\mbox{\upshape\scshape #1}\right\rangle$}%
}

% specification for all trees, "default preamble" appends to existing specification.
% The version with apostrophe replaces it.
\forestset{default preamble'={
for tree={align=center,parent anchor=south, child anchor=north,anchor=north,base=bottom},
before drawing tree={
sort by=y,
for min={tree}{baseline}
}
}}

\forestset{
declare dimen={child anchor yshift}{0pt},
if n children>=2{
before packing={
tempdima/.max={max_y}{children},
for children={
child anchor yshift=tempdima()-max_y()
},
}
}{}
},
default preamble={
for tree={
edge path'={(!u.parent anchor)--([yshift=\forestoption{child anchor yshift}].child anchor)},
}
},
}

\begin{document}

\frame{

\begin{forest}
[S
[\alt<1>{XP}{NP1234}]
[\alt<1>{YP}{NP1234}]
[\alt<1>{ZP}{NP1234}]
[\ldots, visible on=<1>]
[{\alt<1>{H}{V} \sliste{ \alt<1>{XP}{NP}, \alt<1>{YP}{NP}, \alt<1>{ZP}{NP}\visible<1>{, \ldots} }}
[{V \sliste{ NP, NP, NP }}, visible on=<1>,no edge]]]
\end{forest}

\pause

}

\end{document}


Edit: Made the NPs longer by adding 1234 so that the jump is really obvious.

• According to what I find your frame does not jump. Could you please add \begin{tikzpicture}[overlay,remember picture] \draw[red] (current page.west) -- (current page.east); \end{tikzpicture} just before \pause, compile 3 times and tell me if you really see a jump.
– user121799
Mar 3, 2019 at 17:49
• Yes, if you hold a piece of paper at NP and check with ZP after advancing to the next slide, you see that ZP starts at a different place from NP. Mar 3, 2019 at 18:16
• I just played the thing on full screen mode in acroread and have a hard time confirming what you are saying. Apart from the obvious observation that the characters used in the tree have different widths I cannot see any jump. Please consider clarifying which jumps you are talking about.
– user121799
Mar 3, 2019 at 18:30
• It jumps. If you hold a piece of paper at the N of NP that was a ZP on slide 1 you see that the NP does not start at the position of the ZP. Also the tree edges are at different places. Mar 3, 2019 at 18:42
• OK, thanks! Yes, "Apart from the obvious observation that the characters used in the tree have different widths I cannot see any jump. " You could add a minimum width to the nodes whose contents changes.
– user121799
Mar 3, 2019 at 22:58

This may be a first step towards a solution. I added a key min width to which can pass the two different texts. Yes, it would be much better if one could change \alt to do that automatically, but I was unable to do this because of expansion issues. (I do not even know against whose expansion I was fighting, beamer or forest.)

\documentclass{beamer}

\usepackage{forest}

% http://tex.stackexchange.com/questions/167690/presenting-a-forest-tree-from-bottom-to-top-in-beamer
% showing and hiding nodes and edges in forest
\tikzset{
invisible/.style={opacity=0,text opacity=0},
visible on/.style={alt=#1{}{invisible}},
alt/.code args={<#1>#2#3}{%
\alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}} % \pgfkeysalso doesn't change the path
},
}
\forestset{
visible on/.style={
for tree={
/tikz/visible on={#1},
edge={/tikz/visible on={#1}}}}}

\newcommand{\sliste}[1]{%
\mbox{%
$\left\langle\mbox{\upshape\scshape #1}\right\rangle$}%
}

% specification for all trees, "default preamble" appends to existing specification.
% The version with apostrophe replaces it.
\forestset{default preamble'={
for tree={align=center,parent anchor=south, child anchor=north,anchor=north,base=bottom},
before drawing tree={
sort by=y,
for min={tree}{baseline}
}
}}

\forestset{
declare dimen={child anchor yshift}{0pt},
if n children>=2{
before packing={
tempdima/.max={max_y}{children},
for children={
child anchor yshift=tempdima()-max_y()
},
}
}{}
},
default preamble={
for tree={
edge path'={(!u.parent anchor)--([yshift=\forestoption{child anchor yshift}].child anchor)},
}
},
}

\begin{document}
\tikzset{min width/.style n args={2}{/utils/exec={\pgfmathparse{max(width("#1"),width("#2"))}
\edef\mywidth{\pgfmathresult}},
text width={\mywidth pt}}}
\frame{

\begin{forest}
[S
[\alt<1>{XP}{NP1234},min width={XP}{NP1234}]
[\alt<1>{YP}{NP1234},min width={YP}{NP1234}]
[\alt<1>{ZP}{NP1234},min width={ZP}{NP1234}]
[\ldots, visible on=<1>]
[{\alt<1>{H}{V} \sliste{ \alt<1>{XP}{NP}, \alt<1>{YP}{NP}, \alt<1>{ZP}{NP}\visible<1>{, \ldots} }}
[{V \sliste{ NP, NP, NP }}, visible on=<1>,no edge]]]
\end{forest}

\begin{tikzpicture}[overlay,remember picture]
\draw[red] (current page.south west) grid (current page.north east);
\end{tikzpicture}
\pause

}

\end{document}


The red grid is only to guide the eye.