PGF 手册第 14.15 节(第 150 页)描述了该let
操作。在第 152 页(第一个示例)中,它应用于将坐标(元组值)保存到点寄存器p1
,并p2
使用该coordinate
函数允许访问操作主体之外的计算坐标let
。在我看来,应该可以以类似的方式保存和重用数字寄存器,但事实似乎并非如此。请参阅下面的建议代码。我想使用计算出的长度\n1
作为子矩形的长度DATASET
,但这在该语法的约束内似乎是不可能的。有什么想法吗?有解决方法吗?
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows, positioning, calc}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5em}%
\begin{document}
\begin{tikzpicture}[auto]
\tikzstyle{file} = [ellipse, rounded corners, minimum height=1em, minimum width=1em, align=center, draw]
\tikzstyle{block} = [rectangle, rounded corners, minimum height=2em, minimum width=15em, align=center, draw]
\node [block, minimum height=10em] (DB){\textbf{Database}};
% Set coordinate points for sub-rectangles
% See pg 420 of 2.10 PGF manual for anchors
% 'let' is defined on pg 150 in Section 14.15 (The Let Operation)
\path
let
\p1 = ($(DB.east)$),
\p2 = ($(DB.west)$),
\n1 = {veclen(\x1-\x2,\y1-\y2)/2}
in
coordinate(p1) at (\p1)
coordinate(p2) at (\p2)
%number(n1) at (\n1) % illustrative syntax
;
% Use n1 for 'minimum width' instead of 2em
\node [block, draw, minimum width=2em] (DATASET) at ($(DB.north)!2/7!(DB.south)$) {Dataset};
\end{tikzpicture}
\end{document}
答案1
您可以定义一个.code
键,例如
\pgfkeys{/tikz/savenumber/.code 2 args={\global\edef#1{#2}}}
您可以使用
[savenumber={<macro to save to>}{<macro to save>}]
(注意方括号)位于 TikZ 路径的某处(包括操作期间let
)。
你的例子看起来就像
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows, positioning, calc}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5em}%
\pgfkeys{/tikz/savenumber/.code 2 args={\global\edef#1{#2}}}
\begin{document}
\begin{tikzpicture}[auto]
\tikzstyle{file} = [ellipse, rounded corners, text width=12em, text height=3em, minimum height=1em, minimum width=1em, align=center, draw]
\tikzstyle{block} = [rectangle, rounded corners, text width=12em, text height=3em, minimum height=2em, minimum width=15em, align=center, draw]
\node [block, minimum height=10em] (DB){\textbf{Database}};
% Set coordinate points for sub-rectangles
% See pg 420 of 2.10 PGF manual for anchors
% 'let' is defined on pg 150 in Section 14.15 (The Let Operation)
\path
let
\p1 = ($(DB.east)$),
\p2 = ($(DB.west)$),
\n1 = {veclen(\x1-\x2,\y1-\y2)/2}
in
coordinate(p1) at (\p1)
coordinate(p2) at (\p2)
[savenumber={\n}{\n1}]
;
% Use n1 for 'minimum width' instead of 2em
\node [block, draw, minimum width=\n] (DATASET) at ($(DB.north)!2/7!(DB.south)$) {Dataset};
\end{tikzpicture}
\end{document}