如何在路径上定位多个装饰并保持它们的分离不变?

如何在路径上定位多个装饰并保持它们的分离不变?

装饰的语法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}

输出

在此处输入图片描述

相关内容