5

Is there anyway that during the decoration process I can save the value of \pgfdecoratedpathlength to some macro (via xdef or some other mechanism)?

  • 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 6:57
  • @PaulGaborit i want the input length, assuming that's the entire length of the – A.Ellett May 30 '15 at 7:02
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
  • 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 6 '17 at 20:22
  • 1
    @Dr.ManuelKuehner The store path length decoration is a fake decoration that only store the total length of the path into \pathlength. The store path length style apply this decoration as preaction to a path. – Paul Gaborit Jan 7 '17 at 0: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}

enter image description here

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.