Questions tagged [foreach]
{foreach} is for questions regarding the TikZ/PGF module of the same name which can also be used as a standalone package pgffor.
798
questions
59
votes
3
answers
77k
views
TikZ: Using Loop to Draw Grid of Nodes
I am trying to draw an Ising model (in my case, a grid of nodes with an undirected edge between adjacent nodes on the same row or column) using TikZ. I have the grid of nodes working nicely using a ...
43
votes
1
answer
16k
views
what is the [count=\i] in \foreach for?
Can someone explain what does [count=\i] in a foreach mean?
Antal S-Z's answer to Fitting a list of points with TikZ and its \foreach use this. It is also mentioned in Increment loop variable in ...
40
votes
2
answers
9k
views
TikZ \foreach loop with macro-defined list
I have a small problem with the TikZ \foreach loop whenever the list, over which we loop, is defined my a macro. The following code example will show my problem:
\documentclass{scrartcl}
\usepackage[...
34
votes
2
answers
9k
views
\foreach not behaving in axis environment
Am hesitant to ask yet one more embarrassing question, but I can't seem to see the problem with the \foreach in the MWE. It should produce several horizontal lines but only produces the one with the ...
34
votes
3
answers
2k
views
Why doesn't TikZ's \foreach iterate over the last element of the list?
This code
\documentclass{report}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[dashed] (1,-1) -- (1,2) node[above] {$1$};
\draw[dashed] (2,-1) -- (2,2) node[above] {$2$};
\foreach ...
33
votes
3
answers
7k
views
PGFplots foreach equivalent to TikZ's with multiple variables separated by a slash
I like TikZ's capability to traverse multiple variables separated by a slash in the foreach (as in the example below). I am looking for something equivalent in PGFplots? I saw in the documentation ...
32
votes
2
answers
25k
views
simple arithmetic on index of foreach statement in tikz
Is foreach really a tikz statement or is derived from some other package?
I would like to draw a horizontal axis with 7 ticks spaced 1 cm apart.
I'd like to place a label under each tick that is the ...
27
votes
1
answer
32k
views
Increments in \foreach loop with two variables, TikZ
I'm trying to draw a stack of arrows at different angles in a \foreach loop in TikZ. I don't want to have to declare every z coordinate and angle manually so I was using the {1,2,...,10} syntax but ...
26
votes
4
answers
10k
views
pgffor: Special treatment for last item in \foreach-list
Is there a way for handling the last item in a \foreach-list in a special way. An example might illustrate an application for that:
\documentclass{minimal}
\usepackage{pgffor}
\begin{document}
\verb|\...
21
votes
5
answers
5k
views
Multiple TikZ keys with foreach
Playing around with pgffor, I'm trying to slightly modify an example from the TikZ/pgf manual:
\path \foreach \x/\content/\style in {%
0/a/draw,
1/b/{draw,red},
2/c/{circle,blue},
3/d/draw%
} ...
21
votes
6
answers
2k
views
How to change the item separator in TikZ foreach command
I want to cycle over several sentences using a foreach loop. Now sentences will prpbably contain commas, which are used by foreach as item separators. So I would like to change the item separator to ...
20
votes
3
answers
10k
views
TikZ Drawing a changing vector field in a grid
So far I succeeded in drawing a simple grid along with it's centres using grid and nodes.
\draw[step=1] (0, 0) grid (4, 3);
\coordinate (a) at (0.4, 0.3);
\coordinate (b) at (3.8, 0.8);
\draw[fill=...
19
votes
3
answers
6k
views
Draw a path between many nodes using foreach
I'm running into a weird problem here... I have very many nodes defined, and I would like to draw a path from the first to the last using foreach. But, when I try, it just draws edges between p1 and ...
19
votes
2
answers
3k
views
Getting last value in tikz foreach
I tried this, but it doesn't work as intended:
\foreach \x/\index in {4/0,5/1,19/2} {
\ifnum \index > 0
% use lastx
\fi
\pgfmathsetmacro\lastx{\x}
}
How do I set \lastx properly?
18
votes
3
answers
5k
views
Computing in the list of a tikz foreach
If I say something along the lines of
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\def\I{2}
\foreach \i in { \I+1, ..., 20 }
\node at (\i,10) {+};
\end{...
18
votes
1
answer
3k
views
Foreach inside a TikZ matrix
Consider this MWE:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix [matrix of nodes] {
a \\
b \\
c \\
};
\end{tikzpicture}
\end{...
17
votes
3
answers
3k
views
How do I use the ampersand (&) inside a foreach or conditional (or other group/environment) when building tables?
I am trying to build a table, and inside the tabularx environment I would like to have a loop, something like (here is a MWE):
\documentclass[10pt,a4paper]{article}
\usepackage{tabularx}
\usepackage{...
17
votes
3
answers
817
views
How keep a running list of strings and then process them one at a time
I would like to keep track of all the \AddRef commands in a document and then process them one at a time, for instance print them in the document on one line per value. It seems that this should be ...
17
votes
1
answer
1k
views
Why do paramaters of \renewcommand need to double up the # within a \foreach
Introduction:
When you use \renewcommand within another macro or environment, one needs to double up the ## to access the parameters of the inner macro. This serves as a way to distinguish the inner ...
16
votes
1
answer
13k
views
TikZ \foreach loop evaluate variable using pgfmath function
I have problem with defining a new variable within the \foreach loop using the evaluate option using a pgfmath function:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{...
15
votes
3
answers
3k
views
Copy table row n times
I would like to copy the row of a table, let say, n times, but unfortunately my sample below does not compile. It is my understanding that the \foreach command should copy what is in its argument but ...
15
votes
1
answer
7k
views
Increment loop variable in inner foreach loop
I need the following pairs of numbers:
12 13 14
23 24
34
My first attempt was to use two nested foreach loops:
\foreach \x in {1,...,4} {
\foreach \y in {\x+1,...,4} { % This would be very easy in ...
15
votes
1
answer
823
views
Why does \foreach \x in {0,1,...,0}{} perform two iterations (instead of one)?
See the following MWE to understand what I mean better.
\documentclass[preview,border=12pt]{standalone}
\usepackage{pgffor,multido}
\begin{document}
\section*{foreach}
\foreach \ix in {0,1,...,3}{%
...
14
votes
8
answers
917
views
Shading 25 squares within a square using `\foreach`
In a square of edge length 5*sqrt(2), I have 45 congruent squares of edge length sqrt(2) drawn. I would like to shade 25 of them - 5 of them in each row. I shaded the 5 squares in the bottom row and ...
14
votes
2
answers
2k
views
Error with \foreach in TikZ
In the MWE below
\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 +(...
14
votes
1
answer
8k
views
Tikz binomial distribution
I'm trying to plot the pmf of the binomial distribution for particular values of N and p. For example, when N=10 and p=0.5:
\documentclass{article}
\usepackage{amsmath}
\usepackage{pgfplots}
\...
14
votes
4
answers
3k
views
Macro to access a specific member of a list
I want to be able to have a macro access a specific element of a list. The following code works, however requires me to have two separate macros: one that accepts a list, and the other that accepts ...
13
votes
1
answer
7k
views
Repeating the same column type
I'm using package numprint to create a table which automatically formats its columns. However, the table has many columns.
Right now, my table definition looks like this:
\begin{tabular}{l n{2}{3} n{...
13
votes
3
answers
419
views
I encountered the following problems when using foreach:
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \x in{0,0.1,...,0.5}{
\node[] at (20*\x,0) {\x};
}
\end{tikzpicture}
\end{document}
...
13
votes
2
answers
10k
views
Nested foreach loop in TikZ
I would like to draw some vector fields on a triangle, and I am using nested \foreach loop to do that, however in the inner loop, you can't use the outer loop variable as the numeric range. For e.g. ...
13
votes
2
answers
2k
views
number formatting in foreach statement of tikz
I have the following code:
\begin{tikzpicture}
\foreach \i [evaluate=\i as \ii using \i+5] in {1,2,3,4} {
\node at (\i,0) {\ii};
}
\end{tikzpicture}
I like to print 6 7 8 9, but the above code ...
13
votes
1
answer
1k
views
How to expand a macro used in the range of a foreach loop?
Motivating Example:
Suppose that I want to draw the following tree:
root
/ / \ / A B C D
This is easy enough to do by explicitly drawing the two subtrees
as children ...
12
votes
6
answers
2k
views
How can I create a comma-separated list of characters from a text string?
How can I split the string Hello World! - stored into a macro \def\mystring{Hello Word!} - into a comma-separated list of characters (including spaces) which can be used as argument of a \foreach loop ...
12
votes
5
answers
2k
views
tikz loops, creating triangle with jagged hypotenuse
I am in dire need of constructing the image below.
In short I am trying to create an exact replica of the image,
and an alternative image, with a much more jagged hypotenuse.
I thought about using a ...
12
votes
2
answers
3k
views
Tikz foreach inside matrix
Apparently this question has been asked a hundred times already. But I couldn't get it to work with any of the answers provided.
I want to have nested for loops inside a Tikz matrix. Something like:
...
12
votes
2
answers
3k
views
\pgfmath and \foreach in TikZ
I am quite new to TikZ. I have the following code:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{chains}
\begin{document}
\begin{tikzpicture}[start chain=circle placed {at=(\...
12
votes
4
answers
454
views
number of arguments that is a multiple of a number in a macro for curves with continuously varying thickness
In some mathematical figures it's nice to vary curve thickness smoothly along a path. That's for reproducing hand drawn mathematical figures made with inking liners.
\documentclass{article}\...
12
votes
2
answers
909
views
Graphing Trigonometric Functions
In my attempt to graph the transformation of a trigonometric function, I found myself having to work section by section. I researched the \foreach function to shorten the code and make it more robust ...
12
votes
3
answers
2k
views
Using \foreach inside a draw command using TIKZ
I'm brand new to using \foreach but not coding in general. Following the code in answer number 40 of this example: How to draw tangent line of an arbitrary point on a path in TikZ
How can I properly ...
12
votes
2
answers
2k
views
Iterate over space-separated list
I’ll briefly explain the background in case this can be solved simpler but I’d still be interested in an answer to the actual question, too.
My chapter titles are very narrow and use a large font so ...
12
votes
1
answer
3k
views
Sum and Difference on the number line using TikZ
I am trying to replicate the idea of sum and difference of real numbers on the number line and have been successful in some way. For example, the code:
\documentclass[letterpaper]{article}
\usepackage{...
12
votes
2
answers
2k
views
Generating tables with \@for command
I'm making a report template that requires the list of authors to be presented in several different ways, one of which is a table that lists each author and provides a space for them to sign the ...
12
votes
1
answer
132
views
Smarter way for a drawing
Consider the following MWE:
\documentclass[border=5pt]{article}
\usepackage{tikz,anysize}
\marginsize{0cm}{0cm}{0cm}{0cm}
\definecolor{forest}{RGB}{34,139,34}
\definecolor{rot}{RGB}{220,20,60}
\...
11
votes
3
answers
3k
views
pgf plot and foreach loop
I have this function y=a*x^2 where a changes (-2.4,-2.1,-1.8,....,2.1,2.4) and i want to draw it with pgf plots. Problem is i dont know how to implement this i think i'll need foreach loop or ...
11
votes
2
answers
3k
views
\foreach with two or more variables
I often find myself wanting to use some feature that allows me to loop through several sets where the nth item in each list is paired together with the nth item in the other lists.
\begin{tikzpicture}...
11
votes
3
answers
870
views
tikz foreach loops: is there an equivalent of metapost's step?
I apologise if I'm using and searching with incorrect terminology. This seems an obvious question (to me) so I'm surprised I've not found anything (and so assume I'm missing something).
In metapost, ...
11
votes
3
answers
4k
views
pgfplots: use foreach inside addplot
I want to add a bunch of evenly spaced vertical lines to a plot using \foreach.
I can do it like this:
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\...
11
votes
3
answers
686
views
Using \foreach loop in Cubic B-spline curve refinement
This question is only about TikZ (/PGF) implementation. This is a MWE of what I want to ask for:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}...
11
votes
1
answer
1k
views
foreach with colors leads to "undefined control sequence"
The following MWE works nicely when I remove the optional argument [\forcol] from the \addplotcommand or when I change it to red, for example. Though I would like the curves to have different colors ...
11
votes
1
answer
702
views
\foreach has a problem with ‘initially’ argument in remember part
Is there an error in my code or did I find a bug? It seems like the \foreach statement doesn’t increase \lastn if there is an initally (value) but it works with a manually given initially value (\def\...