5

This is the code I have:

\begin{tikzpicture}
\tikzset{edge from parent/.style=
{draw,
    edge from parent path={(\tikzparentnode.south)
                            -- +(0,-5pt)
                            -| (\tikzchildnode)}}}

\Tree   [.Fibres
        [.Natural
            [.Animal Silk
            ]
            [.Vegetable
                [.Cotton\\Linen\\Hemp\\Ramie\\Jute ]
            ]
            [.Mineral   Asbestos ]
        ]
        [.Man-made
            [.{Natural Polymers} 
                [.XYZ ]
                [.XYZ ]
                [.XYZ ]
            ]
            [.{Synthetic Polymers}
                [.XYZ ]
                [.XYZ ]
            ]
        ]
    ]

\end{tikzpicture}

which gives me a good tree. But I want to have "Cotton Linen Hemp Ramie Jute" displayed one below another instead of right next to each other. Can someone help?

Harald
  • 1,349
  • Welcome to TeX.SE! I edited your question title to describe your question more clearly. Things like "how to do this" could stand for virtually anything and doesn't help to spur interest or find the question if one has the same problem. Feel free to change it again. – Harald Jan 05 '15 at 15:30

2 Answers2

8

forest is a powerful tree-drawing tool, although Jesse's answer has the advantage of simplicity in this case. However, for what it is worth, here is the same tree with forest:

\documentclass[tikz, border=5pt]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
  for tree={
    align=center,
    edge path={
      \noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) -- +(0,-15pt) -| (.child anchor)\forestoption{edge label};
    },
    if n=2{
      if ={equal(n_children("!u"),3)}{calign with current}{}}{},
  }
  [Fibres
        [Natural
            [Animal
              [Silk
              ]
            ]
            [Vegetable
                [Cotton\\Linen\\Hemp\\Ramie\\Jute
                ]
            ]
            [Mineral
              [Asbestos
              ]
            ]
        ]
        [Man-made
            [Natural Polymers
                [XYZ
                ]
                [XYZ
                ]
                [XYZ
                ]
            ]
            [Synthetic Polymers
                [XYZ
                ]
                [XYZ
                ]
            ]
        ]
    ]
\end{forest}
\end{document}

forest alignments

cfr
  • 198,882
2

Something like this. Define a local node that has its own style, displayed below.

\tikzset{my node/.style={text width=2cm,align=center}}  % left or right are available

Furthermore, the vegetable was not centered and that is improved by setting equal length for the neighbors, like

\makebox[\widthof{Vegetable}]{Animal}

enter image description here

Code

\documentclass[a4paper,oneside,article]{memoir}

\usepackage{tikz-qtree}
\usetikzlibrary{trees}
\usepackage{calc}

\begin{document}

OP's result ---

\begin{tikzpicture}
\tikzset{edge from parent/.style=
{draw,
    edge from parent path={(\tikzparentnode.south)
                            -- +(0,-5pt)
                            -| (\tikzchildnode)}}}

\Tree   [.Fibres
        [.Natural
            [.Animal Silk
            ]
            [.Vegetable
                [.Cotton\\Linen\\Hemp\\Ramie\\Jute ]
            ]
            [.Mineral   Asbestos ]
        ]
        [.Man-made
            [.{Natural Polymers} 
                [.XYZ ]
                [.XYZ ]
                [.XYZ ]
            ]
            [.{Synthetic Polymers}
                [.XYZ ]
                [.XYZ ]
            ]
        ]
    ]

\end{tikzpicture}


\medskip

Proposed result ---

\tikzset{my node/.style={text width=2cm,align=center}}


\tikzset{edge from parent/.style=
{draw,
    edge from parent path={(\tikzparentnode.south)
                            -- +(0,-5pt)
                            -| (\tikzchildnode)}}}
\begin{tikzpicture}
\Tree   [.Fibres
        [.Natural
            [.\makebox[\widthof{Vegetable}]{Animal} Silk
            ]
            [.Vegetable
                [.\node[my node]{                   % <--- here
                Cotton\\Linen\\Hemp\\Ramie\\Jute};
                ]
            ]
            [.\makebox[\widthof{Vegetable}]{Mineral}   Asbestos ]
        ]
        [.Man-made
            [.{Natural Polymers} 
                [.XYZ ]
                [.XYZ ]
                [.XYZ ]
            ]
            [.{Synthetic Polymers}
                [.XYZ ]
                [.XYZ ]
            ]
        ]
    ]

\end{tikzpicture}
\end{document}
Jesse
  • 29,686
  • Couldn't you just set align=center for the picture? – cfr Jan 05 '15 at 13:37
  • @cfr Are you referring to the centering problem of Vegetable that I did not observed until you mentioned it. – Jesse Jan 05 '15 at 14:22
  • I just wondered if you needed a special node style or whether you could just apply align=center to the lot. Since the cotton/linen/... node includes explicit line breaks, the width specification might not really be needed? [Whatever problem you noticed with Vegetable on account of my comment was in no way due to any insight of mine ;).] Afraid I forgot to (+1) earlier... – cfr Jan 05 '15 at 15:21
  • I learned that special node from reading the manual; without text width specification will cause messy overlapping. As always, your comments make me think and learn. Thanks. – Jesse Jan 05 '15 at 15:43
  • Not sure about that ;). Interesting about the overlapping. Your current code doesn't compile for me, though. \widthof is undefined? – cfr Jan 05 '15 at 16:13
  • 2
    You need \usepackage{calc}. Sorry, forgot to add this package after adding new feature. – Jesse Jan 05 '15 at 16:18
  • @ShyamSrinivasaraghavan Please consider accepting this answer by clicking on the greyed-out check mark to the top left of the answer. This helps other users find it, gives the author points and also gives you points. – cfr Jan 05 '15 at 23:35
  • @ShyamSrinivasaraghavan You are welcome. – Jesse Jan 06 '15 at 00:47