diff options
Diffstat (limited to 'Master/texmf-dist/doc/generic/pgf/version-for-tex4ht/en/pgfmanualse15.html')
-rw-r--r-- | Master/texmf-dist/doc/generic/pgf/version-for-tex4ht/en/pgfmanualse15.html | 974 |
1 files changed, 974 insertions, 0 deletions
diff --git a/Master/texmf-dist/doc/generic/pgf/version-for-tex4ht/en/pgfmanualse15.html b/Master/texmf-dist/doc/generic/pgf/version-for-tex4ht/en/pgfmanualse15.html new file mode 100644 index 00000000000..3be2199c388 --- /dev/null +++ b/Master/texmf-dist/doc/generic/pgf/version-for-tex4ht/en/pgfmanualse15.html @@ -0,0 +1,974 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" + "http://www.w3.org/TR/html4/loose.dtd"> +<html > +<head><title>15 Repeating Things: The Foreach Statement</title> +<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> +<meta name="generator" content="TeX4ht (http://www.cis.ohio-state.edu/~gurari/TeX4ht/mn.html)"> +<meta name="originator" content="TeX4ht (http://www.cis.ohio-state.edu/~gurari/TeX4ht/mn.html)"> +<!-- html --> +<meta name="src" content="pgfmanual.tex"> +<meta name="date" content="2005-10-23 18:34:00"> +<link rel="stylesheet" type="text/css" href="pgfmanual.css"> +</head><body +> + <div class="crosslinks"><p class="noindent">[<a +href="pgfmanualse16.html" >next</a>] [<a +href="pgfmanualse14.html" >prev</a>] [<a +href="pgfmanualse14.html#tailpgfmanualse14.html" >prev-tail</a>] [<a +href="#tailpgfmanualse15.html">tail</a>] [<a +href="pgfmanual.html#pgfmanualse15.html" >up</a>] </p></div> + <h3 class="sectionHead"><span class="titlemark">15 </span> <a + id="x16-17900015"></a>Repeating Things: The Foreach Statement</h3> +<!--l. 10--><p class="noindent">In this section the package <span class="obeylines-h"><span class="verb"><span +class="cmtt-10">pgffor.sty</span></span></span> is described. It can be used independently of <span +class="cmcsc-10"><small +class="small-caps">P</small><small +class="small-caps">G</small><small +class="small-caps">F</small></span>, but it works particularly +well together with <span +class="cmcsc-10"><small +class="small-caps">P</small><small +class="small-caps">G</small><small +class="small-caps">F</small></span> and Ti<span +class="cmti-10">k</span>Z. +<!--l. 14--><p class="indent"> When you say <span class="obeylines-h"><span class="verb"><span +class="cmtt-10">\usepackage{pgffor}</span></span></span>, two commands are defined: <span class="obeylines-h"><span class="verb"><span +class="cmtt-10">\foreach</span></span></span> and <span class="obeylines-h"><span class="verb"><span +class="cmtt-10">\breakforeach</span></span></span>. Their +behaviour is described in the following: +<!--l. 19--><p class="indent"> + <dl class="description"><dt class="description"> +<span +class="colorbox" id="colorbox147"><span +class="cmtt-10">\foreach</span><span class="obeylines-h"><span class="verb"> </span></span><span +class="cmsy-10"><</span><span +class="cmti-10">variables</span><span +class="cmsy-10">></span><span class="obeylines-h"><span class="verb"> <span +class="cmtt-10">in </span></span></span><span +class="cmtt-10">{</span><span +class="cmsy-10"><</span><span +class="cmti-10">list</span><span +class="cmsy-10">></span><span +class="cmtt-10">} </span><span +class="cmsy-10"><</span><span +class="cmti-10">commands</span><span +class="cmsy-10">></span></span> </dt><dd +class="description"> +<a + id="dx16-179001"></a> + <!--l. 20--><p class="noindent">The syntax of this command is a bit complicated, so let us go through it step-by-step. + <!--l. 23--><p class="noindent">In the easiest case, <span +class="cmsy-10"><</span><span +class="cmti-10">variables</span><span +class="cmsy-10">> </span>is a single <span class="TEX">T<span +class="E">E</span>X</span>-command like <span class="obeylines-h"><span class="verb"><span +class="cmtt-10">\x</span></span></span> or <span class="obeylines-h"><span class="verb"><span +class="cmtt-10">\point</span></span></span>. (If you want to have some + fun, you can also use active characters. If you do not know what active characters are, you are blessed.) + <!--l. 28--><p class="noindent">Still in the easiest case, <span +class="cmsy-10"><</span><span +class="cmti-10">list</span><span +class="cmsy-10">> </span>is a comma-separated list of values. Anything can be used as a value, + but numbers are most likely. + <!--l. 32--><p class="noindent">Finally, in the easiest case, <span +class="cmsy-10"><</span><span +class="cmti-10">commands</span><span +class="cmsy-10">> </span>is some <span class="TEX">T<span +class="E">E</span>X</span>-text in curly braces. + <!--l. 35--><p class="noindent">With all these assumptions, the <span class="obeylines-h"><span class="verb"><span +class="cmtt-10">\foreach</span></span></span> statement will execute the <span +class="cmsy-10"><</span><span +class="cmti-10">commands</span><span +class="cmsy-10">> </span>repeatedly, once for + every element of the <span +class="cmsy-10"><</span><span +class="cmti-10">list</span><span +class="cmsy-10">></span>. Each time the <span +class="cmsy-10"><</span><span +class="cmti-10">commands</span><span +class="cmsy-10">> </span>are executed, the <span +class="cmsy-10"><</span><span +class="cmti-10">variable</span><span +class="cmsy-10">> </span>will be set to the + current value of the list item. + <!--l. 40--><p class="noindent"><div class="obeylines-v"></div><!--tex4ht:inline--><div class="tabular"><table class="tabular" +cellspacing="0" cellpadding="0" +frame="void" id="TBL-366-" ><colgroup id="TBL-366-1g"><col +id="TBL-366-1"><col +id="TBL-366-2"></colgroup><tr + valign="baseline" id="TBL-366-1-"><td align="left" style="white-space:nowrap; text-align:left;" id="TBL-366-1-1" +class="td11"> [1][2][3][0] </td><td align="left" style="white-space:nowrap; text-align:left;" id="TBL-366-1-2" +class="td11"> <table class="minipage"><tr><td><div class="obeylines-v"> +<br><span +class="cmtt-8">\foreach</span><span +class="cmtt-8"> \x</span><span +class="cmtt-8"> in</span><span +class="cmtt-8"> {1,2,3,0}</span><span +class="cmtt-8"> {[\x]}</span></div> </td></tr></table> </td> +</tr></table> + </div> + <!--l. 45--><p class="noindent"><span +class="cmbx-10">Syntax for the commands. </span>Let use move on to a more complicated setting. The first complication occurs + when the <span +class="cmsy-10"><</span><span +class="cmti-10">commands</span><span +class="cmsy-10">> </span>are not some text in curly braces. If the <span class="obeylines-h"><span class="verb"><span +class="cmtt-10">\foreach</span></span></span> statement does not encounter an + opening brace, it will instead scan everything up to the next semicolon and use this as <span +class="cmsy-10"><</span><span +class="cmti-10">commands</span><span +class="cmsy-10">></span>. This is + most useful in situations like the following: + <!--l. 53--><p class="noindent"><div class="obeylines-v"></div><!--tex4ht:inline--><div class="tabular"><table class="tabular" +cellspacing="0" cellpadding="0" +frame="void" id="TBL-367-" ><colgroup id="TBL-367-1g"><col +id="TBL-367-1"><col +id="TBL-367-2"></colgroup><tr + valign="baseline" id="TBL-367-1-"><td align="left" style="white-space:nowrap; text-align:left;" id="TBL-367-1-1" +class="td11"> <object data="pgfmanual-457.svg" width="97.13914pt" height="11.78091pt" type="image/svg+xml"><p>SVG-Viewer needed.</p></object> </td><td align="left" style="white-space:nowrap; text-align:left;" id="TBL-367-1-2" +class="td11"> <table class="minipage"><tr><td><div class="obeylines-v"> +<br><span +class="cmtt-8">\tikz</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> \foreach</span><span +class="cmtt-8"> \x</span><span +class="cmtt-8"> in</span><span +class="cmtt-8"> {0,1,2,3}</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> \draw</span><span +class="cmtt-8"> (\x,0)</span><span +class="cmtt-8"> circle</span><span +class="cmtt-8"> (0.2cm);</span></div> </td></tr></table> </td> +</tr></table></div> + <!--l. 59--><p class="noindent">However, the “reading till the next semicolon” is not the whole truth. There is another rule: If a <span class="obeylines-h"><span class="verb"><span +class="cmtt-10">\foreach</span></span></span> + statement is directly followed by another <span class="obeylines-h"><span class="verb"><span +class="cmtt-10">\foreach</span></span></span> statement, this second foreach statement is collected as + <span +class="cmsy-10"><</span><span +class="cmti-10">commands</span><span +class="cmsy-10">></span>. This allows you to write the following: + <!--l. 65--><p class="noindent"><div class="obeylines-v"></div><!--tex4ht:inline--><div class="tabular"><table class="tabular" +cellspacing="0" cellpadding="0" +frame="void" id="TBL-368-" ><colgroup id="TBL-368-1g"><col +id="TBL-368-1"><col +id="TBL-368-2"></colgroup><tr + valign="baseline" id="TBL-368-1-"><td align="left" style="white-space:nowrap; text-align:left;" id="TBL-368-1-1" +class="td11"> <object data="pgfmanual-458.svg" width="97.13914pt" height="97.13914pt" type="image/svg+xml"><p>SVG-Viewer needed.</p></object> </td><td align="left" style="white-space:nowrap; text-align:left;" id="TBL-368-1-2" +class="td11"> <table class="minipage"><tr><td><div class="obeylines-v"> +<br><span +class="cmtt-8">\begin{tikzpicture}</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> \foreach</span><span +class="cmtt-8"> \x</span><span +class="cmtt-8"> in</span><span +class="cmtt-8"> {0,1,2,3}</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> \foreach</span><span +class="cmtt-8"> \y</span><span +class="cmtt-8"> in</span><span +class="cmtt-8"> {0,1,2,3}</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> {</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> \draw</span><span +class="cmtt-8"> (\x,\y)</span><span +class="cmtt-8"> circle</span><span +class="cmtt-8"> (0.2cm);</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> \fill</span><span +class="cmtt-8"> (\x,\y)</span><span +class="cmtt-8"> circle</span><span +class="cmtt-8"> (0.1cm);</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> }</span> +<br><span +class="cmtt-8">\end{tikzpicture}</span></div> </td></tr></table> </td> +</tr></table></div> + + + <!--l. 77--><p class="noindent"><span +class="cmbx-10">The dots notation. </span>The second complication concerns the <span +class="cmsy-10"><</span><span +class="cmti-10">list</span><span +class="cmsy-10">></span>. If this <span +class="cmsy-10"><</span><span +class="cmti-10">list</span><span +class="cmsy-10">> </span>contains the + list item “<span class="obeylines-h"><span class="verb"><span +class="cmtt-10">...</span></span></span>”, this list item is replaced by the “missing values.” More precisely, the following + happens: + <!--l. 82--><p class="noindent">Normally, when a list item <span class="obeylines-h"><span class="verb"><span +class="cmtt-10">...</span></span></span> is encountered, there should already have been <span +class="cmti-10">two </span>list items before it, which + where numbers. Examples of <span +class="cmti-10">numbers </span>are <span class="obeylines-h"><span class="verb"><span +class="cmtt-10">1</span></span></span>, <span class="obeylines-h"><span class="verb"><span +class="cmtt-10">-10</span></span></span>, or <span class="obeylines-h"><span class="verb"><span +class="cmtt-10">-0.24</span></span></span>. Let us call these numbers <span +class="cmmi-10">x </span>and <span +class="cmmi-10">y </span>and let + <span +class="cmmi-10">d </span>:= <span +class="cmmi-10">y </span><span +class="cmsy-10">-</span><span +class="cmmi-10">x </span>be their difference. Next, there should also be one number following the three dots, let us call this + number <span +class="cmmi-10">z</span>. + <!--l. 89--><p class="noindent">In this situation, the part of the list reading “<span +class="cmmi-10">x</span><span class="obeylines-h"><span class="verb"><span +class="cmtt-10">,</span></span></span><span +class="cmmi-10">y</span><span class="obeylines-h"><span class="verb"><span +class="cmtt-10">,...,</span></span></span><span +class="cmmi-10">z</span>” is replaced by “<span +class="cmmi-10">x</span>, <span +class="cmmi-10">x </span>+ <span +class="cmmi-10">d</span>, <span +class="cmmi-10">x </span>+ 2<span +class="cmmi-10">d</span>, <span +class="cmmi-10">x </span>+ 3<span +class="cmmi-10">d</span>, ..., + <span +class="cmmi-10">x </span>+ <span +class="cmmi-10">md</span>,” where the last dots are semantic dots, not syntactic dots. The value <span +class="cmmi-10">m </span>is the largest number such + that <span +class="cmmi-10">x </span>+ <span +class="cmmi-10">md </span><span +class="cmsy-10"><u +class="underline"><</u> </span><span +class="cmmi-10">z </span>if <span +class="cmmi-10">d </span>is positive or such that <span +class="cmmi-10">x </span>+ <span +class="cmmi-10">md </span><span +class="cmsy-10"><u +class="underline">></u> </span><span +class="cmmi-10">z </span>if <span +class="cmmi-10">d </span>is negative. + <!--l. 96--><p class="noindent">Perhaps it is best to explain this by some examples: The following <span +class="cmsy-10"><</span><span +class="cmti-10">list</span><span +class="cmsy-10">> </span>have the same effects: + <!--l. 99--><p class="noindent"><span class="obeylines-h"><span class="verb"><span +class="cmtt-10">\foreach</span><span +class="cmtt-10"> \x</span><span +class="cmtt-10"> in</span><span +class="cmtt-10"> {1,2,...,6}</span><span +class="cmtt-10"> {\x,</span><span +class="cmtt-10"> }</span></span></span> yields 1, 2, 3, 4, 5, 6, + <!--l. 101--><p class="noindent"><span class="obeylines-h"><span class="verb"><span +class="cmtt-10">\foreach</span><span +class="cmtt-10"> \x</span><span +class="cmtt-10"> in</span><span +class="cmtt-10"> {1,2,3,...,6}</span><span +class="cmtt-10"> {\x,</span><span +class="cmtt-10"> }</span></span></span> yields 1, 2, 3, 4, 5, 6, + <!--l. 103--><p class="noindent"><span class="obeylines-h"><span class="verb"><span +class="cmtt-10">\foreach</span><span +class="cmtt-10"> \x</span><span +class="cmtt-10"> in</span><span +class="cmtt-10"> {1,3,...,11}</span><span +class="cmtt-10"> {\x,</span><span +class="cmtt-10"> }</span></span></span> yields 1, 3, 5, 7, 9, 11, + <!--l. 105--><p class="noindent"><span class="obeylines-h"><span class="verb"><span +class="cmtt-10">\foreach</span><span +class="cmtt-10"> \x</span><span +class="cmtt-10"> in</span><span +class="cmtt-10"> {1,3,...,10}</span><span +class="cmtt-10"> {\x,</span><span +class="cmtt-10"> }</span></span></span> yields 1, 3, 5, 7, 9, + <!--l. 107--><p class="noindent"><span class="obeylines-h"><span class="verb"><span +class="cmtt-10">\foreach</span><span +class="cmtt-10"> \x</span><span +class="cmtt-10"> in</span><span +class="cmtt-10"> {0,0.1,...,0.5}</span><span +class="cmtt-10"> {\x,</span><span +class="cmtt-10"> }</span></span></span> yields 0, 0.1, 0.20001, 0.30002, 0.40002, + <!--l. 109--><p class="noindent"><span class="obeylines-h"><span class="verb"><span +class="cmtt-10">\foreach</span><span +class="cmtt-10"> \x</span><span +class="cmtt-10"> in</span><span +class="cmtt-10"> {a,b,9,8,...,1,2,2.125,...,2.5}</span><span +class="cmtt-10"> {\x,</span><span +class="cmtt-10"> }</span></span></span> yields a, b, 9, 8, 7, 6, 5, 4, 3, 2, 1, 2, 2.125, + 2.25, 2.375, 2.5, + <!--l. 111--><p class="noindent">As can be seen, for fractional steps that are not multiples of 2<sup ><span +class="cmsy-7">-</span><span +class="cmmi-7">n</span></sup> for some small <span +class="cmmi-10">n</span>, rounding errors can + occur pretty easily. Thus, in the second last case, <span class="obeylines-h"><span class="verb"><span +class="cmtt-10">0.5</span></span></span> should probably be replaced by <span class="obeylines-h"><span class="verb"><span +class="cmtt-10">0.501</span></span></span> for + robustness. + <!--l. 116--><p class="noindent">There is yet another special case for the <span class="obeylines-h"><span class="verb"><span +class="cmtt-10">...</span></span></span> statement: If the <span class="obeylines-h"><span class="verb"><span +class="cmtt-10">...</span></span></span> is used right after the first item in the list, + that is, if there is an <span +class="cmmi-10">x</span>, but no <span +class="cmmi-10">y</span>, the difference <span +class="cmmi-10">d </span>obviously cannot be computed and is set to 1 if the number <span +class="cmmi-10">z</span> + following the dots is larger than <span +class="cmmi-10">x </span>and is set to <span +class="cmsy-10">-</span>1 if <span +class="cmmi-10">z </span>is smaller: + <!--l. 122--><p class="noindent"><span class="obeylines-h"><span class="verb"><span +class="cmtt-10">\foreach</span><span +class="cmtt-10"> \x</span><span +class="cmtt-10"> in</span><span +class="cmtt-10"> {1,...,6}</span><span +class="cmtt-10"> {\x,</span><span +class="cmtt-10"> }</span></span></span> yields 1, 2, 3, 4, 5, 6, + <!--l. 124--><p class="noindent"><span class="obeylines-h"><span class="verb"><span +class="cmtt-10">\foreach</span><span +class="cmtt-10"> \x</span><span +class="cmtt-10"> in</span><span +class="cmtt-10"> {9,...,3.5}</span><span +class="cmtt-10"> {\x,</span><span +class="cmtt-10"> }</span></span></span> yields 9, 8, 7, 6, 5, 4, + <!--l. 127--><p class="noindent"><span +class="cmbx-10">Special handling of pairs. </span>Different list items are separated by commas. However, this causes a problem + when the list items contain commas themselves as pairs like <span class="obeylines-h"><span class="verb"><span +class="cmtt-10">(0,1)</span></span></span> do. In this case, you should put the items + containing commas in braces as in <span class="obeylines-h"><span class="verb"><span +class="cmtt-10">{(0,1)}</span></span></span>. However, since pairs are such a natural and useful + case, they get a special treatment by the <span class="obeylines-h"><span class="verb"><span +class="cmtt-10">\foreach</span></span></span> statement. When a list item starts with + a <span class="obeylines-h"><span class="verb"><span +class="cmtt-10">(</span></span></span> everything up to the next <span class="obeylines-h"><span class="verb"><span +class="cmtt-10">)</span></span></span> is made part of the item. Thus, we can write things like the + following: + <!--l. 137--><p class="noindent"><div class="obeylines-v"></div><!--tex4ht:inline--><div class="tabular"><table class="tabular" +cellspacing="0" cellpadding="0" +frame="void" id="TBL-369-" ><colgroup id="TBL-369-1g"><col +id="TBL-369-1"><col +id="TBL-369-2"></colgroup><tr + valign="baseline" id="TBL-369-1-"><td align="left" style="white-space:nowrap; text-align:left;" id="TBL-369-1-1" +class="td11"> <object data="pgfmanual-459.svg" width="92.8714pt" height="43.0791pt" type="image/svg+xml"><p>SVG-Viewer needed.</p></object> </td><td align="left" style="white-space:nowrap; text-align:left;" id="TBL-369-1-2" +class="td11"> <table class="minipage"><tr><td><div class="obeylines-v"> +<br><span +class="cmtt-8">\tikz</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> \foreach</span><span +class="cmtt-8"> \position</span><span +class="cmtt-8"> in</span><span +class="cmtt-8"> {(0,0),</span><span +class="cmtt-8"> (1,1),</span><span +class="cmtt-8"> (2,0),</span><span +class="cmtt-8"> (3,1)}</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> \draw</span><span +class="cmtt-8"> \position</span><span +class="cmtt-8"> rectangle</span><span +class="cmtt-8"> +(.25,.5);</span></div> </td></tr></table> </td> +</tr></table></div> + <!--l. 144--><p class="noindent"><span +class="cmbx-10">Using the foreach-statement inside paths. </span>Ti<span +class="cmti-10">k</span>Z allows you to use a <span class="obeylines-h"><span class="verb"><span +class="cmtt-10">\foreach</span></span></span> statement inside a path + construction. In such a case, the <span +class="cmsy-10"><</span><span +class="cmti-10">commands</span><span +class="cmsy-10">> </span>must be path construction commands. Here are two + examples: + <!--l. 149--><p class="noindent"><div class="obeylines-v"></div><!--tex4ht:inline--><div class="tabular"><table class="tabular" +cellspacing="0" cellpadding="0" +frame="void" id="TBL-370-" ><colgroup id="TBL-370-1g"><col +id="TBL-370-1"><col +id="TBL-370-2"></colgroup><tr + valign="baseline" id="TBL-370-1-"><td align="left" style="white-space:nowrap; text-align:left;" id="TBL-370-1-1" +class="td11"> <object data="pgfmanual-460.svg" width="85.75822pt" height="28.85274pt" type="image/svg+xml"><p>SVG-Viewer needed.</p></object> </td><td align="left" style="white-space:nowrap; text-align:left;" id="TBL-370-1-2" +class="td11"> <table class="minipage"><tr><td><div class="obeylines-v"> +<br><span +class="cmtt-8">\tikz</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> \draw</span><span +class="cmtt-8"> (0,0)</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> \foreach</span><span +class="cmtt-8"> \x</span><span +class="cmtt-8"> in</span><span +class="cmtt-8"> {1,...,3}</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> {</span><span +class="cmtt-8"> --</span><span +class="cmtt-8"> (\x,1)</span><span +class="cmtt-8"> --</span><span +class="cmtt-8"> (\x,0)</span><span +class="cmtt-8"> }</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> ;</span></div> </td></tr></table> </td> +</tr></table></div> + + + <!--l. 157--><p class="noindent"><div class="obeylines-v"></div><!--tex4ht:inline--><div class="tabular"> +<table class="tabular" +cellspacing="0" cellpadding="0" +frame="void" id="TBL-371-" ><colgroup id="TBL-371-1g"><col +id="TBL-371-1"><col +id="TBL-371-2"></colgroup><tr + valign="baseline" id="TBL-371-1-"><td align="left" style="white-space:nowrap; text-align:left;" id="TBL-371-1-1" +class="td11"> <object data="pgfmanual-461.svg" width="57.30548pt" height="57.30548pt" type="image/svg+xml"><p>SVG-Viewer needed.</p></object> </td><td align="left" style="white-space:nowrap; text-align:left;" id="TBL-371-1-2" +class="td11"> <table class="minipage"><tr><td><div class="obeylines-v"> +<br><span +class="cmtt-8">\tikz</span><span +class="cmtt-8"> \draw</span><span +class="cmtt-8"> \foreach</span><span +class="cmtt-8"> \p</span><span +class="cmtt-8"> in</span><span +class="cmtt-8"> {1,...,3}</span><span +class="cmtt-8"> {(\p,1)--(\p,3)</span><span +class="cmtt-8"> (1,\p)--(3,\p)};</span></div> </td></tr></table> </td> +</tr></table></div> + <!--l. 162--><p class="noindent"><span +class="cmbx-10">Multiple variables. </span>You will often wish to iterate over two variables at the same time. Since you can nest + <span class="obeylines-h"><span class="verb"><span +class="cmtt-10">\foreach</span></span></span> loops, this is normally straight-forward. However, you sometimes wish variables to iterate + “simultaneously.” For example, we might be given a list of edges that connect two coordinates and might wish + to iterate over these edges. While doing so, we would like the source and target of the edges to be set to two + different variables. + <!--l. 171--><p class="noindent">To achieve this, you can use the following syntax: The <span +class="cmsy-10"><</span><span +class="cmti-10">variables</span><span +class="cmsy-10">> </span>may not only be a single <span class="TEX">T<span +class="E">E</span>X</span>-variable. + Instead, it can also be a list of variables separated by slashes (<span class="obeylines-h"><span class="verb"><span +class="cmtt-10">/</span></span></span>). In this case the list items can also be lists of + values separated by slashes. + <!--l. 177--><p class="noindent">Assuming that the <span +class="cmsy-10"><</span><span +class="cmti-10">variables</span><span +class="cmsy-10">> </span>and the list items are lists of values, each time the <span +class="cmsy-10"><</span><span +class="cmti-10">commands</span><span +class="cmsy-10">> </span>are executed, + each of the variables in <span +class="cmsy-10"><</span><span +class="cmti-10">variables</span><span +class="cmsy-10">> </span>is set to one part of the list making up the current list item. Here is an + example to clarify this: + <!--l. 182--><p class="noindent"><span +class="cmti-10">Example: </span><span class="obeylines-h"><span class="verb"><span +class="cmtt-10">\foreach</span><span +class="cmtt-10"> \x</span><span +class="cmtt-10"> /</span><span +class="cmtt-10"> \y</span><span +class="cmtt-10"> in</span><span +class="cmtt-10"> {1/2,a/b}</span><span +class="cmtt-10"> {``\x\</span><span +class="cmtt-10"> and</span><span +class="cmtt-10"> \y''}</span></span></span> yields “1 and 2”“a and b”. + <!--l. 185--><p class="noindent">If some entry in the <span +class="cmsy-10"><</span><span +class="cmti-10">list</span><span +class="cmsy-10">> </span>does not have “enough” slashes, the last entry will be repeated. Here is an + example: + <!--l. 187--><p class="noindent"><div class="obeylines-v"></div><!--tex4ht:inline--><div class="tabular"><table class="tabular" +cellspacing="0" cellpadding="0" +frame="void" id="TBL-372-" ><colgroup id="TBL-372-1g"><col +id="TBL-372-1"><col +id="TBL-372-2"></colgroup><tr + valign="baseline" id="TBL-372-1-"><td align="left" style="white-space:nowrap; text-align:left;" id="TBL-372-1-1" +class="td11"> <object data="pgfmanual-462.svg" width="97.02419pt" height="13.11038pt" type="image/svg+xml"><p>SVG-Viewer needed.</p></object> </td><td align="left" style="white-space:nowrap; text-align:left;" id="TBL-372-1-2" +class="td11"> <table class="minipage"><tr><td><div class="obeylines-v"> +<br><span +class="cmtt-8">\begin{tikzpicture}</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> \foreach</span><span +class="cmtt-8"> \x/\xtext</span><span +class="cmtt-8"> in</span><span +class="cmtt-8"> {0,...,3,2.72</span><span +class="cmtt-8"> /</span><span +class="cmtt-8"> e}</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> \draw</span><span +class="cmtt-8"> (\x,0)</span><span +class="cmtt-8"> node{$\xtext$};</span> +<br><span +class="cmtt-8">\end{tikzpicture}</span></div> </td></tr></table> </td> +</tr></table></div> + <!--l. 194--><p class="noindent">Here are more useful examples: + <!--l. 195--><p class="noindent"><div class="obeylines-v"></div><!--tex4ht:inline--><div class="tabular"><table class="tabular" +cellspacing="0" cellpadding="0" +frame="void" id="TBL-373-" ><colgroup id="TBL-373-1g"><col +id="TBL-373-1"><col +id="TBL-373-2"></colgroup><tr + valign="baseline" id="TBL-373-1-"><td align="left" style="white-space:nowrap; text-align:left;" id="TBL-373-1-1" +class="td11"> <object data="pgfmanual-463.svg" width="99.98459pt" height="67.2985pt" type="image/svg+xml"><p>SVG-Viewer needed.</p></object> </td><td align="left" style="white-space:nowrap; text-align:left;" id="TBL-373-1-2" +class="td11"> <table class="minipage"><tr><td><div class="obeylines-v"> +<br><span +class="cmtt-8">\begin{tikzpicture}</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmsltt-10x-x-80">%</span><span +class="cmsltt-10x-x-80"> Define</span><span +class="cmsltt-10x-x-80"> some</span><span +class="cmsltt-10x-x-80"> coordinates:</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> \tikzstyle{every</span><span +class="cmtt-8"> node}=[draw,fill]</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> \path[shape=circle,fill=examplefill]</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> (0,0)</span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> node(a)</span><span +class="cmtt-8"> {a}</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> (2,0.55)</span><span +class="cmtt-8"> node(b)</span><span +class="cmtt-8"> {b}</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> (1,1.5)</span><span +class="cmtt-8"> </span><span +class="cmtt-8"> node(c)</span><span +class="cmtt-8"> {c}</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> (2,1.75)</span><span +class="cmtt-8"> node(d)</span><span +class="cmtt-8"> {d};</span> +<br> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmsltt-10x-x-80">%</span><span +class="cmsltt-10x-x-80"> Draw</span><span +class="cmsltt-10x-x-80"> some</span><span +class="cmsltt-10x-x-80"> connections:</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> \foreach</span><span +class="cmtt-8"> \source/\target</span><span +class="cmtt-8"> in</span><span +class="cmtt-8"> {a/b,</span><span +class="cmtt-8"> b/c,</span><span +class="cmtt-8"> c/a,</span><span +class="cmtt-8"> c/d}</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> \draw</span><span +class="cmtt-8"> (\source)</span><span +class="cmtt-8"> ..</span><span +class="cmtt-8"> controls</span><span +class="cmtt-8"> +(.75cm,0pt)</span><span +class="cmtt-8"> and</span><span +class="cmtt-8"> +(-.75cm,0pt)..(\target);</span> +<br><span +class="cmtt-8">\end{tikzpicture}</span></div> </td></tr></table> </td> +</tr></table></div> + + + <!--l. 211--><p class="noindent"><div class="obeylines-v"></div><!--tex4ht:inline--><div class="tabular"> +<table class="tabular" +cellspacing="0" cellpadding="0" +frame="void" id="TBL-374-" ><colgroup id="TBL-374-1g"><col +id="TBL-374-1"><col +id="TBL-374-2"></colgroup><tr + valign="baseline" id="TBL-374-1-"><td align="left" style="white-space:nowrap; text-align:left;" id="TBL-374-1-1" +class="td11"> <object data="pgfmanual-464.svg" width="65.84128pt" height="114.21097pt" type="image/svg+xml"><p>SVG-Viewer needed.</p></object> </td><td align="left" style="white-space:nowrap; text-align:left;" id="TBL-374-1-2" +class="td11"> <table class="minipage"><tr><td><div class="obeylines-v"> +<br><span +class="cmtt-8">\begin{tikzpicture}</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmsltt-10x-x-80">%</span><span +class="cmsltt-10x-x-80"> Let's</span><span +class="cmsltt-10x-x-80"> draw</span><span +class="cmsltt-10x-x-80"> circles</span><span +class="cmsltt-10x-x-80"> at</span><span +class="cmsltt-10x-x-80"> interesting</span><span +class="cmsltt-10x-x-80"> points:</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> \foreach</span><span +class="cmtt-8"> \x</span><span +class="cmtt-8"> /</span><span +class="cmtt-8"> \y</span><span +class="cmtt-8"> /</span><span +class="cmtt-8"> \diameter</span><span +class="cmtt-8"> in</span><span +class="cmtt-8"> {0</span><span +class="cmtt-8"> /</span><span +class="cmtt-8"> 0</span><span +class="cmtt-8"> /</span><span +class="cmtt-8"> 2mm,</span><span +class="cmtt-8"> 1</span><span +class="cmtt-8"> /</span><span +class="cmtt-8"> 1</span><span +class="cmtt-8"> /</span><span +class="cmtt-8"> 3mm,</span><span +class="cmtt-8"> 2</span><span +class="cmtt-8"> /</span><span +class="cmtt-8"> 0</span><span +class="cmtt-8"> /</span><span +class="cmtt-8"> 1mm}</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> \draw</span><span +class="cmtt-8"> (\x,\y)</span><span +class="cmtt-8"> circle</span><span +class="cmtt-8"> (\diameter);</span> +<br> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmsltt-10x-x-80">%</span><span +class="cmsltt-10x-x-80"> Same</span><span +class="cmsltt-10x-x-80"> effect</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> \foreach</span><span +class="cmtt-8"> \center/\diameter</span><span +class="cmtt-8"> in</span><span +class="cmtt-8"> {{(0,0)/2mm},</span><span +class="cmtt-8"> {(1,1)/3mm},</span><span +class="cmtt-8"> {(2,0)/1mm}}</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> \draw[yshift=2.5cm]</span><span +class="cmtt-8"> \center</span><span +class="cmtt-8"> circle</span><span +class="cmtt-8"> (\diameter);</span> +<br><span +class="cmtt-8">\end{tikzpicture}</span></div> </td></tr></table> </td> +</tr></table></div> + <!--l. 223--><p class="noindent"><div class="obeylines-v"></div><!--tex4ht:inline--><div class="tabular"><table class="tabular" +cellspacing="0" cellpadding="0" +frame="void" id="TBL-375-" ><colgroup id="TBL-375-1g"><col +id="TBL-375-1"><col +id="TBL-375-2"></colgroup><tr + valign="baseline" id="TBL-375-1-"><td align="left" style="white-space:nowrap; text-align:left;" id="TBL-375-1-1" +class="td11"> <object data="pgfmanual-465.svg" width="116.811pt" height="116.811pt" type="image/svg+xml"><p>SVG-Viewer needed.</p></object> </td><td align="left" style="white-space:nowrap; text-align:left;" id="TBL-375-1-2" +class="td11"> <table class="minipage"><tr><td><div class="obeylines-v"> +<br><span +class="cmtt-8">\begin{tikzpicture}[cap=round,line</span><span +class="cmtt-8"> width=3pt]</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> \filldraw</span><span +class="cmtt-8"> [fill=examplefill]</span><span +class="cmtt-8"> (0,0)</span><span +class="cmtt-8"> circle</span><span +class="cmtt-8"> (2cm);</span> +<br> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> \foreach</span><span +class="cmtt-8"> \angle</span><span +class="cmtt-8"> /</span><span +class="cmtt-8"> \label</span><span +class="cmtt-8"> in</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> {0/3,</span><span +class="cmtt-8"> 30/2,</span><span +class="cmtt-8"> 60/1,</span><span +class="cmtt-8"> 90/12,</span><span +class="cmtt-8"> 120/11,</span><span +class="cmtt-8"> 150/10,</span><span +class="cmtt-8"> 180/9,</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> 210/8,</span><span +class="cmtt-8"> 240/7,</span><span +class="cmtt-8"> 270/6,</span><span +class="cmtt-8"> 300/5,</span><span +class="cmtt-8"> 330/4}</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> {</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> \draw[line</span><span +class="cmtt-8"> width=1pt]</span><span +class="cmtt-8"> (\angle:1.8cm)</span><span +class="cmtt-8"> --</span><span +class="cmtt-8"> (\angle:2cm);</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> \draw</span><span +class="cmtt-8"> (\angle:1.4cm)</span><span +class="cmtt-8"> node{\textsf{\label}};</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> }</span> +<br> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> \foreach</span><span +class="cmtt-8"> \angle</span><span +class="cmtt-8"> in</span><span +class="cmtt-8"> {0,90,180,270}</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> \draw[line</span><span +class="cmtt-8"> width=2pt]</span><span +class="cmtt-8"> (\angle:1.6cm)</span><span +class="cmtt-8"> --</span><span +class="cmtt-8"> (\angle:2cm);</span> +<br> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> \draw</span><span +class="cmtt-8"> (0,0)</span><span +class="cmtt-8"> --</span><span +class="cmtt-8"> (120:0.8cm);</span><span +class="cmtt-8"> </span><span +class="cmsltt-10x-x-80">%</span><span +class="cmsltt-10x-x-80"> hour</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> \draw</span><span +class="cmtt-8"> (0,0)</span><span +class="cmtt-8"> --</span><span +class="cmtt-8"> (90:1cm);</span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmsltt-10x-x-80">%</span><span +class="cmsltt-10x-x-80"> minute</span> +<br><span +class="cmtt-8">\end{tikzpicture}</span><span +class="cmsltt-10x-x-80">%</span></div> </td></tr></table> </td> +</tr></table></div> + <!--l. 243--><p class="noindent"><div class="obeylines-v"></div><!--tex4ht:inline--><div class="tabular"> +<table class="tabular" +cellspacing="0" cellpadding="0" +frame="void" id="TBL-376-" ><colgroup id="TBL-376-1g"><col +id="TBL-376-1"><col +id="TBL-376-2"></colgroup><tr + valign="baseline" id="TBL-376-1-"><td align="left" style="white-space:nowrap; text-align:left;" id="TBL-376-1-1" +class="td11"> <object data="pgfmanual-466.svg" width="108.12007pt" height="108.12007pt" type="image/svg+xml"><p>SVG-Viewer needed.</p></object> </td><td align="left" style="white-space:nowrap; text-align:left;" id="TBL-376-1-2" +class="td11"> <table class="minipage"><tr><td><div class="obeylines-v"> +<br><span +class="cmtt-8">\tikz[shading=ball]</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> \foreach</span><span +class="cmtt-8"> \x</span><span +class="cmtt-8"> /</span><span +class="cmtt-8"> \cola</span><span +class="cmtt-8"> in</span><span +class="cmtt-8"> {0/red,1/green,2/blue,3/yellow}</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> \foreach</span><span +class="cmtt-8"> \y</span><span +class="cmtt-8"> /</span><span +class="cmtt-8"> \colb</span><span +class="cmtt-8"> in</span><span +class="cmtt-8"> {0/red,1/green,2/blue,3/yellow}</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> \shade[ball</span><span +class="cmtt-8"> color=\cola!50!\colb]</span><span +class="cmtt-8"> (\x,\y)</span><span +class="cmtt-8"> circle</span><span +class="cmtt-8"> (0.4cm);</span></div> </td></tr></table> </td> +</tr></table></div> + </dd></dl> +<!--l. 252--><p class="indent"> + + + <dl class="description"><dt class="description"> +<span +class="colorbox" id="colorbox148"><span +class="cmtt-10">\breakforeach</span></span> </dt><dd +class="description"> +<a + id="dx16-179002"></a> + <!--l. 253--><p class="noindent">If this command is given inside a <span class="obeylines-h"><span class="verb"><span +class="cmtt-10">\foreach</span></span></span> command, no further executions of the <span +class="cmsy-10"><</span><span +class="cmti-10">commands</span><span +class="cmsy-10">> </span>will + occur. However, the current execution of the <span +class="cmsy-10"><</span><span +class="cmti-10">commands</span><span +class="cmsy-10">> </span>is continued normally, so it is probably best + to use this command only at the end of a <span class="obeylines-h"><span class="verb"><span +class="cmtt-10">\foreach</span></span></span> command. + <!--l. 259--><p class="noindent"><div class="obeylines-v"></div><!--tex4ht:inline--><div class="tabular"><table class="tabular" +cellspacing="0" cellpadding="0" +frame="void" id="TBL-377-" ><colgroup id="TBL-377-1g"><col +id="TBL-377-1"><col +id="TBL-377-2"></colgroup><tr + valign="baseline" id="TBL-377-1-"><td align="left" style="white-space:nowrap; text-align:left;" id="TBL-377-1-1" +class="td11"> <object data="pgfmanual-467.svg" width="91.35823pt" height="97.35823pt" type="image/svg+xml"><p>SVG-Viewer needed.</p></object> </td><td align="left" style="white-space:nowrap; text-align:left;" id="TBL-377-1-2" +class="td11"> <table class="minipage"><tr><td><div class="obeylines-v"> +<br><span +class="cmtt-8">\begin{tikzpicture}</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> \foreach</span><span +class="cmtt-8"> \x</span><span +class="cmtt-8"> in</span><span +class="cmtt-8"> {1,...,4}</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> \foreach</span><span +class="cmtt-8"> \y</span><span +class="cmtt-8"> in</span><span +class="cmtt-8"> {1,...,4}</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> {</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> \fill[red!50]</span><span +class="cmtt-8"> (\x,\y)</span><span +class="cmtt-8"> ellipse</span><span +class="cmtt-8"> (3pt</span><span +class="cmtt-8"> and</span><span +class="cmtt-8"> 6pt);</span> +<br> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> \ifnum</span><span +class="cmtt-8"> \x<\y</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> \breakforeach</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> \fi</span> +<br><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> </span><span +class="cmtt-8"> }</span> +<br><span +class="cmtt-8">\end{tikzpicture}</span></div> </td></tr></table> </td> +</tr></table></div> + </dd></dl> + + + + + + + <div class="crosslinks"><p class="noindent">[<a +href="pgfmanualse16.html" >next</a>] [<a +href="pgfmanualse14.html" >prev</a>] [<a +href="pgfmanualse14.html#tailpgfmanualse14.html" >prev-tail</a>] [<a +href="pgfmanualse15.html" >front</a>] [<a +href="pgfmanual.html#pgfmanualse15.html" >up</a>] </p></div> +<!--l. 8--><p class="indent"> <a + id="tailpgfmanualse15.html"></a> +</body></html> |