在 TikZ 中,是否可以将节点放置在路径上的特定距离处。我知道有
\draw (A) -- node[pos=<f>, above] {$+$} (B);
这将在路径的某个部分放置一个节点。但是,我想在从到 的f
路径结束之前放置一个节点,比如说 10pt 。在 TikZ 中执行此操作的最简单方法是什么?(A)
(B)
最小示例
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\node (A) {$A$};
\node [right=of A] (B) {$B$};
\draw [->] (A) -- node[pos=0.9, above] {$+$} (B);
\end{tikzpicture}
\end{document}
答案1
答案2
如果你不介意装饰库依赖,那么
\documentclass[tikz]{standalone}
\usetikzlibrary{positioning, decorations}
\makeatletter
\tikzset{
distance from start/.code={%
\pgfgetpath\currentpath\pgfprocessround{\currentpath}{\currentpath}%
\pgf@decorate@parsesoftpath{\currentpath}{\currentpath}%
\pgfmathparse{#1/\pgf@decorate@totalpathlength}\tikzset{pos=\pgfmathresult}},
distance from end/.code={%
\pgfgetpath\currentpath\pgfprocessround{\currentpath}{\currentpath}%
\pgf@decorate@parsesoftpath{\currentpath}{\currentpath}%
\pgfmathparse{1-(#1/\pgf@decorate@totalpathlength)}\tikzset{pos=\pgfmathresult}}
}
\makeatother
\begin{document}
\begin{tikzpicture}
\node (A) {$A$};
\node [right=of A] (B) {$B$};
\draw(A) -- node[distance from start=10pt, above] {$+$}
node[distance from end=5pt, below] {$\times$} (B);
\draw[->, red] (A.east) -- ++(10pt,0); % Test distances
\draw[->, blue] (B.west) -- ++(-5pt,0);
% test curve
\draw[green](A) to[in=45,out=225]
node[distance from start=10mm, inner sep=1pt,circle,fill] {}
node[distance from end=10mm, inner sep=1pt,circle,fill] {} (B);
\end{tikzpicture}
\end{document}
答案3
使用calc
库:
\path ($(B)!10pt!(A)$) node [below, red]{$*$};
应该可以工作(沿线10pt (B)
)(A)
。
答案4
如果路径在水平线上,则可以使用xshift
:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\node (A) {$A$};
\node [right=of A] (B) {$B$};
\draw [->] (A) -- (B.west) node[above,xshift=-10pt] {$+$};
\end{tikzpicture}
\end{document}
注意:(B.west)
如果(B)
希望新节点位于路径的尽头(正如你的问题所说)而不是在中心的左边(B)
。