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

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
673 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
242 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
776 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
412 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
396 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
285 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
502 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
240 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
517 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
528 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
584 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
562 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
649 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
963 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
329 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
612 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
553 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
999 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
722 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
272 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
450 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
495 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
846 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
309 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
508 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
274 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
296 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
393 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
522 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 ...
5
votes
3answers
363 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
5k 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 ...
21
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} ...