# How to position multiple decorations on a path while keeping their separation constant?

The at position=<value> syntax for decorations obviously makes the positioning of the decorations dependent on the path length:

\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}


Since the three magenta paths have increasing length, the markings will be farther apart from each other. Is there a way to place both decorations such that the distance along the path between them remains constant (i.e., placing both decorations at, let's say, half the path but guaranteeing that they will always be 50pt away from each other along the path)?

-
Do you want them to be aligned horizontally or the distance between them should remain the same? –  percusse Apr 17 '13 at 20:01
@percusse the distance between them (along the path) should remain the same. –  Gonzalo Medina Apr 17 '13 at 20:03
The same approach, different syntax: Placement of arrowtips with TikZ’s markings –  Qrrbrbirlbel Jun 6 '13 at 4:06

## 2 Answers

Use \pgfdecoratedpathlength to calculate exact, absolute distances:

\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}


-

### Steps:

• Get the total length of the path with the help of the /pgf/decoration/mark info/distance from start key
• Save this in \@totallength and make it global
• at position 1 works not every time (as previously found out on TeX.sx), so a very short distance from the end is used; here -.01pt worked, -.001pt didn’t for the first path
• Use .5*\@totallength-25pt for the first node and
• use .5*\@totallength+25pt for the second

## Code

\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}


-
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.9377079010009766, "perplexity": 10901.705287538236}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": false}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2014-52/segments/1418802769990.68/warc/CC-MAIN-20141217075249-00012-ip-10-231-17-201.ec2.internal.warc.gz"}