Tagged Questions

{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

6
votes
3answers
198 views

How to “demux” several aspects from a `\foreach` variable with `\ifstrequal`?

How can I "demux" several features from a mode specification given in a \foreach? I would like to use a \foreach to draw several nodes in a TikZ picture. The loop will specify a mode for each ...
2
votes
2answers
404 views

forarray and a \gdef list parameter

\documentclass{article} \usepackage{forarray} \begin{document} \newcommand{\mytext}[1]{\gdef\themytext{#1}} \newcommand{\mytest}[1]{\ForEach{,}{\typeout{\thislevelitem}}{#1}} % this one works, ...
9
votes
2answers
811 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: ...
2
votes
3answers
233 views

Process Last Item in \ForEach (forarray package)

The following command of the forarray package \ForEach{,}{\thislevelitem(\the\thislevelcount) : }}{x,y,z} produces the output x(1) : y(2) : z(3) : I want to have the same output without the last ...
6
votes
1answer
301 views

Fill Nodes according to table/data file

I have a lattice of nodes generated by two \foreach commands. So each node has two numbers which define its location (essentially x and y coordinates). Is there any way to fill a circle at that node ...
12
votes
5answers
1k 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 ...
8
votes
1answer
689 views

Creating a fraction graph in TikZ

Ok, so I was helping a friend of mine with a fraction problem on how to represent fractions so that they are easy to compare for children. We were thinking of a ruler and separate the ruler into ...
6
votes
1answer
1k 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} ...
4
votes
1answer
248 views

PGF loop variables in string comparisons

In a bigger context, I need to check whether an integer parameter is contained in a set of numbers. I decided to encode sets as strings; elements are separated by !. I don't know whether this is the ...
7
votes
1answer
777 views

pgf's \foreach macro

Please is there a way of turning the group scoping of \foreach off? The following problem illustrates my difficulty. Without global assignment the aim is not achieved, but global assignments might ...
4
votes
1answer
426 views

Lengths and for-loops

It seems that lengths are not updated in for loops. Compiling the code below: \documentclass{article} \usepackage{tikz} \newlength{\test} \newcommand{\inc}{\addtolength{\test}{1cm}} ...
4
votes
2answers
404 views

How to iteratively draw a path e.g. with foreach

I have a long path and want to draw parts of it in an iterative manner \documentclass{article} \usepackage{tikz} \begin{document} \usetikzlibrary{shapes,arrows} \usetikzlibrary{calc} ...
3
votes
2answers
290 views

How to cycle colors through the lines of a polygon

I want to shift the lines specification through a polygon. My current code looks like this (not DRY at all): \documentclass{article} \usepackage{tikz} \begin{document} \usetikzlibrary{shapes,arrows} ...
3
votes
1answer
144 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
510 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
244 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 ...
8
votes
1answer
529 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
535 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
605 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
573 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
652 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
977 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
331 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
623 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
565 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 ...
7
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
1k 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
733 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
273 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
456 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
496 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 ...
6
votes
1answer
870 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
312 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
514 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
279 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
299 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
395 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
529 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); ...
11
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 ...