2

Is there any way I can, so to say, frame my forest-tree with the shape of a cone?

This is what I got so far:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{float}
\usepackage[linguistics]{forest}
\usetikzlibrary{positioning,fit,calc,cd}
\begin{document}
    \begin{figure}[H]
        \centering
        \begin{tikzpicture}
              \draw[dashed] (0,0) arc (170:10:2cm and 0.4cm)coordinate[pos=0] (a);
              \draw (0,0) arc (-170:-10:2cm and 0.4cm)coordinate (b);
              \draw (a) -- ([yshift=4cm]$(a)!0.5!(b)$) -- (b);
         \end{tikzpicture}
        \begin{forest}
        [0[1[0[1\\ \vdots][1\\ \vdots][2\\ \vdots]][2[0\\ \vdots]]][1[0\\ \vdots][2\\ \vdots]][2[0\\ \vdots]]]
        \end{forest}
    \end{figure}
\end{document}

Tree & Cone

Now, I just need both figures to somehow overlap.

2 Answers2

2

With the use of tikzmark, you can place a node at your forest root, and draw your cone from there, with the remember picture, overlay] option.
Note that I didn't do any math, just drew on the fly. You can obviously define a 'real' cone but it depends on what you exactly need.

Geometric figure 'framing' forest tree

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{float}
\usepackage[linguistics]{forest}
\usetikzlibrary{positioning,fit,calc,cd,tikzmark}
\begin{document}
    \begin{figure}[H]
        \centering
    \begin{forest}
    [\subnode{ROOT}{0}
        [1
            [0
                [1\\ \vdots]
                [1\\ \vdots]
                [2\\ \vdots]]
                [2[0\\ \vdots]
            ]
        ]
    [1
        [0\\ \vdots]
        [2\\ \vdots]]
        [2
            [0\\ \vdots]
        ]
    ]
    \end{forest}

    \begin{tikzpicture}[remember picture,overlay]
            \path ($(ROOT)+(0,2)$) coordinate (S);
            \draw (S) --++ (-60:8) coordinate (rbase)
                  (S) --++ (-120:8);
            \draw (rbase) arc (-5:-175:4.01cm and 0.8cm);
            \draw[dashed] (rbase) arc (5:175:4.01cm and 0.8cm);
     \end{tikzpicture}
\end{figure}

\end{document}

SebGlav
  • 19,186
  • 2
    Brilliant solution. I've actually looked into the 'remember picture' option, but wasn't able to pull it off myself. Merci beaucoup @SebGlav. – maskenissen Jan 22 '22 at 20:32
2

SebGlav's solution works nicely, but it has two drawbacks. First, we need two compilation cycles due to the tikzmark. Second, the surrounding text might overlap the cone. The alternative solution below employs the fact that you can put any TikZ code after the last closing bracket of the tree and \end{forest}. To draw the cone around the tree below, I used the OP's code for the tree and the minimally adjusted TikZ code for the cone from SebGlav's answer (I only needed to remove ROOT in the first line of the cone. The TikZ code following the tree is applied in the context of the root note, as if it was an argument to the tikz key, so the relative node () refers to the root node.)

\documentclass{article}
\usepackage{tikz}
\usepackage{float}
\usepackage[linguistics]{forest}
\begin{document}
\begin{figure}[H]
  \centering
  \begin{forest}
    [0[1[0[1\\ \vdots][1\\ \vdots][2\\ \vdots]][2[0\\ \vdots]]][1[0\\ \vdots][2\\ \vdots]][2[0\\ \vdots]]]
    \path ($()+(0,2)$) coordinate (S);
    \draw (S) --++ (-60:8) coordinate (rbase)
    (S) --++ (-120:8);
    \draw (rbase) arc (-5:-175:4.01cm and 0.8cm);
    \draw[dashed] (rbase) arc (5:175:4.01cm and 0.8cm);
  \end{forest}
\end{figure}
\end{document}