13

I want to connect two forest trees with an arrow, in a similar way it is discussed here for plain tikz trees. However, it seems to be impossible to add options to the forest environment in a similar fashion. Simply referencing the nodes will only place the arrow to the right of the right tree. (To avoid confusions: I don't speak about the $\Rightarrow_G$ in the first forest environment, but the bended one, that should connect two specific nodes.)

\documentclass{beamer}
\usepackage{tikz}
\newcommand{\false}{\mathbf{f}}
\usetikzlibrary{calc, positioning}
\usetikzlibrary{arrows}
\usepackage{forest}
\setbeamercovered{transparent}
\begin{document}

\begin{frame}{}  

% Tree 1
\begin{forest}
    [$\sigma$, calign=last
        [$a$]
        [{\langle \sigma, \{1,2\},\false \rangle}% 
                , math content, calign=first, baseline, draw=blue, ellipse,  name = replaceNode
            [$\sigma_*$]
            [$a$]
        ]
    ]
  \node at (current bounding box.east)
        [anchor=west]
        {$\Rightarrow_G$};
\end{forest}
%
% Tree 2
\begin{forest}
    [$\sigma$, calign=last
        [$a$]
        [$\sigma$, calign=last
            [$b$]
            [{\langle \sigma, \{1,2\},\false \rangle}% 
                , math content, calign=first, baseline, name = t1
                [$\sigma$, calign=first
                    [$\sigma_*$]
                    [$a$]
                ]
                [$b$]
            ]
        ]
    ]
\end{forest}
%
% This arrow should connect two nodes of both trees.
\begin{tikzpicture}
\draw[->, black, dashed, bend angle=45, bend left] (replaceNode) to (t1);
\end{tikzpicture}


\end{frame}

\end{document}

There seems to be the workaround of putting both trees in the same forest environment with a phantom parent node. However, I would appreciate an overlay solution for better modularity of my graphics, especially if more than two trees are involved.

2
  • Interesting question. One can add the remember picture option by for example \forestset{begin draw/.code={\begin{tikzpicture}[remember picture]}}, but this ruins the rendering. Commented May 1, 2014 at 11:26
  • Does tikzmark work with forest?
    – cfr
    Commented Jun 26, 2014 at 3:05

1 Answer 1

9

As @cfr suggested in the comment section, this can be done by making use of the tikzmark library.

You need to add \usetikzlibrary{tikzmark} to your preamble. Then, you can put a \subnode in each forest environment and then add a tikzpicture after that with the options overlay and remember picture.

When you issue the \draw command, you can prefix the names of the subnodes with pic cs:, if you want to treat that subnode as a coordinate point (otherwise don't use the pic cs: prefix). Note that you will also need to compile the document twice.

I've also moved the starting point and ending point of the arrow up and over a bit by using the ($()+()$) syntax for specifying a coordinate position.

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\newcommand{\false}{\mathbf{f}}
\usetikzlibrary{calc, positioning}
\usetikzlibrary{arrows}
\usepackage{forest}
\setbeamercovered{transparent}
\begin{document}

\begin{frame}{}  

% Tree 1
\begin{forest}
    [$\sigma$, calign=last
        [$a$]
        [\subnode{replaceNode}{$\langle\sigma, \{1,2\},\false \rangle$}, calign=first, baseline, draw=blue, ellipse
            [$\sigma_*$]
            [$a$]
        ]
    ]
  \node at (current bounding box.east)
        [anchor=west]
        {$\Rightarrow_G$};
\end{forest}
%
% Tree 2
\begin{forest}
    [$\sigma$, calign=last
        [$a$]
        [$\sigma$, calign=last
            [$b$]
            [\subnode{t1}{$\langle\sigma, \{1,2\},\false \rangle$}, calign=first, baseline
                [$\sigma$, calign=first
                    [$\sigma_*$]
                    [$a$]
                ]
                [$b$]
            ]
        ]
    ]
\end{forest}
%
% This arrow should connect two nodes of both trees.
\begin{tikzpicture}[overlay,remember picture]
\draw[->, black, dashed, bend angle=45, bend left] ($(pic cs:replaceNode)+(0.25,.2)$) to ($(pic cs:t1)+(0.25,.2)$);
\end{tikzpicture}

\end{frame}

\end{document}

enter image description here

2
  • 1
    Nice trees! It does seem ironic that a package named forest cannot manage more than one tree without help...
    – cfr
    Commented Oct 8, 2014 at 1:54
  • @cfr Agreed! :)
    – Adam Liter
    Commented Oct 8, 2014 at 2:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .