{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

8
votes
2answers
338 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?
3
votes
2answers
259 views

Numbers as markers with \foreach and additional macro based on counter

First issue I would like to plot several curves using the \foreach command and have their number in the sequence as markers. Here is the code I came up with : \documentclass{article} ...
16
votes
4answers
871 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% } ...
5
votes
3answers
929 views

Use TikZ foreach variable in node

This MWE \documentclass{standalone} \usepackage{pgfplots} \begin{document} \begin{tikzpicture}[scale=0.75] \begin{axis}[ylabel=Y-Axis, xlabel=X-Axis , xmin=2, xmax=10, ymin=0, ymax=12, ...
5
votes
1answer
992 views

Package pgfkeys Error: I do not know the key '/tikz/ '

I have an error when I want to compile the LaTeX code below when I set \var to 1. Otherwise I have no problem. The error I got is: Package pgfkeys Error: I do not know the key '/tikz/ ' Could you ...
2
votes
2answers
309 views

Impossible to use the variable set by \foreach as a variable in a function

The latex code below does not compile. This is due to \foreach and the function \getcolor. Does anybody know, how I can use the variable set by \foreach in a function. Find below the code ...
9
votes
3answers
858 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 ...
8
votes
3answers
3k 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 ...
6
votes
3answers
181 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
322 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
639 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
188 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 ...
5
votes
1answer
257 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
587 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
809 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} ...
3
votes
1answer
215 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
754 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
346 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
325 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
254 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
125 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
445 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
213 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
476 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
470 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} { ...
3
votes
1answer
1k 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 ...
5
votes
2answers
470 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
479 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
561 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, ...
4
votes
1answer
832 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
297 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}{ -- ...
11
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
534 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
482 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
896 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
810 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
631 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
247 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
1k 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
995 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
404 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
451 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 ...
3
votes
1answer
645 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
896 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
295 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
454 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
2k 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. ...