10 更改 tikz-qtrees 中 level distance 的计算方式

发布于 2025-09-11 18:03:12

tikz 的 level distance 可不可以更改一下计算方式:

image.png

就像上面这幅图那样, 第二次计算 这个 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}

查看更多

关注者
0
被浏览
210
M
M 2天前
这家伙很懒,什么也没写!

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

局部截取_20250913_005317.png

2 个回答
远方不远
远方不远 3天前
Hello, LuaLaTeX!

没研究过tikz-qtree这个包,只测试了下原生的写法。或许有更好的实践。晚点再测试下。
Root

\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}

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览