PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tex/plain/base/doc/generic/pgf/text-en/pgfmanual-en-pgffor.tex

https://github.com/joetait/texmf
LaTeX | 389 lines | 300 code | 77 blank | 12 comment | 0 complexity | 7688ca086fef6b36892698f67701d090 MD5 | raw file
Possible License(s): LGPL-2.0, CC-BY-SA-3.0, GPL-2.0
  1. % Copyright 2006 by Till Tantau
  2. %
  3. % This file may be distributed and/or modified
  4. %
  5. % 1. under the LaTeX Project Public License and/or
  6. % 2. under the GNU Free Documentation License.
  7. %
  8. % See the file doc/generic/pgf/licenses/LICENSE for more details.
  9. \section{Repeating Things: The Foreach Statement}
  10. \label{section-foreach}
  11. This section describes the package |pgffor|, which is loaded
  12. automatically by \tikzname, but not by \pgfname:
  13. \begin{package}{pgffor}
  14. This package can be used independently of \pgfname, but works
  15. particularly well together with \pgfname\ and \tikzname. It defines
  16. two new commands: |\foreach| and |\breakforeach|.
  17. \end{package}
  18. \begin{command}{\foreach| |\meta{variables}| |\opt{{\ttfamily[}\meta{options}{\ttfamily]}}%
  19. | in |\meta{list} \meta{commands}}
  20. The syntax of this command is a bit complicated, so let us go
  21. through it step-by-step.
  22. In the easiest case, \meta{variables} is a single \TeX-command like
  23. |\x| or |\point|. (If you want to have some fun, you can also use
  24. active characters. If you do not know what active characters are,
  25. you are blessed.)
  26. Still in the easiest case, \meta{options} will be omitted. The keys
  27. for customizing this command will be discussed below.
  28. Again, in the easiest case, \meta{list} is either a comma-separated
  29. list of values surrounded by curly braces or it is the name of a
  30. macro that contain such a list of values. Anything can be used as a
  31. value, but numbers are most likely.
  32. Finally, in the easiest case, \meta{commands} is some \TeX-text in
  33. curly braces.
  34. With all these assumptions, the |\foreach| statement will execute
  35. the \meta{commands} repeatedly, once for every element of the
  36. \meta{list}. Each time the \meta{commands} are executed, the
  37. \meta{variable} will be set to the current value of the list
  38. item.
  39. \begin{codeexample}[]
  40. \foreach \x in {1,2,3,0} {[\x]}
  41. \end{codeexample}
  42. \begin{codeexample}[]
  43. \def\mylist{1,2,3,0}
  44. \foreach \x in \mylist {[\x]}
  45. \end{codeexample}
  46. Note that in each execution of \meta{commands} the
  47. \meta{commands} are put in a \TeX\ group. This means that
  48. \emph{local changes to counters inside \meta{commands} do not
  49. persist till the next iteration}. For instance, if you add 1 to a
  50. counter inside \meta{commands} locally, then in the next iteration
  51. the counter will have the same value it had at the beginning of the
  52. first iteration. You have to add |\global| if you wish changes to
  53. persist from iteration to iteration.
  54. \medskip
  55. \textbf{Syntax for the commands.}
  56. Let us move on to a more complicated setting. The first
  57. complication occurs when the \meta{commands} are not some text in
  58. curly braces. If the |\foreach| statement does not encounter an
  59. opening brace, it will instead scan everything up to the next
  60. semicolon and use this as \meta{commands}. This is most useful in
  61. situations like the following:
  62. \begin{codeexample}[]
  63. \tikz
  64. \foreach \x in {0,1,2,3}
  65. \draw (\x,0) circle (0.2cm);
  66. \end{codeexample}
  67. However, the ``reading till the next semicolon'' is not the whole
  68. truth. There is another rule: If a |\foreach| statement is directly
  69. followed by another |\foreach| statement, this second foreach
  70. statement is collected as \meta{commands}. This allows you to write
  71. the following:
  72. \begin{codeexample}[]
  73. \begin{tikzpicture}
  74. \foreach \x in {0,1,2,3}
  75. \foreach \y in {0,1,2,3}
  76. {
  77. \draw (\x,\y) circle (0.2cm);
  78. \fill (\x,\y) circle (0.1cm);
  79. }
  80. \end{tikzpicture}
  81. \end{codeexample}
  82. \medskip
  83. \textbf{The dots notation.}
  84. The second complication concerns the \meta{list}. If this
  85. \meta{list} contains the list item ``|...|'', this list item is replaced
  86. by the ``missing values.'' More precisely, the following happens:
  87. Normally, when a list item |...| is encountered, there should
  88. already have been \emph{two} list items before it, which where
  89. numbers. Examples of \emph{numbers} are |1|, |-10|, or
  90. |-0.24|. Let us call these numbers $x$ and $y$ and let $d := y-x$ be
  91. their difference. Next, there should also be one number following
  92. the three dots, let us call this number~$z$.
  93. In this situation, the part of the list reading
  94. ``$x$|,|$y$|,...,|$z$'' is replaced by ``$x$, $x+d$, $x+2d$, $x+3d$,
  95. \dots, $x+md$,'' where the last dots are semantic dots, not
  96. syntactic dots. The value $m$ is the largest number such that $x +
  97. md \le z$ if $d$ is positive or such that $x+md \ge z$ if $d$ is
  98. negative.
  99. Perhaps it is best to explain this by some examples: The following
  100. \meta{list} have the same effects:
  101. |\foreach \x in {1,2,...,6} {\x, }| yields \foreach \x in {1,2,...,6} {\x, }
  102. |\foreach \x in {1,2,3,...,6} {\x, }| yields \foreach \x in {1,2,3,...,6} {\x, }
  103. |\foreach \x in {1,3,...,11} {\x, }| yields \foreach \x in {1,3,...,11} {\x, }
  104. |\foreach \x in {1,3,...,10} {\x, }| yields \foreach \x in {1,3,...,10} {\x, }
  105. |\foreach \x in {0,0.1,...,0.5} {\x, }| yields \foreach \x in {0,0.1,...,0.5} {\x, }
  106. |\foreach \x in {a,b,9,8,...,1,2,2.125,...,2.5} {\x, }| yields \foreach \x in {a,b,9,8,...,1,2,2.125,...,2.5} {\x, }
  107. As can be seen, for fractional steps that are not multiples of
  108. $2^{-n}$ for some small $n$, rounding errors can occur pretty
  109. easily. Thus, in the second last case, |0.5| should probably be
  110. replaced by |0.501| for robustness.
  111. There is another special case for the |...| statement: If the
  112. |...| is used right after the first item in the list, that is, if
  113. there is an $x$, but no $y$, the difference $d$ obviously cannot be
  114. computed and is set to $1$ if the number $z$ following the dots is
  115. larger than $x$ and is set to $-1$ if $z$ is smaller:
  116. |\foreach \x in {1,...,6} {\x, }| yields \foreach \x in {1,...,6} {\x, }
  117. |\foreach \x in {9,...,3.5} {\x, }| yields \foreach \x in {9,...,3.5} {\x, }
  118. There is a yet a further special case for the |...| statement, in that
  119. it can indicate an alphabetic character sequence:
  120. |\foreach \x in {a,...,m} {\x, }| yields \foreach \x in {a,...,m} {\x, }
  121. |\foreach \x in {Z,X,...,M} {\x, }| yields \foreach \x in {Z,X,...,M} {\x, }
  122. A final special case for the |...| statement is contextual replacement.
  123. If the |...| is used in some context, for example, |sin(...)|, this
  124. context will be interpreted correctly, provided that the list items
  125. prior to the |...| statement have \emph{exactly} the same pattern,
  126. except that, instead of dots, they have a number or a character:
  127. |\foreach \x in {2^1,2^...,2^7} {$\x$, }| yields \foreach \x in {2^1,2^...,2^7} {$\x$, }
  128. |\foreach \x in {0\pi,0.5\pi,...\pi,3\pi} {$\x$, }| yields \foreach \x in {0\pi,0.5\pi,...\pi,3\pi} {$\x$, }
  129. |\foreach \x in {A_1,..._1,H_1} {$\x$, }| yields \foreach \x in {A_1,..._1,H_1} {$\x$, }
  130. \textbf{Special handling of pairs.}
  131. Different list items are separated by commas. However, this causes a
  132. problem when the list items contain commas themselves as pairs like
  133. |(0,1)| do. In this case, you should put the items containing commas
  134. in braces as in |{(0,1)}|. However, since pairs are such a natural
  135. and useful case, they get a special treatment by the |\foreach|
  136. statement. When a list item starts with a |(| everything up to the
  137. next |)| is made part of the item. Thus, we can write things like
  138. the following:
  139. \begin{codeexample}[]
  140. \tikz
  141. \foreach \position in {(0,0), (1,1), (2,0), (3,1)}
  142. \draw \position rectangle +(.25,.5);
  143. \end{codeexample}
  144. \medskip
  145. \textbf{Using the foreach-statement inside paths.}
  146. \tikzname\ allows you to use a |\foreach| statement inside a path
  147. construction. In such a case, the \meta{commands} must be path
  148. construction commands. Here are two examples:
  149. \begin{codeexample}[]
  150. \tikz
  151. \draw (0,0)
  152. \foreach \x in {1,...,3}
  153. { -- (\x,1) -- (\x,0) }
  154. ;
  155. \end{codeexample}
  156. \begin{codeexample}[]
  157. \tikz \draw \foreach \p in {1,...,3} {(\p,1)--(\p,3) (1,\p)--(3,\p)};
  158. \end{codeexample}
  159. \medskip
  160. \textbf{Multiple variables.}
  161. You will often wish to iterate over two variables at the same
  162. time. Since you can nest |\foreach| loops, this is normally
  163. straight-forward. However, you sometimes wish variables to
  164. iterate ``simultaneously.'' For example, we might be given a list of
  165. edges that connect two coordinates and might wish to iterate over
  166. these edges. While doing so, we would like the source and target of
  167. the edges to be set to two different variables.
  168. To achieve this, you can use the following syntax: The
  169. \meta{variables} may not only be a single \TeX-variable. Instead, it
  170. can also be a list of variables separated by slashes (|/|). In this
  171. case the list items can also be lists of values separated by
  172. slashes.
  173. Assuming that the \meta{variables} and the list items are lists of
  174. values, each time the \meta{commands} are executed, each of the
  175. variables in \meta{variables} is set to one part of the list making
  176. up the current list item. Here is an example to clarify this:
  177. \example |\foreach \x / \y in {1/2,a/b} {``\x\ and \y''}| yields
  178. \foreach \x / \y in {1/2,a/b} {``\x\ and \y''}.
  179. If some entry in the \meta{list} does not have ``enough'' slashes,
  180. the last entry will be repeated. Here is an example:
  181. \begin{codeexample}[]
  182. \begin{tikzpicture}
  183. \foreach \x/\xtext in {0,...,3,2.72 / e}
  184. \draw (\x,0) node{$\xtext$};
  185. \end{tikzpicture}
  186. \end{codeexample}
  187. Here are more useful examples:
  188. \begin{codeexample}[]
  189. \begin{tikzpicture}
  190. % Define some coordinates:
  191. \path[nodes={circle,fill=examplefill,draw}]
  192. (0,0) node(a) {a}
  193. (2,0.55) node(b) {b}
  194. (1,1.5) node(c) {c}
  195. (2,1.75) node(d) {d};
  196. % Draw some connections:
  197. \foreach \source/\target in {a/b, b/c, c/a, c/d}
  198. \draw (\source) .. controls +(.75cm,0pt) and +(-.75cm,0pt)..(\target);
  199. \end{tikzpicture}
  200. \end{codeexample}
  201. \begin{codeexample}[]
  202. \begin{tikzpicture}
  203. % Let's draw circles at interesting points:
  204. \foreach \x / \y / \diameter in {0 / 0 / 2mm, 1 / 1 / 3mm, 2 / 0 / 1mm}
  205. \draw (\x,\y) circle (\diameter);
  206. % Same effect
  207. \foreach \center/\diameter in {{(0,0)/2mm}, {(1,1)/3mm}, {(2,0)/1mm}}
  208. \draw[yshift=2.5cm] \center circle (\diameter);
  209. \end{tikzpicture}
  210. \end{codeexample}
  211. \begin{codeexample}[]
  212. \begin{tikzpicture}[line cap=round,line width=3pt]
  213. \filldraw [fill=examplefill] (0,0) circle (2cm);
  214. \foreach \angle / \label in
  215. {0/3, 30/2, 60/1, 90/12, 120/11, 150/10, 180/9,
  216. 210/8, 240/7, 270/6, 300/5, 330/4}
  217. {
  218. \draw[line width=1pt] (\angle:1.8cm) -- (\angle:2cm);
  219. \draw (\angle:1.4cm) node{\textsf{\label}};
  220. }
  221. \foreach \angle in {0,90,180,270}
  222. \draw[line width=2pt] (\angle:1.6cm) -- (\angle:2cm);
  223. \draw (0,0) -- (120:0.8cm); % hour
  224. \draw (0,0) -- (90:1cm); % minute
  225. \end{tikzpicture}%
  226. \end{codeexample}
  227. \begin{codeexample}[]
  228. \tikz[shading=ball]
  229. \foreach \x / \cola in {0/red,1/green,2/blue,3/yellow}
  230. \foreach \y / \colb in {0/red,1/green,2/blue,3/yellow}
  231. \shade[ball color=\cola!50!\colb] (\x,\y) circle (0.4cm);
  232. \end{codeexample}
  233. \medskip
  234. \textbf{Options to customize the foreach-statement.}
  235. The keys described below can be used in the \meta{options} argument
  236. to the |\foreach| command. They all have the path |/pgf/foreach/|,
  237. however, the path is set automatically when \meta{options} are
  238. parsed, so it does not have to explicitly stated.
  239. \begin{key}{/pgf/foreach/var=\meta{variable}}
  240. This key provides an alternative way to specify variables:
  241. |\foreach [var=\x,var=\y]| is the same as |\foreach \x/\y|.
  242. If used, this key should be used before the other keys.
  243. \end{key}
  244. \begin{key}{/pgf/foreach/evaluate=\meta{variable}| |\opt{|as |\meta{macro}| using |\meta{formula}}}
  245. By default list items are not evaluated: |1+2|, yields |1+2|,
  246. not |3|. This key allows a variable to be evaluated using the
  247. mathematical engine. The variable must have been specified either
  248. using the |var| key or in the \meta{variables} argument of the
  249. |foreach| command.
  250. By default, the result of the evaluation will be stored in
  251. \meta{variable}. However, the optional |as |\meta{macro} statement
  252. can be used to store the result in \meta{macro}.
  253. \begin{codeexample}[]
  254. \foreach \x [evaluate=\x] in {2^0,2^...,2^8}{$\x$, }
  255. \end{codeexample}
  256. \begin{codeexample}[]
  257. \foreach \x [evaluate=\x as \xeval] in {2^0,2^...,2^8}{$\x=\xeval$, }
  258. \end{codeexample}
  259. The optional |using |\meta{formula} statement means an evaluation
  260. does not have to be explicitly stated for each item in \meta{list}.
  261. The \meta{formula} should contain at least one reference to
  262. \meta{variable}.
  263. \begin{codeexample}[]
  264. \tikz\foreach \x [evaluate=\x as \shade using \x*10] in {0,1,...,10}
  265. \node [fill=red!\shade!yellow, minimum size=0.65cm] at (\x,0) {\x};
  266. \end{codeexample}
  267. \end{key}
  268. \begin{key}{/pgf/foreach/remember=\meta{variable}| as |\meta{macro}| |\opt{|(initially |\meta{value}|)|}}
  269. This key allows the item value stored in \meta{variable} to be
  270. remembered during the next iteration, stored in \meta{macro}.
  271. If a variable is evaluated, the result of this evaluation is
  272. remembered.
  273. By default the value of \meta{variable} is zero for the first
  274. iteration, however, the optional |(initially |\meta{value}|)|
  275. statement, allows the \meta{macro} to be initially defined
  276. as \meta{value}.
  277. \begin{codeexample}[]
  278. \foreach \x [remember=\x as \lastx (initially A)] in {B,...,H}{$\overrightarrow{\lastx\x}$, }
  279. \end{codeexample}
  280. \end{key}
  281. \begin{key}{/pgf/foreach/count=\meta{macro}| |\opt{|from |\meta{value}}}
  282. This key allows \meta{macro} to hold the position in the list of
  283. the current item. The optional |from |\meta{value} statement allows
  284. the counting to begin from \meta{value}.
  285. \begin{codeexample}[]
  286. \tikz[x=0.75cm,y=0.75cm]
  287. \foreach \x [count=\xi] in {a,...,e}
  288. \foreach \y [count=\yi] in {\x,...,e}
  289. \node [draw, top color=white, bottom color=blue!50, minimum size=0.666cm]
  290. at (\xi,-\yi) {$\mathstrut\x\y$};
  291. \end{codeexample}
  292. \end{key}
  293. \end{command}
  294. \begin{command}{\breakforeach}
  295. If this command is given inside a |\foreach| command, no further
  296. executions of the \meta{commands} will occur. However, the current
  297. execution of the \meta{commands} is continued normally, so it is
  298. probably best to use this command only at the end of a |\foreach|
  299. command.
  300. \begin{codeexample}[]
  301. \begin{tikzpicture}
  302. \foreach \x in {1,...,4}
  303. \foreach \y in {1,...,4}
  304. {
  305. \fill[red!50] (\x,\y) ellipse (3pt and 6pt);
  306. \ifnum \x<\y
  307. \breakforeach
  308. \fi
  309. }
  310. \end{tikzpicture}
  311. \end{codeexample}
  312. \end{command}