看了下源码,向右生长的时候,子树和父节点拼接时的基线是父节点的左端和子树的左端,所以它们各自左端的水平偏移是固定的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-qtrees 中 level distance 的计算方式