我需要获取一个节点(西和东)的 2 个 X 值,将它们保存为变量,然后使用它们进行一些计算。以下是一些显然不起作用的伪代码:
\usepackage{fp}
\usepackage{tikz}
\usetikzlibrary{calc}
\path let
\p{W}=(node1.west),
\p{E}=(node1.east)
in
\def\nWest{\x{W}} % Here, I get an error everytime I try to any commands
\def\nEast{\x{E}}
;
\def\nWidth {} % Also, is it possible to do calculations right here in the definition?
\FPeval\num{clip((nEast-nWest)/3)} % This may be more complex in the final code.
答案1
如以下示例所示,这是可能的:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\node[draw,minimum width=3cm](node1) {};
\path
let
\p1=(node1.west),
\p2=(node1.east),
\n1={abs(\x2-\x1)/3} in node[
draw,red,
minimum width=\n1,] {};
\end{tikzpicture}
\end{document}
如果您想在这里使用\def
(或\xdef
) 等,请\pgfextra
类似地使用:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\node[draw,minimum width=3cm](node1) {};
\path
let
\p1=(node1.west),
\p2=(node1.east),
\n1={abs(\x2-\x1)/3} in
\pgfextra{
\newlength{\mylen}
\setlength{\mylen}{\n1}
}
node[
draw,red,
minimum width=\mylen,] {};
\end{tikzpicture}
\end{document}