5

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:

MWE

\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}
tvk
  • 1,637

1 Answers1

2

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}

enter image description here

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.