装饰的语法at position=<value>
显然使得装饰的定位依赖于路径长度:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\begin{document}
\begin{tikzpicture}[decoration={
markings,
mark=at position 0.4
with {\node[draw=blue,fill=blue,inner sep=2pt] {};},
mark=at position 0.6
with {\node[circle,draw=green,fill=green,inner sep=2pt] {};}
}
]
\node (A) {A};
\node at (1,0) (B) {B};
\draw[magenta,postaction=decorate] (A) .. controls(0.25,2) and (0.75,-2) .. (B);
\begin{scope}[xshift=3cm]
\node (A) {A};
\node at (1,0) (B) {B};
\draw[magenta,postaction=decorate] (A) .. controls(0.25,4) and (0.75,-4) .. (B);
\end{scope}
\begin{scope}[xshift=6cm]
\node (A) {A};
\node at (1,0) (B) {B};
\draw[magenta,postaction=decorate] (A) .. controls(0.25,7) and (0.75,-7) .. (B);
\end{scope}
\end{tikzpicture}
\end{document}
由于三条洋红色路径的长度不断增加,标记之间的距离也会越来越远。有没有办法放置两个装饰物,使它们之间的距离保持不变(即,将两个装饰物放置在路径的一半处,但保证它们在50pt
路径上始终彼此远离)?
答案1
用于\pgfdecoratedpathlength
计算精确的绝对距离:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\begin{document}
\begin{tikzpicture}[decoration={
markings,
mark=at position 0.5*\pgfdecoratedpathlength-25pt
with {\node[draw=blue,fill=blue,inner sep=2pt] {};},
mark=at position 0.5*\pgfdecoratedpathlength+25pt
with {\node[circle,draw=green,fill=green,inner sep=2pt] {};}
}
]
\node (A) {A};
\node at (1,0) (B) {B};
\draw[magenta,postaction=decorate] (A) .. controls(0.25,2) and (0.75,-2) .. (B);
\begin{scope}[xshift=3cm]
\node (A) {A};
\node at (1,0) (B) {B};
\draw[magenta,postaction=decorate] (A) .. controls(0.25,4) and (0.75,-4) .. (B);
\end{scope}
\begin{scope}[xshift=6cm]
\node (A) {A};
\node at (1,0) (B) {B};
\draw[magenta,postaction=decorate] (A) .. controls(0.25,7) and (0.75,-7) .. (B);
\end{scope}
\end{tikzpicture}
\end{document}
答案2
脚步:
- 利用 key 获取路径的总
/pgf/decoration/mark info/distance from start
长度- 保存此
\@totallength
并使其成为全局 at position 1
并非每次都有效(如之前在 TeX.sx 上发现的那样),因此使用距离末端很短的距离;这里-.01pt
有效,-.001pt
但第一条路径无效
- 保存此
- 用于
.5*\@totallength-25pt
第一个节点和 - 用于
.5*\@totallength+25pt
第二次
代码
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\makeatletter
\tikzset{%
decorate me/.style={
decoration={
markings,
mark=at position -.01pt with {%
\pgfkeysgetvalue{/pgf/decoration/mark info/distance from start}\@totallength
\global\let\@totallength\@totallength
},
mark=at position .5*\@totallength-25pt with {\node[draw=blue,fill=blue,inner sep=2pt] {};},
mark=at position .5*\@totallength+25pt with {\node[circle,draw=green,fill=green,inner sep=2pt] {};}
},
postaction=decorate
}
}
\makeatother
\begin{document}
\begin{tikzpicture}
\node (A) {A}; \node at (1,0) (B) {B};
\draw[magenta, decorate me] (A) .. controls(0.25,2) and (0.75,-2) .. (B);
\begin{scope}[xshift=3cm]
\node (A) {A}; \node at (1,0) (B) {B};
\draw[magenta, decorate me] (A) .. controls(0.25,4) and (0.75,-4) .. (B);
\end{scope}
\begin{scope}[xshift=6cm]
\node (A) {A}; \node at (1,0) (B) {B};
\draw[magenta, decorate me] (A) .. controls(0.25,7) and (0.75,-7) .. (B);
\end{scope}
\end{tikzpicture}
\end{document}