2

TLDR: I would like the coordinate shift specified at the start of a Tikzpicture to be interpreted relative to the western edge of the node above, rather than the center, but couldn't find how.

Hi,

I am trying to create a horizontally growing Tikz tree in LaTeX with text at the nodes, and as the tree has quite a number of levels and some of the text can be somewhat long, I want to be as sparingly as possible with horizontal space.

I have set everything up more or less as I want it, but the problem is that the coordinate shift I set up for each new level seems to begin at the center of the text field, which means the horizontal shift for a new level can be much further than desired for wide text boxes. I would like to be able to set the coordinate shift relative to the west edge of the text boxes. Just setting anchor to west doesn't seem to work, because the line just elongates horizontally.

Here's a MWE:

\documentclass[12pt]{article}

\usepackage{tikz}
    \usetikzlibrary{trees}

\begin{document}

\tikzstyle{every node}=[draw=black,thick,anchor=west]

\begin{tikzpicture}[
    grow via three points={one child at (0.5,-0.7) and
    two children at (0.5,-0.7) and (0.5,-1.4)},
    edge from parent path={(\tikzparentnode.west) |- (\tikzchildnode.west)}]
    \node[anchor=west]{}
        child { node[anchor=west] {Text}}
        child { node[anchor=west] {Quite a long text here}
            child { node[anchor=west] {Another long text for a wide box}
                child { node[anchor=west] {More text}}
            }
        };
\end{tikzpicture}

\end{document}

This produces the following result: enter image description here

As you can see, the lower two text boxes are shifted horizontally by the desired amount (compare "Text" relative to the root node) relative to the center of the above level, which means they still extend very far, horizontally. Is there any way I can make the coordinate shift be interpreted as relative to the left/western edge of the above level's text box?

Thanks!

3

like this?

enter image description here

with forest package this is simple to obtain:

\documentclass[margin=3mm]{standalone}
\usepackage[edges]{forest}

\begin{document}
    \begin{forest}
    for tree={grow'=0,  % direction
              folder,   % show folder organization
              draw,     % nodes have borders
              l sep'=6mm
              }
    [
      [A
        [Text]
        [Quite a long text here
          [Another long text for a wide box
            [More text]
          ]
        ]
      ]
    ]
    \end{forest}
\end{document}
  • Wow, that looks so much simpler! And yes, that is precisely what I was looking for. Thank you very much! – Vincent Oostelbos Mar 9 '18 at 15:45
  • l sep'=6mm is quicker. – cfr Mar 10 '18 at 0:17
  • @cfr, is difference at this simple short code observable? anyway, i corrected my answer. thank you very much. – Zarko Mar 10 '18 at 1:02
  • No. Well, technically, I guess it depends on processing speed and what is doing the noticing but, no. Your answer wasn't incorrect by any means: it was just a comment ;). – cfr Mar 10 '18 at 2:02

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.