# Beamer and forest: Dynamically highlight a path from the root to a node in a tree

I would like in a Beamer document to dynamically highlight a path leading from the root of a tree (defined using the forest package) to a specific node by defining a forest style. Below is a minimum non-working example.

The issue is that the style onslide is defined in TikZ which does not recognize the forest style path highlight that I defined. I also cannot define the path highlight style in TikZ instead, since TikZ does not know forest styles like edge and for ancestor.

Followup question: In the highlighted path, is it possible to leave just the root unhighlighted?

Example:

\documentclass{beamer}

\usepackage{tikz}

\tikzset{onslide/.code args={<#1>#2}{%
\only<#1>{\pgfkeysalso{#2}}
}} % use a style for a node/path only on certain slides
%https://tex.stackexchange.com/q/253364/9789

\usepackage{forest}

\begin{document}

\forestset{path highlight/.style={edge=red,line width=2pt,
for ancestors={edge=red, line width=2pt}}}

\begin{frame}{}
\begin{forest}
[VP
[DP
[V']
[DP,onslide=<2->{path highlight}]
]
[DP]
]
\end{forest}
\end{frame}

\end{document}


You may want to use for tree, see this answer, and need to use \forestset, not \tikzset. Excluding the root is then as easy as saying if level=0{}{red}. I assume you intended to say edge={red, line width=2pt},, i.e. add the line width to the edge.

\documentclass{beamer}
\usepackage{forest}
\forestset{path highlight/.style={red,edge={red, line width=2pt},
for ancestors={edge={red,line width=2pt},
if level=0{}{red}}}}

% see https://tex.stackexchange.com/a/112471/121799
\forestset{alt/.code args={<#1>#2#3}{%
\alt<#1>{\pgfkeysalso{for tree={#2}}}{\pgfkeysalso{for tree={#3}}}},
onslide/.code args={<#1>#2}{%
\only<#1>{\pgfkeysalso{for tree={#2}}}
}}

\begin{document}

\begin{frame}{}
\begin{forest}
[VP
[DP
[V']
[DP,onslide=<2->{path highlight}
]
]
[DP]
]
\end{forest}
\end{frame}
\end{document}


Let me remark that all these styles like alt are part of the nice TikZ library beamer-overlay-styles but of course here we need \forestset versions thereof.

• This is great help. Thank you! – Fang Jing Jun 4 '19 at 20:59