[pgf-tikz] UPD: Dynamic Sibling distance on single level

Henri Menke henrimenke at gmail.com
Fri Apr 24 02:03:30 CEST 2020


Dear Gaston,

Thank you for using PGF/TikZ and thanks for reaching out.  Your drawing
looks good and the code looks fine to me.  Drawing trees in TikZ is
unfortunately a bit verbose.  There are two alternatives:

  - The graphdrawing library with the tree layout.  This is great if you
    want to draw a tree in an automated fashion an don't care too much
    about the layout.

  - The forest package is based on TikZ and was originally designed to
    draw linguistic trees, but it is also a great tool for drawing
    generic trees.

I posted a comparison of TikZ trees and Forest trees on Reddit some time
ago (this was in response to someone who though it would be a great idea
to write a Python wrapper for TikZ that requires over 100 lines of
boilerplate).
https://www.reddit.com/r/LaTeX/comments/f5q47m/i_wrote_a_python_to_tikz_transpiler_write/fi30w56/

In general I always read the mailing list and try to reply in a timely
manner, but generally if you have questions you might want to head over to
https://tex.stackexchange.com/
where there a lot more active users and you usually get an answer within
a few minutes of your question.

Kind regards,
Henri

On 23/04/20, 15:22, Gaston Gloesener wrote:
> Hello again,
> 
> 
> 
> In addition to my previous mail (below) I did some more testing and found
> that I can use the sibling distance option on the "child" macro. But I have
> to specify it on the left and right child. If only on one child, the parent
> node does not remain centered between the children.
> 
> 
> 
> Here below is my new code with adapted sibling distances as required to
> display the tree properly (red lines now also are bended, but that's not
> part of the problem ;-) ). Maybe there is a more elegant way ?
> 
> 
> 
> 
> 
> \begin{tikzpicture}[level/.style={sibling distance=80mm},
> 
>                     level 1/.style={sibling distance=55mm},
> 
>                     level 2/.style={sibling distance=55mm},
> 
>                     level 3/.style={sibling distance=25mm},
> 
>                     level 4/.style={sibling distance=17mm},
> 
>                     level 5/.style={sibling distance=10mm},
> 
>                     swapme/.style={<->,red,thick},
> 
>                     every node/.style = {shape=circle,
> 
>                       draw, align=center},
> 
>                     every
> label/.style={shape=rectangle,draw=none,fill=none,font=\tiny}
> 
>                     ]]
> 
>   \node {+}
> 
>     child { node[label=below:41] (L1_1) {+}
> 
>       child[sibling distance=45mm] { node[label=below:29] (L2_1) {+}
> 
>         child[sibling distance=28mm] { node[label=below:15] (L3_1) {*}
> 
>           child { node[label=below:9] (L4_1) {*}
> 
>             child { node[label=below:4] (L5_1) {8}}
> 
>             child { node[label=below:5] (L5_2) {A}}
> 
>           }
> 
>           child { node[label=below:6] (L4_2) {B}}
> 
>         }
> 
>         child[sibling distance=28mm] { node[label=below:14] (L3_2) {*}
> 
>           child[sibling distance=20mm] { node[label=below:8] (L4_3) {*}
> 
>             child { node[label=below:6] (L5_3) {B}}
> 
>             child { node[label=below:2] (L5_4) {4}}
> 
>           }
> 
>           child[sibling distance=20mm] { node[label=below:6] (L4_4) {*}
> 
>            child { node[label=below:5] (L5_5) {A}}
> 
>            child { node[label=below:1] (L5_6) {2}}
> 
>           }
> 
>         }
> 
>       }
> 
>       child[sibling distance=45mm] { node[label=below:12] (L2_2) {*}
> 
>         child[sibling distance=15mm] { node[label=below:6] (L3_3) {*}
> 
>           child[sibling distance=10mm] { node[label=below:1] (L4_5) {2}}
> 
>           child[sibling distance=10mm] { node[label=below:5] (L4_6) {A}}
> 
>         }
> 
>         child[sibling distance=15mm] { node[label=below:6] (L3_4) {B}}
> 
>       }
> 
>     }
> 
>     child { node[label=below:14] (L1_2) {*}
> 
>       child[sibling distance=15mm] { node[label=below:8] (L2_3) {*}
> 
>         child[sibling distance=10mm] { node[label=below:3] (L3_5) {6}}
> 
>         child[sibling distance=10mm] { node[label=below:5] (L3_6) {A}}
> 
>       }
> 
>       child[sibling distance=15mm] { node[label=below:6] (L2_4) {B}}
> 
>     };
> 
>   \draw[swapme] (L5_3) to [bend right=15] (L5_4);
> 
>   \draw[swapme] (L5_5) to [bend right=15] (L5_6);
> 
>   \draw[swapme] (L4_1) to [bend right=15] (L4_2);
> 
>   \draw[swapme] (L4_3) to [bend right=15] (L4_4);
> 
>   \draw[swapme] (L3_1) to [bend right=15] (L3_2);
> 
>   \draw[swapme] (L3_3) to [bend right=15] (L3_4);
> 
>   \draw[swapme] (L2_1) to [bend right=15] (L2_2);
> 
>   \draw[swapme] (L2_3) to [bend right=15] (L2_4);
> 
>   \draw[swapme] (L1_1) to [bend right=15] (L1_2);
> 
> \end{tikzpicture}
> 
> 
> 
> 
> 
> 
> 
> Hello,
> 
> 
> 
> I started using PGF/TKIZ yesterday for drawing an unbalanced expression tree
> with weights. I started from examples found in the web and thus learned by
> doing. Later I found this very great over 1276 pages manual and started to
> read, and the bits started to fit together and make sense an I was able to
> start styling. I admit of course that I did not read all 1276 pages since
> yesterday evening, but searched quite a lot in the manual and via google,
> not finding any solution for my problem.
> 
> 
> 
> I have an unbalanced tree of 5 levels which has a problem with overlapping
> nodes, trying to fix this with level based sibling sizes does not work
> because it either becomes too wide or the nodes overlap. The solution would
> be to change sibling distance for certain siblings only.
> 
> 
> 
> In the example below there are 2 nodes overlapping completely in level 3.
> The solution would be simply to change the sibling distance between node "*"
> and node "B" on that level, making it smaller, as this branch stops at level
> 4. But I found no way to achieve this. The same way I would adjust upper
> levels distances for a more compact tree. But maybe there is a more elegant
> way?
> 
> 
> 
> 
> 
> Thanks,
> 
> Gaston
> 
> 
> 
> Any solution available for this case?
> 
> 
> 
> P.S.: please do not suggest to rearrange the tree for balancing as this is
> not possible in this case!
> 
> P.P.S: The red lines are not linked to this problem, they are related to the
> tree usage.
> 
> 
> 
> Thanks,
> 
> Gaston
> 
> 
> 
> 
> 
> 
> 
> 
> 
> \begin{tikzpicture}[level/.style={sibling distance=80mm},
> 
>                     level 1/.style={sibling distance=80mm},
> 
>                     level 2/.style={sibling distance=55mm},
> 
>                     level 3/.style={sibling distance=25mm},
> 
>                     level 4/.style={sibling distance=17mm},
> 
>                     level 5/.style={sibling distance=10mm},
> 
>                     swapme/.style={<->,red,thick},
> 
>                     every node/.style = {shape=circle,
> 
>                       draw, align=center},
> 
>                     every
> label/.style={shape=rectangle,draw=none,fill=none,font=\tiny}
> 
>                     ]]
> 
>   \node {+}
> 
>     child{ node[label=below:41] (L1_1) {+}
> 
>       child{ node[label=below:29] (L2_1) {+}
> 
>         child{ node[label=below:15] (L3_1) {*}
> 
>           child{ node[label=below:9] (L4_1) {*}
> 
>             child{ node[label=below:4] (L5_1) {8}}
> 
>             child{ node[label=below:5] (L5_2) {A}}
> 
>           }
> 
>           child{ node[label=below:6] (L4_2) {B}}
> 
>         }
> 
>         child { node[label=below:14] (L3_2) {*}
> 
>           child { node[label=below:8] (L4_3) {*}
> 
>             child { node[label=below:6] (L5_3) {B}}
> 
>             child { node[label=below:2] (L5_4) {4}}
> 
>           }
> 
>           child { node[label=below:6] (L4_4) {*}
> 
>            child { node[label=below:5] (L5_5) {A}}
> 
>            child { node[label=below:1] (L5_6) {2}}
> 
>           }
> 
>         }
> 
>       }
> 
>       child { node[label=below:12] (L2_2) {*}
> 
>         child { node[label=below:6] (L3_3) {*}
> 
>           child { node[label=below:1] (L4_5) {2}}
> 
>           child { node[label=below:5] (L4_6) {A}}
> 
>         }
> 
>         child { node[label=below:6] (L3_4) {B}}
> 
>       }
> 
>     }
> 
>     child { node[label=below:14] (L1_2) {*}
> 
>       child { node[label=below:8] (L2_3) {*}
> 
>         child { node[label=below:3] (L3_5) {6}}
> 
>         child { node[label=below:5] (L3_6) {A}}
> 
>       }
> 
>       child { node[label=below:6] (L2_4) {B}}
> 
>     };
> 
>  \draw[swapme] (L5_3) -- (L5_4);
> 
>   \draw[swapme] (L5_5) -- (L5_6);
> 
>   \draw[swapme] (L4_1) -- (L4_2);
> 
>   \draw[swapme] (L4_3) -- (L4_4);
> 
>   \draw[swapme] (L3_1) -- (L3_2);
> 
>   \draw[swapme] (L3_3) -- (L3_4);
> 
>   \draw[swapme] (L2_1) -- (L2_2);
> 
>   \draw[swapme] (L2_3) -- (L2_4);
> 
>   \draw[swapme] (L1_1) -- (L1_2);
> 
> \end{tikzpicture}
> 
> 
> 
> 
> 


More information about the pgf-tikz mailing list.