这是我的代码:
\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}
这给了我一棵好树。但我想让“棉麻麻麻黄麻”一个接一个地显示,而不是紧挨着一个。有人能帮忙吗?
答案1
forest
是一个强大的树形绘制工具,尽管杰西的回答在这种情况下具有简单的优点。然而,值得一提的是,下面是具有 的同一棵树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}
答案2
就像这样。定义一个具有自己样式的本地节点,如下所示。
\tikzset{my node/.style={text width=2cm,align=center}} % left or right are available
此外,vegetable
没有居中,可以通过为邻居设置相等的长度来改进,例如
\makebox[\widthof{Vegetable}]{Animal}
代码
\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}