{foreach} is for questions regarding the TikZ/PGF module of the same name which can also be used as a standalone package pgffor.

learn more… | top users | synonyms

3
votes
1answer
143 views

pass a set of numbers to pgfplotsinvokeforeach <list>

Is it possible to pass a numeric set to the list argument of the \pgfplotsinvokeforeach command? I need something like this: \newcommand{\a}{1,2,...,10} \pgfplotsinvokeforeach{\a} {<...>} ...
14
votes
2answers
487 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 ...
3
votes
2answers
233 views

\DTLdeletedb (from datatool package) has no effect within \foreach

This question led to a new feature in a package: datatool \DTLgdeletedb is available in (v2.13) and later. I am trying to use a \foreach loop to iterate over a few items. For each iteration ...
7
votes
1answer
505 views

Scalar field in pgfplots

I want to make a PGF plot which, besides normal 2D graph, includes scalar field, that is values of the 2D function in various positions of the 2D graph. Of course, I tried to do that using \foreach ...
4
votes
1answer
517 views

Using foreach loop variable as node label in pgfplots

I have a (multi)line plot generated from a table in pgfplots: \begin{document} \begin{tikzpicture} \begin{axis} \pgfplotstableread{table.tsv}\loadedtable \foreach \metavar in {foo,bar} { ...
4
votes
1answer
2k views

Iterate with `\foreach` over multiple variables and '\remember' one of them does not work as expected

I'm not so new to TikZ but am now trying something a little more advanced and am puzzled by the following problem. From researching solutions, I see that lots of good answers with good explanations ...
6
votes
2answers
553 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 ...
6
votes
1answer
547 views

Problem setting a flag inside a TikZ `\foreach` loop

I'm pulling my hair out trying to understand why the following doesn't work. The intention is to calculate whether a given number #1 is prime, and to set \isprime to either 1 (true) or 0 (false). The ...
7
votes
1answer
630 views

Using \Alph on \foreach loop argument

Here's basically what I try to do: \begin{tikzpicture} \foreach \x in {1,2,3,4,5} { \draw (\x,1) node{\Alph{\x}}; } \end{tikzpicture} However if I do that, I get ERROR: Missing number, ...
5
votes
1answer
939 views

foreach loop and calc

Why doesn't \posx in the following loop change? I have added manually what should be the result (second picture on the left). Any way to fix this? \documentclass{article} \usepackage{tikz} ...
7
votes
4answers
3k views

tikz foreach index in variable name

I want to have a set of variables holding numerical values (e.g., cnt1, cnt2, cnt3) and access them based on the foreach index. For example: \foreach \i in {1,...,5} { ...
3
votes
1answer
319 views

foreach within \draw, without giving the first point explicitly

I am plotting line given by y(x) expression evaluated at regularly spaced points, e.g. \def\ffIiC{+0.0436275-1.18778*\x^1+0.542693*\x^2} % ... \draw (0,0.0436275) \foreach\x in {0,.1,...,4}{ -- ...
12
votes
3answers
1k 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) {+}; ...
5
votes
1answer
594 views

TikZ, foreach and sum

I'm trying to create a list of rectangles of various sizes, starting at the end of the previous rectangle. I'm using a foreach loop with a TeX length, but I don't get the result I want, and I really ...
8
votes
2answers
542 views

TikZ: can I get an arbitrary list format for foreach?

I'd like very much to define a foreach loop that looks like this: \foreach \pointA-\pointB in {(1,0)-(2,2),(3,4)-(2,1)}{ \draw \pointA -- \pointB } so that for each couple of points the lines ...
6
votes
1answer
1k views

Using \evaluate in nested \foreach loops with TikZ

I would like to draw two grids side by side using TikZ; the left one being a complete square grid, and the right one being a lower-triangular one. I tried writing the following code to make the ...
6
votes
3answers
1k views

Tikz foreach with two variables and the remember option

The question is really simple but I am a beginner in the world of tikz. I have the following example and I am trying to simplify the connections of the nodes. Here the working example: ...
4
votes
2answers
954 views

Filling triangle doesn't seem to work with PGF/TikZ

I was wondering if anyone had any clue on why my triangles are not filled in red within the following piece of code: \documentclass{article} \usepackage{tikz} \usetikzlibrary{calc,3d} ...
2
votes
1answer
701 views

Using an array element to specify end iteration point in TikZ “\foreach”?

Is it possible to access an array and use one of its elements to specify the last iteration point in a TikZ \foreach? I've tried with the code below but I keep getting a compilation error: ! ...
5
votes
3answers
267 views

Correcting TikZ code

foreach how to fix the code below so that the center of the figure is at the origin (0,0)? \begin{tikzpicture} \fill[blue] circle (2pt) node[right] {$(0,0)$}; \draw (-3,1) -- (3,1); \draw[blue] ...
6
votes
2answers
2k views

How to concatenate strings, to e.g. create commands that return lists?

I was thinking about Felix' Question, where he wanted to return lists in TikZ. My approach so far was to return the single results as individual \nodes, separated one centimeter each. Now I would like ...
6
votes
1answer
1k views

Combining TikZ foreach and let operation

I'm not having any luck using the TikZ let operation inside a foreach loop. Is there anything I'm missing? Sample code (that doesn't work): \begin{tikzpicture} \foreach \y in {1,2,3} {\draw ...
5
votes
1answer
445 views

Repeating preactions

I'm trying to use multiple similar preactions. Since I would prefer to automatise that I tried to use foreach with preactions. But it appears that's not working. Let's see an example ...
6
votes
1answer
488 views

External images displayed in circle

I'd like to have several external images organized in a circular shape, with TikZ drawing on top of it. I tried the scope option in TikZ with \includegraphics command, the whole thing being included ...
5
votes
1answer
811 views

How can I use \foreach in an axis environment of pgfplots?

I'm having a pretty weird issue with TikZ and pgfplots. I want to have a legend driven by a \foreach variable, but TikZ won't let me do that. I tried a couple of different stuff and realized that ...
7
votes
3answers
1k views

How to get the list of predefined colors in xcolor such that the list can be used in foreach looping?

I need to get the list of predefined colors in xcolor. Hard-coding as follows is not a good coding practice. \documentclass[dvipsnames,cmyk]{minimal} \parindent=0pt \usepackage{pstricks} ...
4
votes
1answer
304 views

More: Generalised: Accessing the logic values of a TikZ coordinate

This is a follow-up question to: Generalised: Accessing the logic values of a TikZ coordinate. In that question a PGFplot answer was suggested by jake. I took that answer and modified it to the ...
7
votes
4answers
495 views

How to create a triangle of dots in TikZ using the foreach command?

How to create a triangle of dots in TikZ using the foreach command? I need this.
1
vote
1answer
3k views

Why does the placement of a \COMMENT in the algorithmic enviroment matter?

I'm using the algorithmic package. When I start off a FORALL with a COMMENT, LaTeX behaves as though I've created an invalid loop body. If I put it as the second line, with a STATE first, all is well. ...
8
votes
2answers
270 views

Preparing macro content in a loop (calling \foreach from \edef)

I need to do something like \def\MyArray{{1, 2, 3, 4}} but with a dynamical number of elements. The following command makes the string: \newcommand{\MakeArray}[1]{\{ 1 \foreach \x in {2, ..., #1}{ , ...
10
votes
1answer
1k 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} ...
1
vote
2answers
291 views

\pgfmathdectobase macro \pgfmathresult digit index or bitwise operators

Is there a way to access each digit of a macro returned by \pgfmathdectobase (or \pgfmathresult) which is a binary number ? This would be useful for a function which prints the power set of a given ...
3
votes
2answers
385 views

binary representation of an integer

Is there a way to get a binary string of an integer calculated by e.g., \pgfmathparse{pow(2,3)} \pgfmathresult such that later on I can use the string digits in a foreach loop like .. if it is '1' do ...
10
votes
3answers
509 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 ...
12
votes
1answer
1k 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 it's \foreach use this. It is also mentioned in Increment loop variable in ...
12
votes
3answers
3k 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); ...
10
votes
1answer
3k 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 ...
5
votes
3answers
358 views

Is there a \foreach function that works in the preamble?

Tikz/PGF has an extremely useful implementation of \foreach, which allows for instance the construction \foreach \x / \y in {1/a,2/b,3/c,4/d} {\x is a lot like \y} in which you can iterate over a ...
9
votes
1answer
4k 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 ...
20
votes
2answers
2k 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 ...
8
votes
2answers
2k 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. ...
3
votes
1answer
403 views

Use of \foreach and \csname<macro name>\endcsname together

When I compile this minimum LaTeX file: \documentclass{report} \def\defMacro#1#2{\expandafter\def\csname#1\endcsname{#2}} \def\useMacro#1{\csname#1\endcsname} \usepackage{pgffor} \begin{document} ...
14
votes
4answers
2k 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} ...
7
votes
1answer
831 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 ...
15
votes
3answers
360 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 ...
15
votes
1answer
3k 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} ...
7
votes
2answers
1k 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 ...
6
votes
2answers
4k views

How to use parameter numbers and \foreach

Is there a way to use the TikZ \foreach command to do operations with parameters? E.g. I would like to be able to write (just a silly example): \newcommand{\foo}[9]{ \foreach \x in{1,2,...,9}{ ...
3
votes
1answer
1k views

Drawing a Markov chain - How do I update the value of a variable in TikZ?

I would like to plot a simulation of a Markov process in R^2. Basically, I want to draw a line that starts at (x_0,y_0)=(0,2) and then follow this rule: For each i=1,...,10, the line will go through ...
4
votes
1answer
344 views

Are there list building macros in PGF/Tikz?

In Mathematica, C# linq, or other languages, we can use auxiliary functions to produce list of certain pattern. For example: In Mathematica, we can use Table[] or Range[] to produce a list of ...