Hot answers tagged foreach
17
To have more than one \draw (or similar) within a loop, you have to enclose them in braces ({}):
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \i in {0, 1, 2, 3} {
\draw (\i, 0) rectangle +(0.5, 0.5);
\draw (\i, 1) rectangle +(0.5, 0.5);
}
\end{tikzpicture}
\end{document}
15
There is exactly one reason why there are pgfplots equivalents to the \foreach command of pgf/tikz: scoping. Occasionally, one wants to aggregrate things, i.e. to compute a value which exists outside of the loop's body -- but which is not global either.
If you do not care about scoping and if you are not aggregrating things, the use of \foreach might be the ...
15
Here's another version, not all that different from the previously supplied but combines them in a slightly different way. In effect, it is a bit like defining a dynamic style alias that expands to the given list of options. When a style like red drawing/.style={draw,red} is defined, then calling red drawing executes \tikzset{draw,red} (sort of, actually ...
13
The \let\OldRef=\the\Ref doesn't work because \OldRef is then simply a copy of \the. Also note that \Ref here stands for \toks<number> and coping it using \let would only let both macros point to the same token register.
You can accumulate code using the \g@addto@macro macro:
\def\Ref{}
\makeatletter
\newcommand{\AddRef}[1]{%
...
13
You can use the additional facilities of foreach macro given in the manual by adding pgfmath package too. For some reason, (initially 4) option is not working if TikZ is not fully loaded so you can define it externally.
\documentclass{article}
\usepackage{pgffor,pgfmath}
\begin{document}
\def\lastx{4}
\foreach \x[count=\xi from 2,remember=\x as \lastx] ...
12
To iterate variables "simultaneously" TikZ has the following syntax: the list of variables must be separated by slashes /, and the list items can also be lists of values separated by slashes.
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw (0, 0) grid (4, 3);
\foreach \x/\angle in {0.5/20, 1.5/40, 2.5/60, 3.5/80} {
...
12
You can use the option [evaluate = <variable> as <macro> using <expression>] to calculate a new value based on a counter, which can then be used in an inner counter:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach [evaluate = \y as \n using \y*2-1] \y in {1,...,5} {
\foreach \x in {1,...,\n} {
...
12
The simplest solution that respects the question of the OP (a triangle of dots with Tikz) seems to be :
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \y in {0,...,4}
\foreach \x in {-\y,...,\y}
\fill [blue] (\x,-\y) circle [radius=0.2];
\end{tikzpicture}
\end{document}
12
Some possibilities with foreach. In your case, you can try the first one
\documentclass[11pt]{scrartcl}
\usepackage{tikz}
\begin{document}
\def\I{2}
\begin{tikzpicture}
\foreach \i [evaluate=\i] in {\I+1,...+1,19+1}
\node[anchor=center] at (\i,0) {$\i$};
\end{tikzpicture}
\begin{tikzpicture}
\foreach \i [evaluate=\i as \j using \i+1] in ...
12
I agree with Torbjørn's answer but in your case, it was possible to write the next code.
I use only one command \draw on one path. I think is important to understand what is a path to work correctly with TikZ. The end of the path is determined by ;
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \i in {0, 1, 2, ...
12
Use indirect styles numbered (perhaps more verbose but flexible):
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\tikzset{
s0/.style={draw},
s1/.style={draw,red},
s2/.style={circle,draw=blue},
s3/.style={draw},
}
\foreach \x/\content in {%
0/a,
1/b,
2/c,
3/d%
} {
\node[s\x] at (\x,0) ...
11
Section Options to customize the foreach-statement. (pages 507-508):
/pgf/foreach/count=
<macro>from<value> (no default) This
key allows <macro> to hold the
position in the list of the current
item. The optional from<value>
statement allows the counting to begin
from <value>.
So, for example, in
\foreach \x ...
11
You can produce it as follows:
\documentclass[letterpaper]{article}
\usepackage{amsmath, tikz}
\usetikzlibrary{calc}
\newcommand{\fracgraph}[3][2]{%
% #1 = optional height
\begin{tikzpicture}
\pgfmathsetmacro{\Yheight}{0.5*#1}%
\pgfmathsetmacro{\Xincrement}{#2/#3}%
\draw (0,0) rectangle (#2,#1);
\node at ($(0.5*#2,0.75*#1)$) {1};
...
11
Deep in the bowels of TikZ, the child creation code calls \foreach to create the children. However, by the time it has done so then the various parameters to the \foreach loop have passed through several macros. Each time, there is the potentiality to strip off an outer pair of braces. Rather than try to keep track of this, TikZ gets round it by ensuring ...
11
There are several reasons, why this does not work:
\lx1 is not a command name, it consists of the command \lx, followed by digit 1.
Either replace 1 by a letter or the usage is a little more complex via \csname.
\x and \y contain the x and y values, the definition of \x defines \x, not the macro inside and \y whill change with every loop. \expandafter ...
10
The \mymacro isn't expanded by the \foreach loop, but only afterwards. You need to remove the braces { } around the macro to make it work:
\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\begin{document}
Picture one:
\begin{tikzpicture}
\foreach \x/\y in {1.0/2.0, 3.0/4.0} \node[draw] at (\x,\y) {\x--\y};
\end{tikzpicture}
Picture ...
10
\foreach isn't expandable and the grouping is most likely causing some issues as well. I would recommend to use the loop outside the matrix and accumulate the rows in a macro which then only has to be expanded. The etoolbox package provides \gappto (global append to; \xappto would cause issues with fragile content) which can be used here. There is also ...
10
It's possible to use a minimum code
\documentclass{minimal}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[darkstyle/.style={circle,draw,fill=gray!40,minimum size=20}]
\foreach \x in {0,...,4}
\foreach \y in {0,...,4}
{\pgfmathtruncatemacro{\label}{\x - 5 * \y +21}
\node [darkstyle] (\x\y) at (1.5*\x,1.5*\y) {\label};}
...
10
Internally the definition of foreach will be saving the body of the loop in a macro so it is like (if looping over a,b,... )
\def\body{%
\renewcommand*{\SomeCommand}[1]{\color{red}#1}% Using ## here eliminates the error.
\par\SomeCommand{\x}%
}%
}
\def\x{a}\body
\def\x{b}\body
...
That initial \def (or \newcommand if you prefer) will require a # in ...
10
One way to do that is to use use mod function and add 1 to it.
Below I have set it to display the values mod 10 for illustrative purposes. And you see that it goes up to 10 and then starts back again at 1. For your example you should set
\newcommand*{\ModValue}{24}
Notes:
As egreg pointed out, perhaps you want the final hour to be displayed as ...
10
The tikzpicture environment is not intended to put bare text there. It resets the font to \nullfont. The text needs to be placed, for example, as node text.
If you just want to have the \foreach loop, then you do not need the tikzpicture:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\foreach \x [remember=\x as \lastx (initially A)] in ...
9
pgfplots can draw vector fields by means of its quiver plot handler.
\documentclass[a4paper]{article}
\usepackage{pgfplots}
\begin{document}
\thispagestyle{empty}
\begin{tikzpicture}
\begin{axis}[title=Quiver and plot table]
\addplot[blue,
quiver={u=\thisrow{u},v=\thisrow{v}},
-stealth]
table
{
x y u v
0 0 1 0
1 1 ...
9
Yes, foreach is a TikZ/PGF statement. It is described in the very detailed pgfmanual. You can also use it independently of TikZ/PGF by issuing \usepackage{pgffor} in your preamble. This is not necessary if you're using TikZ, as it will be loaded automatically.
Here's an example of how to achieve what you described in your question. ...
9
If you want a local version of \g@addto@macro without using up a new token register you could use something like
\long\def\@addto@macro#1#2{%
\begingroup
\toks@\expandafter{#1#2}%
\expandafter\endgroup\expandafter
\edef\expandafter#1\expandafter{\the\toks@}%
}
this uses the scratch register \toks@ without affecting its existing contents, it is basically ...
9
The let synax is perfectly valid inside a \foreach. You do however have a clash of variable names: The \y from the loop conflicts with the \y⟨n⟩ from let. Simply renaming the loop counter solves the problem:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\foreach \a in {1,2,3}
{\draw (0,0) -- ...
9
The explanation will need a bit of TeX theory.
A group is a part of the document that confines most changes made inside it only to this group itself. That is, most settings changed inside a group will be reverted to their old values once the group ends. A group is delimited by { … } or \begingroup … \endgroup. For example, a command \itshape switches the ...
9
Something is wrong with my use of the foreach macro key (initially 0). It gets stuck on the first point so I had to define it before the loop. The following works without too much emphasis on the cosmetics. The nodes A,B,C etc. can be added easily.
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=3]
\def\lastx{0} % I ...
9
You don't really need a for-each macro for this, you just need to take care that your loop counter and the groups from the table cells interact correctly.
The following produces the three row table without any large (or small) package overhead.
\documentclass{article}
\newcommand{\test}[1]{%
t1 & t2 \\
\ifnum#1>1
...
9
A manual solution, where the reversed list is constructed in a first \foreach loop:
\documentclass{article}
\usepackage{pgffor}
\begin{document}
\let\mylist\empty
\foreach\x in {1,...,4} {
\ifx\mylist\empty
\xdef\mylist{\x}%
\else
\xdef\mylist{\x,\mylist}%
\fi
}
\foreach\x in \mylist {
[\x]
}
\end{document}
9
Use \pgfmathtruncatemacro to calculate the value and truncate it (1.0 → 1).
Code
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\foreach \x [count=\xi from 1] in {-3.0,-2.8,...,1.8}
\draw[thin] (0,0) ++(\x,2) rectangle ++(0.2, 0.2) coordinate [pos=.5] (one\xi);
\foreach \x [count=\ai from 1] in {1,0.8,...,0.2}
...
Only top voted, non community-wiki answers of a minimum length are eligible