我如何访问“\pgfdecoratedpathlength”的值以供稍后在文档中使用?

我如何访问“\pgfdecoratedpathlength”的值以供稍后在文档中使用?

在装饰过程中,我是否可以将值保存\pgfdecoratedpathlength到某个宏中(通过xdef或其他机制)?

答案1

这是一种全局存储路径长度的方法。

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

在编译期间,本文档写道:

==> 144.53998pt
==> 314.19373pt

答案2

这是另一种方法...

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

在此处输入图片描述

相关内容