Is there anyway that during the decoration process I can save the value of \pgfdecoratedpathlength to some macro (via xdef or some other mechanism)?
Asked
Active
Viewed 196 times
5
Paul Gaborit
- 70,770
- 10
- 176
- 283
A.Ellett
- 50,533
-
Yes, it's possible. Do you want to store the length of the input path or the length of the path after some decoration process? – Paul Gaborit May 30 '15 at 06:57
-
@PaulGaborit i want the input length, assuming that's the entire length of the – A.Ellett May 30 '15 at 07:02
2 Answers
6
Here is a way to globally store the path length.
\documentclass[tikz,margin=2mm]{standalone}
\usetikzlibrary{decorations}
\pgfdeclaredecoration{store path length}{final}{
\state{final}{
\xdef\pathlength{\pgfdecoratedpathlength}
\pgfpathmoveto{\pgfpointdecoratedpathlast}
}
}
\tikzset{store path length/.style={
preaction={decorate,decoration={store path length}}}
}
\begin{document}
\begin{tikzpicture}
\draw[store path length] (0,0) -- (1in,0) -- (1in,1in);
\typeout{==> \pathlength}
\draw[store path length] circle(50pt);
\typeout{==> \pathlength}
\end{tikzpicture}
\end{document}
During compilation, this document writes:
==> 144.53998pt ==> 314.19373pt
Paul Gaborit
- 70,770
- 10
- 176
- 283
-
Argh. I thought I'd tried something like this. Only I was mucking around in
\state{initial}. – A.Ellett May 30 '15 at 15:59 -
I'm traveling most of today and tomorrow. I'll look at this a bit closer on Monday. – A.Ellett May 30 '15 at 15:59
-
I just successfully used your code. It would be great if you could explain a little bit what is happening (pseudo code)? – Dr. Manuel Kuehner Jan 06 '17 at 20:22
-
1@Dr.ManuelKuehner The
store path lengthdecoration is a fake decoration that only store the total length of the path into\pathlength. Thestore path lengthstyle apply this decoration aspreactionto a path. – Paul Gaborit Jan 07 '17 at 00:25
6
Here's another way...
\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{decorations}
\makeatletter
\tikzset{%
get path length/.code={%
\tikz@addoption{%
\pgfgetpath\tikz@tmppath%
\pgfprocessround\tikz@tmppath\tikz@tmppath%
\pgf@decorate@parsesoftpath\tikz@tmppath\tikz@discard%
\global\let#1=\pgf@decorate@totalpathlength%
}%
}
}
\begin{document}
\begin{tikzpicture}[x=1pt, y=1pt]
\draw [get path length=\a] (-20, -20) rectangle ++(40, 40);
\node {\a};
\tikzset{shift=(270:100)}
\draw [get path length=\b] circle [radius=50];
\node {\b};
\end{tikzpicture}
\end{document}

Mark Wibrow
- 70,437
-
Very interesting. I'll have to play with this a bit since you're using several macros I'm unfamiliar with. – A.Ellett May 30 '15 at 16:00