这里有一个简单的示例,其中有三个节点和连接它们的箭头,箭头上有标签。但是,我希望标签垂直对齐(即,我想将 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”即可完成图形。简单且相当自动化。对于特定情况,这种方法肯定需要进行一些调整。a
c
顺便说一句,rectangle
和circle
形状即使没有库也始终可用shapes
,并且rectangle
默认用于node
s。节省了一些击键。
\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>
变量,a
、b
和 ,并将c
它们移到适当的位置。画出它们并标记它们,然后添加两个箭头。有趣的部分是这样的:
z1 = 0.5 [center a, center b];
z2 = whatever[center a, center c];
y2 = y1;
这定义了两个<pair>
变量,z1
和z2
。 z1
定义为位于中心a
和中心之间的中间b
;z2
定义为某处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}