假设我在 tikz 中有一个任意路径(如下例所示)。如何获取路径的弧长并将其存储在宏中以供以后使用?
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw plot[smooth] coordinates {(0,0) (1,3) (4,7) (-2,9)};
\end{tikzpicture}
\end{document}
编辑:
使用@Symbol1 的评论,我能够获得路径长度,但无法将其转移到装饰之外以便在文档的其他地方使用它:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{hobby}
\begin{document}
\begin{tikzpicture}
\draw[decoration={
markings,
mark=
at position 0 cm
with
{
\draw (0,0) circle (0pt) node[right]{\pgfdecoratedpathlength};
%\def{\PathLength}{\pgfdecoratedpathlength}
}
},
postaction=decorate
] plot[hobby] coordinates {(0,0) (1,3) (4,7) (-2,9)};
\end{tikzpicture}
%The length of the path is $\PathLength$.
\end{document}
答案1
这是使用 的尝试\pgfextra
。不确定这是否是最优雅的解决方案。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{hobby}
\begin{document}
\begin{tikzpicture}
\draw[
decoration={
markings,
mark=
at position 0 cm
with
{
\draw (0,0) circle (0pt)
\pgfextra{\xdef\PathLength{\pgfdecoratedpathlength}}
node[right] {\pgfdecoratedpathlength};
}
},
postaction=decorate
]
plot[hobby] coordinates {(0,0) (1,3) (4,7) (-2,9)};
\end{tikzpicture}
The length of the path is \PathLength.
\end{document}
答案2
\documentclass{article}
\usepackage{tikz}
\usepgfmodule{decorations}% This is enough. But any decoration library works
\begin{document}
\begin{tikzpicture}
\makeatletter
\path plot[smooth] coordinates {(2,2) (1,3) (4,7) (-2,9)}
\pgfextra{
The easiest way to smuggle the current softpath
\pgfsyssoftpath@getcurrentpath\student@softpath
\global\let\student@softpath = \student@softpath
}
;
You can backup the soft path if you want to
\let\teacher@softpath = \student@softpath
Compute the Length
\pgf@decorate@parsesoftpath\student@softpath\dummy@token@to@hold@parsed@path
\message{^^J^^J Public Service Announcement ^^J^^J}
\message{^^J^^J arc length \pgf@decorate@totalpathlength pt ^^J^^J}
\node[align=center]{arc length\\\pgf@decorate@totalpathlength pt};
Reuse the path
(student copy is destroyed by parsing, must use teacher copy)
\pgfsyssoftpath@setcurrentpath\teacher@softpath
\pgfsyssoftpath@flushcurrentpath
\pgfsys@stroke
Put the softpath back like nothing happens
\draw[red,thick,dotted]\pgfextra{\pgfsyssoftpath@setcurrentpath\teacher@softpath};
\end{tikzpicture}
\end{document}