4

Section 14.15 of the PGF manual (pp 150-) describes the let operation. On pg 152 (first example) it is applied to saving coordinate (tuple values) to point registers p1 and p2 using the coordinate function to enable accessing computed coordinates outside the body of the let operation. It seems to me that it should be possible to save and reuse number registers in a similar fashion, but that does not appear to be the case. See proposed code below. I want to use the computed length \n1 as the length of the subrectangle DATASET, but this does not seem possible within the constraints of this syntax. Ideas? Workarounds?

\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}
Faheem Mitha
  • 7,778

1 Answers1

4

You can define a .code key like

\pgfkeys{/tikz/savenumber/.code 2 args={\global\edef#1{#2}}}

which allows you to use

[savenumber={<macro to save to>}{<macro to save>}]

(note the square brackets) somewhere in a TikZ path (including during a let operation).

Your example would thus look like

\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}
Jake
  • 232,450
  • Hi Jake. You didn't change the last line, which uses the saved value. I assume one would replace 2em with \n? Could one use n1 instead of \n? So, is there any reason a function like my number does not exist? – Faheem Mitha Sep 08 '11 at 08:51
  • @Faheem: Sorry, missed changing that line. Fixed now. Can't tell you why such a function doesn't exist. You could just define a new macro \newcommand\savenumber{[savenumber={#1}{#2}]} to wrap around the key I suggest in my answer, and then you've basically got your number function. – Jake Sep 08 '11 at 10:46
  • @Faheem: And missed your question about the macro name: I'm not quite sure what you mean with "Could one use n1 instead of \n. You can use whatever macro name you want, as long as it's a legal TeX macro name, so it needs to have the backslash in front, and can't have a number; n1 would therefore not work. – Jake Sep 08 '11 at 10:56
  • Thanks. Your solution works, but the \newcommand you suggested doesn't. I get ERROR: Illegal parameter number in definition of \savenumber. – Faheem Mitha Sep 08 '11 at 18:18
  • @Faheem: Sorry, it should be \newcommand\savenumber[2]{[savenumber={#1}{#2}]} (I forgot the [2], which specifies the number of parameters). – Jake Sep 08 '11 at 21:57
  • Thanks. I should have been able to figure that out myself. Color me lazy. – Faheem Mitha Sep 10 '11 at 04:28