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

Gaston Gloesener gaston.gloesener at web.de
Fri Apr 24 14:44:02 CEST 2020


Thanks Henri for your feedback. I am happy of course that my code looks good as it shows (to me) I did quickly understand the concepts of TKIZ. I will have a look at your suggests alternatives for extending my knowledge.

I don't sense tkiz tree code to be very verbose, I more feel a very good ratio of code vs flexibility. An of course no comparison to bare LaTex figures 😃 PGF/TIKZ is a really exceptionally good package!

Currently I stumbled over another problem on the same tree for which I could not find a straight solution, which is putting  a style on edge labels. For other labels I found and used the local option syntax label={[option]label} but this seems not to work for edge labels. In my tree due to the "every node" style all labels get put into circles. I solved this via the "label" style which seems not to impact edge labels and a "edge label" style did also not help, as well as local options as I said. I used bended paths between some nodes in my tree and had one special one which I wanted to label with a question mark.


For all such paths I used:

\draw[swapme] (L5_3) to [bend right=15] (L5_4);

So for this special one I changed to:

  \draw[swapme,red] (L3_3) to [bend right=15,edge label=$?$] (L3_4);

But this put the label read in a red circle and I found no method to change this style. I finally came up with some workaround vie curved path:

\draw[swapme,red] (L3_3) .. controls +(5mm,2mm) and +(-5mm,2mm) .. (L3_4) node[midway,above,shape=rectangle,draw=none] {$?$};

This is ok in this particular case because it is ok for this link to look differently to all other such paths while they all have the same purpose. If they would have to look same it would be good to know if there is a way to re-style an edge label?

No problem if you have to no time for this, I'll post it on stackexchange.

Cheers & Thanks,
Gaston






 

-----Original Message-----
From: pgf-tikz <pgf-tikz-bounces+gaston.gloesener=web.de at tug.org> On Behalf Of Henri Menke
Sent: Friday, April 24, 2020 02:04
To: pgf-tikz at tug.org
Subject: Re: [pgf-tikz] UPD: Dynamic Sibling distance on single level

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.