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)?

Paul Gaborit
  • 70,770
  • 10
  • 176
  • 283
A.Ellett
  • 50,533

2 Answers2

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

enter image description here

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