2

I am trying to learn to plot tree diagrams. Particulary I am interested in the type of diagrams I drew.enter image description here

Here is the code I made so far:

\documentclass{article}
\usepackage{tikz}
\usepackage[linguistics]{forest}

\begin{document}

\begin{figure} \centering \begin{tikzpicture} \coordinate child [level distance=30mm] child [level distance=15mm]{ {child [level distance=30mm] {child [level distance=15mm] child [level distance=30mm]} child [level distance=15mm]} };

 \begin{scope}[shift={(3,-3)}]
 \node (y1) {\textit{}};  
 \node  (y0)[right of=y1] {\textit{}};
 \path [->,auto, thick](y1) edge [] node [above]{$\chi_0$} (y0);
 \end{scope}

\begin{scope}[shift={(6,0)}] \coordinate child [level distance=30mm] child [level distance=15mm]{ {child [level distance=30mm] {child [level distance=15mm] child [level distance=30mm]} child [level distance=15mm]} }; \end{scope} \end{tikzpicture} \caption{.} \end{figure} \end{document}

There are 3 problems I encountered:

  1. The child edges are not aligned with the parent edges.

2) Unable to add a second tree to the same figure.

3) Unable to add an arrow and label it.

I would appreciate if someone could help with my questions.

Thanks

Den
  • 35
  • 3
  • Hello, I suggest for add the other tree use scope environment, inside the tikz code that you want. For label you can use node: \node[right] at (2cm,-60mm) {draw arrow}; just after of the first tree. – Darío Mar 22 '24 at 20:19
  • Welcome! forest or a similar package is recommended. – cfr Mar 22 '24 at 21:13
  • You're loading it but not using it? – cfr Mar 22 '24 at 21:19
  • Hello. I was not able to make edges of different lenghts in the forest package. – Den Mar 22 '24 at 21:44

2 Answers2

3

I would use Forest, though this is largely a matter of personal preference. But using some tree-specific package or other will make things a lot easier. The following defines a style nice tree for the basic style. For combinations of two trees of the kind you want, we define double nice trees which is basically a nice tree with a phantom root node and a space between the two sub-trees for you to put an arrow or whatever.

  nice tree/.style={%

We define this separately so it can be easily applied to single trees rather than being limited to cases where we want a pair.

    before typesetting nodes={%

We delay this so the nodes exist when the style is applied.

      for tree={%

Then we apply the following to the current tree i.e. the current node and its descendants.

        circle,
        fill,
        inner sep=1.5pt,

Make our dot.

        parent anchor=center,
        child anchor=center,

Aim edges to and from dot's centre to avoid getting weird-looking twists above and below.

        calign=fixed edge angles,

We want the edges of parents and children to be at constant angles.

      },
    },
  },

double nice trees sets up a tree group (2+ trees) with a phantom root and applies nice tree to the phantom's children.

  double nice trees/.style={%
    phantom,
    s sep'+=50pt,
    for children={nice tree},
  },
}

To align the dots as in the sketch, we use tier=<name>. We use four named tiers (A, B, C and D) to ensure correct alignment within and between the two trees.

We use tikz+={} to add the arrow between the two.

Complete code:

\documentclass[border=5pt,tikz]{standalone}
% ateb: https://tex.stackexchange.com/a/713845/
\usepackage[linguistics]{forest}
\usetikzlibrary{arrows.meta}
\forestset{%
  nice tree/.style={%
    before typesetting nodes={%
      for tree={%
        circle,
        fill,
        inner sep=1.5pt,
        parent anchor=center,
        child anchor=center,
        calign=fixed edge angles,
      },
    },
  },
  double nice trees/.style={%
    phantom,
    s sep'+=50pt,
    for children={nice tree},
  },
}
\begin{document}
\begin{forest}
  double nice trees,
  tikz+={\draw [-Latex,thick,shorten >=15pt,shorten <=15pt] (!r122 |- !r211) -- node [above,midway] {$\chi_0$} (!r211); },
  [
    [
      [,tier=A]
      [
        [,tier=B
          [, tier=C
            [,phantom,tier=D]
          ]
          [,tier=D]
        ]
        [,tier=A
          [,phantom,tier=B]      
        ]
      ]
    ]
    [
      [
        [,tier=B]
        [,tier=A]
      ]
      [,tier=A
        [,tier=C]
        [,tier=B
          [,phantom,tier=C]
        ]
      ]
    ]
  ]
\end{forest}

\end{document}

A bug in Okular/KDE means my images are currently horrible quality, so the following will look fuzzy here even though the output is fine.

double nice trees with arrow

You can name nodes rather than using coordinates such as (!r122) if preferred. !r122 just means the node you reach if you follow the path root-first child-second child-second child, but name=fred may be easier.

cfr
  • 198,882
0

May be you serve, using forest package:

\documentclass{article}
\usepackage{tikz}
\usepackage{forest}

\newcommand{\dotikz}{\tikz{\fill (0,0) circle (2pt);}}

\begin{document} \begin{figure} \centering \begin{minipage}{0.45\textwidth} \begin{forest} for tree={s sep=20pt} % horizontal separation between nodes [\dotikz%first [,phantom] % phantom node to add space [\dotikz,l=2cm] [\dotikz [,phantom] [\dotikz,l=2cm [\dotikz] [,phantom] [\dotikz,l=2cm] ] [\dotikz] ] ] \end{forest} \end{minipage} \begin{tikzpicture}[overlay, remember picture] \draw[-latex] (-2.5,0) -- (-.5,0) node[midway,below] {}; \end{tikzpicture}% \begin{minipage}{0.45\textwidth} \begin{forest} for tree={s sep=20pt} [\dotikz [\dotikz [,phantom] [\dotikz,l=2cm] [\dotikz] ] [,phantom] [\dotikz,l=2cm [,phantom] [\dotikz,l=2cm] [\dotikz] ] ] \end{forest} \end{minipage} \caption{Forest} \end{figure} \end{document}

enter image description here

Darío
  • 449
  • 1
  • 6
  • I thought the focus of the question was on aligning parent and child edges rather than using forest per se .... – cfr Mar 23 '24 at 02:13