2

I'm trying to set up a template for rendering decision trees using tikz-qtree. Here's what I ended up with :

\documentclass[border = 0.2cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes, positioning}
\usepackage{tikz-qtree}
\begin{document}

\begin{tikzpicture}[ % liens edge from parent path={ [thick, ->, rounded corners] (\tikzparentnode.south) |- +(0,-0.7cm) -| (\tikzchildnode.north) }, % racine every level 0 node/.style={ draw, rounded rectangle, minimum height=1cm, thick }, % noeuds internes every internal node/.style={ draw, diamond, minimum width=2cm, minimum height=2cm, thick }, % feuilles 'oui' yesnode/.style={ circle, draw=green!60, fill=green!5, very thick, minimum width=1cm, minimum height=1cm }, nonode/.style={ circle, draw=red!60, fill=red!5, very thick, minimum width=1cm, minimum height=1cm }, % distances de séparations level distance=3.3cm, sibling distance=2cm ] \Tree [.{conditions météorologiques} \edge node[auto=left, pos=0.25, above]{ensoleillé}; [.{humidité} \edge node[auto=left, pos=0.25, above]{basse}; [.\node[yesnode]{oui}; ] \edge node[auto=left, pos=0.25, above]{haute}; [.\node[nonode]{non}; ] ] \edge node[auto=left, pos=0.25]{pluvieux}; [.{vent} \edge node[auto=left, pos=0.25, above]{faible}; [.\node[yesnode]{oui}; ] \edge node[auto=left, pos=0.25, above]{fort}; [.\node[nonode]{non}; ] ]
] \end{tikzpicture}

\end{document}

And this is what the compiler renders : enter image description here

After reading through documentation and browsing several Latex stack exchange posts these are the questions that I still have :

  1. How can I horizontally align the text from the nodes 'ensoleillé' and 'pluvieux' ? Basically moving down 'pluvieux'. I believe that the letter 'p' is causing the problem.

  2. For now I have to manually determine the minimum height and width of the internal nodes. Is there a way to let the compiler choose the minimum height and width of the internal nodes required to fit the text within it and then apply it to all other internal nodes?

  3. If I try to add a third node on the first level the path is not drawn in the way I intended to. Here's what's rendered : enter image description here

  4. If there's any type of bad habits in my script please let me know.

I apologize for my poor English and if I'm not using the correct terminology. If anyone knows how to solve any of the issues above or if they already have been somewhere else please let me know :)

Bernard
  • 271,350
Lucas
  • 29
  • Regarding 1), see e.g. https://tex.stackexchange.com/a/439025/ or the linked question. You can for example add a \strutin each node. – Torbjørn T. Jan 24 '22 at 19:07

2 Answers2

1

Welcome to TeX.SX!

To address your first problem regarding the vertical shift of the text in different nodes, you can set the option text depth to zero (or some other fixed unit) for every node. Or, as proposed by Torbjørn T. in the comments, you can add \strut to the text of each node.

As for your third problem, you need to define another edge that does not have rounded corners and which you need to select if the edge goes straight downwards. I assume that some rounding errors are responsible for not giving you the desired straight line here. The following code may help you achieve this:

\documentclass[border = 0.2cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes, positioning}
\usepackage{tikz-qtree}
\begin{document}

\begin{tikzpicture}[ % liens edge from parent path={ [thick, ->, rounded corners] (\tikzparentnode.south) |- +(0,-0.7cm) -| (\tikzchildnode.north) }, straight/.style={ edge from parent path={ [thick, ->] (\tikzparentnode.south) -- (\tikzchildnode.north) } }, every node/.style={ text depth=0pt, }, % racine every level 0 node/.style={ draw, rounded rectangle, minimum height=1cm, thick }, % noeuds internes every internal node/.style={ draw, diamond, minimum width=2cm, minimum height=2cm, thick }, % feuilles 'oui' yesnode/.style={ circle, draw=green!60, fill=green!5, very thick, minimum width=1cm, minimum height=1cm }, nonode/.style={ circle, draw=red!60, fill=red!5, very thick, minimum width=1cm, minimum height=1cm }, % distances de séparations level distance=3.3cm, sibling distance=2cm ] \Tree [.{conditions météorologiques} \edge node[auto=left, pos=0.25, above]{ensoleillé}; [.{humidité} \edge node[auto=left, pos=0.25, above]{basse}; [.\node[yesnode]{oui}; ] \edge node[auto=left, pos=0.25, above]{haute}; [.\node[nonode]{non}; ] ] \edge[straight]; [.{test} ] \edge node[auto=left, pos=0.25]{pluvieux}; [.{vent} \edge node[auto=left, pos=0.25, above]{faible}; [.\node[yesnode]{oui}; ] \edge node[auto=left, pos=0.25, above]{fort}; [.\node[nonode]{non}; ] ]
] \end{tikzpicture}

\end{document}

enter image description here

The second problem is a bit difficult to tackle. I cannot think of a simple solution here at the moment.

1

As pure TikZ image. Shorter and simpler code:

\documentclass[border = 0.2cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                positioning,
                shapes}

\begin{document} \begin{tikzpicture}[ node distance = 21mm and 7mm, arr/.style = {-Straight Barb, semithick, rounded corners}, C/.style = {circle, draw=#1, thick, fill=#1!30, minimum size=2em}, D/.style = {diamond, draw, thick, aspect=2, text width=5em, align=center, inner xsep=0pt, text depth=0.5ex}, N/.style = {rounded rectangle, draw, thick, minimum height=5ex}, lbl/.style = {text depth=0.5ex, inner sep=2pt, pos=0.25, above} ] %%% nodes \node (n1) [N] {conditions météorologiques}; % \node (n2a) [D, below left =of n1] {humidité}; \node (n2b) [D, below right=of n1] {vent}; \node (n2c) [D, at={(n2a -| n1)}] {test}; % \node (n31a) [C=teal, below left =of n2a.west] {oui}; \node (n31b) [C=red, below right=of n2a.east] {non}; \node (n32a) [C=teal, below left =of n2b.west] {oui}; \node (n32b) [C=red, below right=of n2b.east] {non}; %%% edges \draw[arr] (n1.south) -- ++ (0,-1) -| (n2a) node[lbl] {ensoleillé}; \draw[arr] (n1.south) -- ++ (0,-1) -| (n2b) node[lbl] {pluvieux}; \draw[arr] (n1) -- (n2c);

\draw[arr] (n2a.south) -- ++ (0,-0.8) -| (n31a) node[lbl] {basse}; \draw[arr] (n2a.south) -- ++ (0,-0.8) -| (n31b) node[lbl] {haute};

\draw[arr] (n2b.south) -- ++ (0,-0.8) -| (n32a) node[lbl] {faible}; \draw[arr] (n2b.south) -- ++ (0,-0.8) -| (n32b) node[lbl] {fort}; \end{tikzpicture} \end{document}

enter image description here

Zarko
  • 296,517