TransWikia.com

How to modify Forest nodes in Beamer overlays?

TeX - LaTeX Asked on March 28, 2021

I would like to change a property, say the color, of a forest node between overlays in beamer. It is proposed elsewhere to use PGF’s keys for this in the general case. However, this approach doesn’t seem to be compatible with forest, at least this is what I experience. The following MWE does not compile:

documentclass{beamer}
usepackage{forest}

tikzset{onslide/.code args={<#1>#2}{%
  only<#1>{pgfkeysalso{#2}} 
}}

begin{document}
begin{frame}

begin{forest}
[A,name=a [B,name=b,onslide=<2>{red}] [C,name=c] ]
end{forest}

end{frame}
end{document}

I could equally well live with a solution along the following lines:

documentclass{beamer}
usepackage{forest}

begin{document}
begin{frame}

begin{forest}
[A,name=a [B,name=b] [C,name=c] ]
only<2>{
    %add parameter red to node b
}
end{forest}

end{frame}
end{document}

Any idea how to achieve this?

Edit: Thanks to @cfr for making me aware of the stupid bug in my MWE. tikzset was located inside the forest environment.

One Answer

This code is originally adapted from various answers by other people. It is what I'm currently using for this kind of thing. Hopefully it will help you too.

documentclass{beamer}
usepackage{forest}
tikzset{% set up for transitions using tikz with beamer overlays
  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
  },
  transparent/.style={opacity=0.1,text opacity=0.1},
  opaque on/.style={alt=#1{}{transparent}},
  alerted/.style={color=alerted text.fg},
  alert on/.style={alt=#1{alerted}{}},
}
forestset{%
  visible on/.style={%
    for tree={%
      /tikz/visible on={#1},
      edge={/tikz/visible on={#1}}}},
  opaque on/.style={%
    for tree={%
      /tikz/opaque on={#1},
      edge={/tikz/opaque on={#1}}}},
  alerted on/.style={%
    for tree={%
      /tikz/alerted on={#1},
      edge={/tikz/alerted on={#1}}}},
}
begin{document}
begin{frame}

begin{forest}
  [A,name=a [B,name=b,alt=<2>{red}{}] [C,name=c] ]
end{forest}

end{frame}
end{document}

trees

Obviously you don't need all of the styles I show, but I included them for completeness as you can easily delete them if they are unwanted.

Correct answer by cfr on March 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP