tikz 的 level distance 可不可以更改一下计算方式:
就像上面这幅图那样, 第二次计算 这个 40pt 是按照蓝色这个长度计算的 ? MWE 如下:
\documentclass{article}
\usepackage{tikz}
\usepackage{tikz-qtree}
\begin{document}
\begin{tikzpicture}[
grow=right,
level distance=40pt,
sibling distance=10pt,
every tree node/.style={anchor=west, inner sep=0pt, outer sep=0pt, draw},
every level 0 node/.style={anchor=east, inner sep=0pt, outer sep=0pt},
edge from parent/.style={
draw,
edge from parent path={
(\tikzparentnode.east) %-- +(8pt, 0pt)
|- (\tikzchildnode.west)
}
},
]
\Tree
[.Root Node
[.AAA-1 ]
[.AAA-2 ]
[.AAA-BBB-X ]
[.AAA-3
[.BBB-3 ]
[.BBB-4 ]
]
]
\end{tikzpicture}
\end{document}
看了下源码,向右生长的时候,子树和父节点拼接时的基线是父节点的左端和子树的左端,所以它们各自左端的水平偏移是固定的level distance
(与示例图相符);向下生长时,子树的上端和父节点的上端之间是固定的level distance
;向左,向上也是类似的。
如果要使示例中蓝色部分为固定宽度(即父节点的右端和子树的左端相距固定宽度),拼接的时候需要将父节点的宽度也计算进去,可以重新定义\pgfsubtree
,在拼接前加上额外的偏移量即可。
\documentclass{article}
\usepackage{tikz}
\usepackage{tikz-qtree}
\usepackage{etoolbox}
\usepackage{scrlayer}
\DeclareNewLayer
[ topmargin, contents = \layercontentsmeasure,addheight=\dimexpr\headheight+\headsep\relax,
] {measurelayer}
\DeclareNewPageStyleByLayers{measurestyle}{measurelayer}
\pagestyle{measurestyle}
\makeatletter
\def\pgfsubtree#1#2{%
\let\parentnodename\nodename
\pgftree@savechildx=\pgftree@childx
\pgftree@savechildy=\pgftree@childy
\pgftree@savechildi=\pgftree@childi
% Build subpicture with all the children and their subtrees
{\pgftree@childx=0pt%
\pgftree@childy=0pt%
\pgftree@childi=0%
\process@children #2}%
\begin{pgfsubpicture}%
% Create node
#1%
\ifnum\pgftree@childi>0%
% Place children
{% center so that parent is midway between origins of first and last children
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 添加额外的偏移
\ifdefstring\leveldirection{right}{\pgftransformshift{\pgfpointdiff{\pgfpoint{\the\pgf@picminx}{0}}{\pgfpoint{\the\pgf@picmaxx}{0}}}}{\message{no right patch}}
\ifdefstring\leveldirection{left}{\pgftransformshift{\pgfpointdiff{\pgfpoint{\the\pgf@picmaxx}{0}}{\pgfpoint{\the\pgf@picminx}{0}}}}{\message{no left patch}}
\ifdefstring\leveldirection{down}{\pgftransformshift{\pgfpointdiff{\pgfpoint{0}{\the\pgf@picmaxy}}{\pgfpoint{0}{\the\pgf@picminy}}}}{\message{no down patch}}
\ifdefstring\leveldirection{up}{\pgftransformshift{\pgfpointdiff{\pgfpoint{0}{\the\pgf@picminy}}{\pgfpoint{0}{\the\pgf@picmaxy}}}}{\message{no up patch}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
\pgftransformshift{\pgfpointscale{-0.5}{\pgfqpoint{\the\pgftree@childx}{\the\pgftree@childy}}}%
\pgfplacesubpicture}%
% Draw the edges
{\pgftree@childi=0%
\process@edges #2}%
\fi
\end{pgfsubpicture}%
\global\pgftree@childi=\pgftree@savechildi
\global\pgftree@childx=\pgftree@savechildx
\global\pgftree@childy=\pgftree@savechildy
}
\makeatother
\begin{document}
\begin{tikzpicture}[
grow=right,
level distance=1cm,
sibling distance=10pt,
edge from parent/.style={
draw,
edge from parent path={
(\tikzparentnode.east) %-- +(0pt, 0pt)
|- (\tikzchildnode.west)
},
},
every tree node/.style={anchor=west, inner sep=0pt, outer sep=0pt, draw},
]
\Tree
[.Root Node
[.AAA-1 ]
[.AAA-2 ]
[.AAA-BBB-X
[.y-1 ]
[.y-2 ]
]
[.AAA-3
[.BBB-3 ]
[.BBB-4 ]
]
]
\end{tikzpicture}
\hspace{5em}
\begin{tikzpicture}[
grow=left,
level distance=1cm,
sibling distance=10pt,
edge from parent/.style={
draw,
edge from parent path={
(\tikzparentnode.west) %-- +(0pt, 0pt)
|- (\tikzchildnode.east)
},
},
every tree node/.style={anchor=east, inner sep=0pt, outer sep=0pt, draw},
]
\Tree
[.Root Node
[.AAA-1 ]
[.AAA-2 ]
[.AAA-BBB-X
[.y-1 ]
[.y-2 ]
]
[.AAA-3
[.BBB-3 ]
[.BBB-4 ]
]
]
\end{tikzpicture}
\par
\begin{tikzpicture}[
grow=down,
level distance=1cm,
sibling distance=10pt,
edge from parent/.style={
draw,
% edge from parent path={
% (\tikzparentnode.east) %-- +(0pt, 0pt)
% |- (\tikzchildnode.west)
% },
},
every tree node/.style={anchor=north, inner sep=0.5cm, outer sep=0pt, draw},
]
\Tree
[.Root Node
[.AAA-1 ]
[.AAA-2 ]
[.AAA-BBB-X
[.y-1 ]
[.y-2 ]
]
[.AAA-3
[.BBB-3 ]
[.BBB-4 ]
]
]
\end{tikzpicture}
\par
\begin{tikzpicture}[
grow=up,
level distance=1cm,
sibling distance=10pt,
edge from parent/.style={
draw,
% edge from parent path={
% (\tikzparentnode.east) %-- +(0pt, 0pt)
% |- (\tikzchildnode.west)
% },
},
every tree node/.style={anchor=south, inner sep=0.5cm, outer sep=0pt, draw},
]
\Tree
[.Root Node
[.AAA-1 ]
[.AAA-2 ]
[.AAA-BBB-X
[.y-1 ]
[.y-2 ]
]
[.AAA-3
[.BBB-3 ]
[.BBB-4 ]
]
]
\end{tikzpicture}
\end{document}
没研究过tikz-qtree
这个包,只测试了下原生的写法。或许有更好的实践。晚点再测试下。
\documentclass[tikz,border=2pt]{standalone}
% \usepackage{tikz-qtree}
\begin{document}
\begin{tikzpicture}[grow=right,
growth parent anchor=east,
every node/.style={draw,
inner sep=0pt,
outer sep=0pt},
level 1/.style={anchor=west,
level distance=2cm,
sibling distance=2cm},
level 2/.style={anchor=west,
level distance=2cm,
sibling distance=1cm},
edge from parent path={(\tikzparentnode.east) |-
(\tikzchildnode.west)}
]
\node [anchor=east] {Root}
child {node {A}
child {node {a1}}
child {node {a2}}}
child {node {BBBB}
child {node {b1}}
child {node {b2}}};
\draw [step=1cm,help lines] (0,-2) grid (6,2);
\end{tikzpicture}
\end{document}
上午翻了一会子文档和源码,发现
tikz-qtree
宏集的组成是:前两个是底层的,后两个是用户层的。
TikZ
自己处理树时,就像上面给出的测试一样,使用的是growth parent anchor
键,它决定了父结点的生长锚点,用来确定父结点与子结点的边界距离计算和布局问题。在这个提问中其值就应该是east
,这在TikZ
的树的写法中是可行的。TikZ
构建子结点时,因为可以使用growth parent anchor
这个机制,所以同级结点可以不用对齐,然而后果就是可能重叠。然而,将这个键传给tikz-qtree
后,什么也不会发生。有点懵……或许下一步该看看为什么tikz-qtree
的树不会重叠,或者说tikz-qtree
怎样处理growth parent anchor
……