在 Tikz 中沿不同路径对齐节点

在 Tikz 中沿不同路径对齐节点

这里有一个简单的示例,其中有三个节点和连接它们的箭头,箭头上有标签。但是,我希望标签垂直对齐(即,我想将 2 个标签向上移动,使其与 1 个标签具有相同的垂直位置,但仍与其自己的箭头重叠)。

\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning,shapes}
\begin{document}
\begin{tikzpicture}
\node at (0,0) [rectangle,draw] (a) {$a$};
\node at (-1,-1) [rectangle,draw] (b) {$b$};
\node at (1,-2) [rectangle,draw] (c) {$c$};
\draw [->,>=latex] (a) -> node [] (l1) {1} (b);
\draw [->,>=latex] (a) -> node [] (l2) {2} (c);
\end{tikzpicture}
\end{document} 

第一个例子

答案1

Z 库intersections在这里可能很有用。所需要的只是一条从 水平延伸的“辅助”路径,该路径与从到 的b路径相交——在交叉点放置“2”即可完成图形。简单且相当自动化。对于特定情况,这种方法肯定需要进行一些调整。ac

顺便说一句,rectanglecircle形状即使没有库也始终可用shapes,并且rectangle默认用于nodes。节省了一些击键。

\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning,intersections}
\begin{document}
\begin{tikzpicture}
\node at (0,0) [draw] (a) {$a$};
\node at (-1,-1) [draw] (b) {$b$};
\node at (1,-2) [draw] (c) {$c$};
\draw [->,>=latex] (a) -> node [inner sep=1pt,fill=white] (l1) {1} (b);
\draw [name path=ac,->,>=latex] (a) -> (c)coordinate(c1);
\path [name path=tmppath] (l1) -- (l1 -| c1);% helper path to get intersection
\node [fill=white,inner sep=1pt,name intersections={of=tmppath and ac}] at (intersection-1) {2};
\end{tikzpicture}
\end{document} 

输出

答案2

这幅图是一个有启发性的例子,可以用于元帖子-- 与之集成的绘图语言lualatex

\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);

  numeric u; u = 42;
  path a, b, c; 

  a = unitsquare shifted -(1/2, 1/2) scaled 1/3 u;
  b = a shifted (-u, -u);
  c = a shifted (u, -2u);

  forsuffixes s = a, b, c: 
    draw s;
    label("$" & str s & "$", center s);
  endfor

  ahangle := 30;
  drawarrow center a -- center b cutbefore a cutafter b;
  drawarrow center a -- center c cutbefore a cutafter c;

  z1 = 0.5 [center a, center b];
  z2 = whatever[center a, center c];
  y2 = y1;

  for i=1, 2:
    picture p; p = thelabel("$" & decimal i & "$", z[i]);
    unfill bbox p; draw p;
  endfor

endfig;
\end{mplibcode}
\end{document}

编译此示例以lualatex生成如下 PDF:

在此处输入图片描述

笔记

这里的方法是定义三个正方形<path>变量,ab和 ,并将c它们移到适当的位置。画出它们并标记它们,然后添加两个箭头。有趣的部分是这样的:

z1 = 0.5 [center a, center b];
z2 = whatever[center a, center c];
y2 = y1;

这定义了两个<pair>变量,z1z2z1定义为位于中心a和中心之间的中间bz2定义为某处a沿着从 的中心到 的中心的线c。然后在第三步中, 的 y 坐标z2设置为与 的 y 坐标相同z1。这正是 OP 想要的。MP 允许您拥有这样的部分方程,前提是在您尝试绘制它们之前定义所有点。

答案3

代码

\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta} % “arrows” is deprecated
\makeatletter
\tikzset{
  on height of/.code={%
    \pgf@process{\tikz@scan@one@point\pgfutil@firstofone(#1)}\pgf@ya=\pgf@y
    \tikz@timer@start \pgf@yb=\pgf@y \tikz@timer@end \pgf@yc=\pgf@y
    \pgfmathsetmacro\tikz@time{(\pgf@ya-\pgf@yb)/(\pgf@yc-\pgf@yb)}}}
\makeatother

\tikzset{node on line/.style={inner sep=+.15em, fill=white}}
\begin{document}

%% manually via “intersection of”
\begin{tikzpicture}[> = Latex]
\path[nodes=draw] node at ( 0, 0) (a) {$a$}
                  node at (-1,-1) (b) {$b$}
                  node at ( 1,-2) (c) {$c$};
\path[->] (a) edge node (l1) {1} (b)
              edge               (c)
 node at (intersection of l1--{[xshift=1cm]l1} and a--c) (l2) {2};
\end{tikzpicture}

%% using “on height of”
\begin{tikzpicture}[> = Latex]
\path[nodes=draw] node at ( 0, 0) (a) {$a$}
                  node at (-1,-1) (b) {$b$}
                  node at ( 1,-2) (c) {$c$};
\path[->, nodes=node on line]
  (a) edge node                  (l1) {1} (b)
      edge node[on height of=l1] (l2) {2} (c);
\end{tikzpicture}

%% auto placement will need help
\begin{tikzpicture}[> = Latex, auto=right]
\path[nodes=draw] node at ( 0, 0) (a) {$a$}
                  node at (-1,-1) (b) {$b$}
                  node at ( 1,-2) (c) {$c$};
\path[->, inner sep=+.15em]
  (a) edge node                              (l1) {1} (b)
      edge node[swap, on height of=l1.south] (l2) {2} (c);
\end{tikzpicture}
\end{document}

输出

在此处输入图片描述

答案4

这种方法与其他方法一样好或坏:

  • 使用 pos 参数
  • 根据需要确定其值

pos=0.185实验发现...只要你不能读出坐标(a)(b)...

结果

\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes}
\begin{document}
 \begin{tikzpicture}[
    >=latex
 ]
    \node at ( 0, 0)    [rectangle,draw] (a) {$a$};
    \node at (-1,-1)    [rectangle,draw] (b) {$b$};
    \node at ( 1,-2)    [rectangle,draw] (c) {$c$};
    \draw [->] (a) -- node [pos=.5] (l1) {1} (b);
    \draw [->] (a) -- node [pos=.185] (l2) {2} (c);
 \end{tikzpicture}
\end{document} 

相关内容