如何确定树图的“动态中点”?

发布于 2025-08-06 02:14:22

A

如何保证上图中红字所在的结点,处在上下相邻的两个cyan小圆点的绝对中间位置?

当更改level distance后,由于红字结点的位置是一个绝对位置(A.south)的绝对偏移量(-.55cm),要保证它处在绝对中间位置,应当使用相对位置,或者说红字所在的结点很有可能是一个预定义的结点。

因太久没看文档,(如果存在,)我忘记了它的名字了。感觉如果能找出来,就可以用两定点的中点语法($(A)!.5!(B)$)来表示红字结点的位置。重点在于观察这个树状图的连接线是如何在向下后分别向左和向右拐弯。

如果并不存在这样的预定义结点,请问应该如何使之实现?

\documentclass[border=2pt,tikz]{standalone}
\usetikzlibrary{trees}
\tikzset{
    every node/.style={draw,inner sep=2pt},
    level/.style={level distance=1.5cm},
    level 1/.style={sibling distance=4.5cm},
    level 2/.style={sibling distance=2cm}
}
\begin{document}
\begin{tikzpicture}
    \node (A) {A} 
    [edge from parent fork down]
    child {node {A.1} 
           child {node {A.1.1}}
           child {node {A.1.2}}
          }
    child {node {A.2}
           child {node {A.2.1}}
           child {node {A.2.2}}
          }
    ;
    \fill [blue] 
          ([yshift=-.275cm]A.south) circle (1pt);
    \fill [cyan]     
          (A.center)                circle (1pt)
          (A.south)                 circle (1pt)
          ([yshift=-.55cm]A.south)  circle (1pt);
    \node at ([yshift=-.275cm]A.south) 
          [scale=.375,font=\ttfamily,draw=none,text=red] 
          {hello};
\end{tikzpicture}
\end{document}

查看更多

关注者
0
被浏览
69
1 个回答
Sagittarius Rover
Sagittarius Rover 18小时前
我要成为Typst糕手/(ㄒoㄒ)/~~

maybe related: https://tex.stackexchange.com/questions/142296/tikz-tree-fork-down-edge-labels

但我看不太懂....


tikz-ext库中有很方便的<spec>:|-|-|-

image.png

\documentclass[border=2pt,tikz]{standalone}
\usetikzlibrary{trees,ext.paths.ortho}
\tikzset{
    every node/.style={draw,inner sep=2pt},
    level/.style={level distance=1.5cm},
    level 1/.style={sibling distance=4.5cm},
    level 2/.style={sibling distance=2cm}
}
\begin{document}
\begin{tikzpicture}
    \node (A) {A} 
    [edge from parent fork down]
    child {node (A1) {A.1}
           child {node {A.1.1}}
           child {node {A.1.2}}
          }
    child {node {A.2}
           child {node {A.2.1}}
           child {node {A.2.2}}
          }
    ;
    \path[magenta,draw] (A1.north) |-| (A.south) 
        node[pos=.25] {.25}
        node[pos=.5] {.5}
        node[pos=.75] {.75}
        node[pos=.875] {.875}
        coordinate[pos=.875] (P)
        ;
    \node[inner sep=0pt,circle,pin=above right:P] at (P) {};
\end{tikzpicture}
\end{document}

image.png

但这不一定是最佳实践....

撰写答案

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

发布
问题

分享
好友

手机
浏览

扫码手机浏览