7

I want to left align the "2222" node with the other nodes on the same level (see MWE below). Is this possible using the graphdrawing library of TikZ 3.0? I could not work it out so far.

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{graphdrawing}
\usetikzlibrary{graphs}
\usegdlibrary{trees}
\begin{document}
\begin{tikzpicture}[>=stealth,every node={align=left}]
\graph [tree layout, grow'=right, fresh nodes, level distance=0.5in,
sibling distance=0.1in]
    {
        4 -> { 
          3 -> { 1 -> { 5, " " }, 2,2 },
          3 -> { 1, 2, 2 },
          3 -> { 1, 2, 2222 }
        } 
    };
\end{tikzpicture}
\end{document}

MWE with "2222" not aligned to other nodes on same level

  • You should rather put a solution in an answer rather than a question to keep things tidy and make it easy for people to find things. It is fine to answer your own question. – cfr Jan 12 '16 at 21:43
6

Instead every node={align=left} in tikzpicture parameters you should say:

every node/.style={anchor=west}

Result:

enter image description here

Upgrade: To my taste, regarding to you edited questions, the following solution looks nicer:

\graph [tree layout, grow'=right, 
        fresh nodes, level distance=0.5in,
        sibling distance=0.1in
                    ]
    {
        4 -> {
          3 -> { 1 -> { 5, " " }, 2,2 },
          3 -> { 1, 2, 2 },
          3 -> [head anchor=west]{ 1, 2, 2222}
        }
    };

It gives: enter image description here

  • Thank you very much for your solution. I should have known this! Now I have got a new problem: How can I align the arrow from "3" to "2222" to point to this anchor instead of the node's center? – Simon Schälli Jan 12 '16 at 20:46
  • @SimonSchälli, I'm not sure, if >I understand you correctly. Arrow to node 2222 has different outgoing angle as at nodes 2. This can be compensated by use of 3 -> { 1, 2, 2222[nudge=(up:-2mm)] } instead 3 -> { 1, 2, 2222 }. Try if this gives what you looking for. – Zarko Jan 12 '16 at 21:10
  • 1
    You did understand me correctly. Thanks again for your solution. I did just find another possibility. Adding "head anchor=west" to the graph parameters does what I want. I will add this to my original post. – Simon Schälli Jan 12 '16 at 21:30
  • @SimonSchälli, I'm glad that you find solution you liked. I still learn graphs and graphsdriwing ... – Zarko Jan 12 '16 at 21:45
  • @SimonSchälli Your upgraded solution does not work for me only every node/.style={anchor=west}. I am on TeXLive 2018: pgf 2015/08/07 v3.0.1a (rcs-revision 1.15), tikzlibrarygraphs.code.tex 2015/06/05 v3.0.1a (rcs-revision 1.44). Can you confirm your local style adjustment 3 -> [head anchor=west]{ 1, 2, 2222} still works? – Hotschke May 19 '18 at 16:01
2

Thanks to Zarko I was able to get a working solution:

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{graphdrawing}
\usetikzlibrary{graphs}
\usegdlibrary{trees}
\begin{document}
\begin{tikzpicture}[>=stealth,every node/.style={anchor=west}]
\graph [tree layout, grow'=right, fresh nodes, level distance=3cm,
sibling distance=2ex, head anchor=west]
    {
        4 -> { 
          3 -> { 1 -> { 5, " " }, 2,2 },
          3 -> { 1, 2, 2 },
          3 -> { 1, 2, 2222 }
        } 
    };
\end{tikzpicture}
\end{document}

gives

MWE Solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.