![如何垂直对齐 tikz 图中标签](https://linux22.com/image/250384/%E5%A6%82%E4%BD%95%E5%9E%82%E7%9B%B4%E5%AF%B9%E9%BD%90%20tikz%20%E5%9B%BE%E4%B8%AD%E6%A0%87%E7%AD%BE.png)
我在 TikZ 中绘制了带有对角箭头的上图,但标签看起来像是放在曲线上而不是彼此相邻。我想我明白为什么它们会这样放置:TikZ 将标签放在箭头中间,而对角箭头的末端略低。但我无法解决这个问题。
我为此图编写的代码是:
\documentclass[border=1]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,arrows}
\begin{document}
\begin{tikzpicture}
[
arr/.style={->,font=\scriptsize},
description/.style={fill=white,inner sep=2pt}
]
\matrix (m) [
matrix of math nodes, row sep=3em, column sep=4em, text height=1.5ex, text depth=0.25ex
]
{A[f]_0:=A & A[f]_1 & A[f]_2 & \cdots \\
& & B \\};
\draw[arr] (m-1-1) -- node[above] {$\pi_0$} (m-1-2);
\draw[arr] (m-1-1) -- node[description] {$f_0:=f$} (m-2-3);
\draw[arr] (m-1-2) -- node[above] {$\pi_1$} (m-1-3);
\draw[arr] (m-1-2) -- node[description] {$f_1$} (m-2-3);
\draw[arr] (m-1-3) -- node[above] {$\pi_2$} (m-1-4);
\draw[arr] (m-1-3) -- node[description] {$f_2$} (m-2-3);
\draw[arr] (m-1-4) -- node[description] {$f_3$} (m-2-3);
\end{tikzpicture}
\end{document}
所以问题是:我怎样才能将标签$f_0:=f$
、$f_1$
和$f_2$
放在$f_3$
相同的高度,最好是的高度$f_2$
?
答案1
正如我在评论中所说的那样,您需要设置一条水平线。我选择f_2
它作为参考点,并在放置在路径上时为其命名
\draw[arr] (m-1-3) -- node[description] (f2) {$f_2$} (m-2-3)
水平线交叉点f2
声明为
\path[name path=pf2] (m-1-1|-f2)--(m-1-4|-f2);
然后你需要name
所有其他路径,如
\draw[arr,name path=pf0] (m-1-1) -- (m-2-3);
并在交叉点处放置标签
\node[arr,description,name intersections={of=pf0 and pf2}] (f0) at (intersection-1) {$f_0:=f$};
我的完整代码是
\documentclass[border=1]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,arrows,intersections}
\begin{document}
\begin{tikzpicture}
[
arr/.style={->,font=\scriptsize},
description/.style={fill=white,inner sep=2pt}
]
\matrix (m) [
matrix of math nodes, row sep=3em, column sep=4em, text height=1.5ex, text depth=0.25ex
]
{A[f]_0:=A & A[f]_1 & A[f]_2 & \cdots \\
& & B \\};
\draw[arr] (m-1-1) -- node[above] {$\pi_0$} (m-1-2);
\draw[arr] (m-1-2) -- node[above] {$\pi_1$} (m-1-3);
\draw[arr] (m-1-3) -- node[above] {$\pi_2$} (m-1-4);
\draw[arr] (m-1-3) -- node[description] (f2) {$f_2$} (m-2-3);
\draw[arr,name path=pf0] (m-1-1) -- (m-2-3);
\draw[arr,name path=pf1] (m-1-2) -- (m-2-3);
\draw[arr,name path=pf3] (m-1-4) -- (m-2-3);
\path[name path=pf2] (m-1-1|-f2)--(m-1-4|-f2);
\draw[red] (m-1-1|-f2)--(m-1-4|-f2);
\node[arr,description,name intersections={of=pf0 and pf2}] (f0) at (intersection-1) {$f_0:=f$};
\node[arr,description,name intersections={of=pf1 and pf2}] (f1) at (intersection-1) {$f_1$};
\node[arr,description,name intersections={of=pf3 and pf2}] (f3) at (intersection-1) {$f_3$};
\end{tikzpicture}
\end{document}
结果