有人知道如何将文本、字母或符号对齐到线箭头中间的正上方,而不考虑线的长度吗?
它看起来应该与下图类似,但节点的位置不应该取决于线的长度。
\begin{tikzpicture}
\draw [-triangle 60] (0,0) -- (2,0) node[pos=.9,above] {$+$};
\draw [-triangle 60] (0,-1) -- (3,-1) node[pos=.9,above] {$+$};
\draw [-triangle 60] (0,-2) -- (2,-2) node[pos=.9,above] {$-$};
\draw [-triangle 60] (0,-3) -- (3,-3) node[pos=.9,above] {$-$};
\end{tikzpicture}
答案1
像这样:
这很简单:
\documentclass[border=3mm, tikz]{standalone}
\usetikzlibrary{arrows}
\begin{document}
\begin{tikzpicture}[
sgn/.style = {above left, inner xsep=0pt}
]
\draw [-triangle 60] (0,0) -- (2, 0) node[sgn] {$+$};
\draw [-triangle 60] (0,-1) -- (3,-1) node[sgn] {$+$};
\draw [-triangle 60] (0,-2) -- (2,-2) node[sgn] {$-$};
\draw [-triangle 60] (0,-3) -- (3,-3) node[sgn] {$-$};
\end{tikzpicture}
\end{document}
答案2
如果您使用当前推荐的语法而不是旧版本的箭头规范,则可以很好地实现自动化。这意味着使用库arrows.meta
而不是arrows
。
例如:
\tikzset{
tlabel/.style={
inner sep=0pt, anchor=center,above,at end, xshift=-.5*\tarrowlength,yshift=.5*\tarrowlength
},
tarrow/.code={
\tikzset{
tarrow length=#1,
-{Triangle[length=#1,width=#1]},
},
},
tarrow length/.store in=\tarrowlength,
tarrow length=10pt,
}
此代码允许您说
\draw [tarrow=20pt] (0,-2) -- (2,-2) node[tlabel] {$-$};
它将确保箭头尖端为 20pt x 20pt,并且末端的标签位于线尾上方 10pt 处和左侧 10pt 处,如果您希望它位于箭头上方的中心,我想这就是您想要的位置。
\documentclass[tikz,border=10pt,multi]{standalone}
\usetikzlibrary{arrows.meta}
\tikzset{
tlabel/.style={
inner sep=0pt, anchor=center,above,at end, xshift=-.5*\tarrowlength,yshift=.5*\tarrowlength
},
tarrow/.code={
\tikzset{
tarrow length=#1,
-{Triangle[length=#1,width=#1]},
},
},
tarrow length/.store in=\tarrowlength,
tarrow length=10pt,
}
\begin{document}
\begin{tikzpicture}
\draw [tarrow=10pt] (0,0) -- (2,0) node [tlabel] {$+$};
\draw [tarrow=20pt] (0,-1) -- (3,-1) node[tlabel] {$+$};
\draw [tarrow=20pt] (0,-2) -- (2,-2) node[tlabel] {$-$};
\draw [tarrow=10pt] (0,-3) -- (3,-3) node[tlabel] {$-$};
\end{tikzpicture}
\end{document}