我正在用 tikz 画一棵树,即使用 tikz-qtree-compat 来简化语法。树上可以有不同高度的节点,但分支会变得混乱。我想添加一条指令,其中母节点的南点和子节点的北点之间的距离是恒定的。
梅威瑟:https://www.overleaf.com/2453721thvhsh#/6394537/
\documentclass[10pt]{report}
\usepackage{tikz-qtree-compat} % draw trees with simple syntax
\begin{document}
wanted but obtained non-general way:
\begin{tikzpicture}[baseline=-20pt, grow=down]
\tikzset{level distance = 25pt, sibling distance = 30pt}
\tikzset{every tree node/.style={align=center,anchor=north}}
\tikzset{level 1/.style={level distance=38pt}}
\tikzset{level 2/.style={level distance=60pt}}
\tikzset{level 4/.style={level distance=60pt}}
\Tree
[.\node{A\\A};
[.\node{B\\B\\B\\B};
[.\node{C};
[.\node{E\\E\\E};]
]
[.\node{D\\D\\D};
[.\node at(0,-25pt){F\\F};
[.\node{G};]
]
]
]
]
\end{tikzpicture}
without any modification looks like:
\begin{tikzpicture}[baseline=-20pt, grow=down]
\tikzset{level distance = 25pt, sibling distance = 30pt}
\tikzset{every tree node/.style={align=center,anchor=north}}
%\tikzet{some single instruction that fix all trees in this way}
\Tree
[.\node{A\\A};
[.\node{B\\B\\B\\B};
[.\node{C};
[.\node{E\\E\\E};]
]
[.\node{D\\D\\D};
[.\node{F\\F};
[.\node{G};]
]
]
]
]
\end{tikzpicture}
\end{document}
答案1
和forest
你可以用不太复杂的语法得到你想要的东西:
\documentclass[10pt]{report}
\usepackage{forest}
\begin{document}
\begin{forest}
[A\\A, for tree={parent anchor=south, child anchor=north, align=center, s sep=5mm}
[B\\B\\B\\B
[C [E\\E\\E]]
[D\\D\\D [F\\F [G]]]
]
]
\end{forest}
\end{document}
答案2
这是Ignasi 的解决方案它试图说明将树转换为的过程forest
以及如何以等效的方式将树与周围文本对齐。
\documentclass{article}
\usepackage{forest}
\begin{document}
a forest solution:
\begin{forest}
for tree={
align=center,% will apply to every node in the tree, enables multiline nodes with centred alignment of text
parent anchor=south,% required to emulate the behaviour of the target tree
child anchor=north,% required to emulate the behaviour of the target tree
s sep=30pt,% equivalent of setting sibling distance, I think
l sep=10pt,% similar to setting level distance, I think
}
[A\\A, anchor=center, baseline% you need to specify the baseline differently to align the tree in a particular way with the surrounding text
[B\\B\\B\\B
[C
[E\\E\\E]
]
[D\\D\\D
[F\\F
[G]
]
]
]
]
\end{forest}
\end{document}