{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

15
votes
3answers
381 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 ...
19
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} ...
21
votes
2answers
3k 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 ...
10
votes
3answers
540 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
3answers
1k 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 ...
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} ...
12
votes
1answer
462 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 ...
11
votes
3answers
387 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} ...
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} ...
14
votes
6answers
508 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 ...
17
votes
4answers
1k 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% } ...
10
votes
3answers
5k 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 ...
14
votes
1answer
324 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}{% ...
9
votes
3answers
962 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 ...
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: ...
5
votes
1answer
125 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 ...
4
votes
1answer
108 views

Build tabular content via \foreach

I am trying to build up tabular data using the solution at How do I use the ampersand (&) inside a foreach or conditional (or other group/environment) when building tables?, but not having any ...
33
votes
3answers
859 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 ...
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 ...
14
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 ...
8
votes
5answers
444 views

Special case for last element with \foreach

I want to create a function that generates a list of equation references from a comma delimited input list. e.g. \erefs{eq:first, eq:second, eq:third} \erefs{eq:first} yields, the following: ...
7
votes
2answers
362 views

TikZ: \foreach in macro with argument as list

The code below generates: What I expect is: Why does the \foreach seem to treat the entire list as one value? How do I fix it? \documentclass{article} \usepackage{xparse} \usepackage{xstring} ...
5
votes
1answer
470 views

Syntax for smoothing a coordinate plot with the foreach command

Currently implementing the solution of Curve synthesis - Adding two curves to get another one. I would like to smooth out the resulting curve. The present code: % red line \path[red,name path=one] ...
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} ...
6
votes
1answer
341 views

How to define macros in a foreach loop with effects between iterations and after the loop without using global?

Without using \global, the following code: \documentclass{minimal} \usepackage[T1]{fontenc} \usepackage{tikz} \usepackage{etextools} \newcommand\appendbyforeach[2]{% ...
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 ...
9
votes
2answers
833 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: ...
6
votes
3answers
1k views

Nested lists in \foreach loop in TikZ

I want to draw a bunch of nodes with different styles (a, b, ...) at different points (named 00, 01, ...). I am using the following code: \foreach \n/\lst in { a/{00, 14, 20, 34}, b/{01, 11, 23, ...
6
votes
3answers
160 views

Expansion issue when adding to CSV list from within a \foreach

Another expansion issue that I am unable to guess at the correct combination of \edef/\expandafters to get to work. Background: Below I have defined \AddToCommaSeparateList which uses \g@addto@macro ...
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 ...
8
votes
1answer
539 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 ...
12
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 ...
9
votes
2answers
264 views

Plotting Arrows Programmatically in Pgfplots

I'm having an issue where I would like to plot a series of evenly spaced arrows where the length is dependent on the values from a data file and I'm not particularly sure how to go about it. I would ...
10
votes
1answer
3k 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} ...
8
votes
1answer
711 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 ...
7
votes
2answers
258 views

Creating anchors in a shape with a \foreach loop

I was playing around with this question, and in particular tried to modify and expand the linked example. It works fine, but there is an error when I try to use a \foreach loop to define the different ...
7
votes
1answer
290 views

using computations with \foreach in tikz

I am trying to learn \foreach in TikZ and I am confused because of all the errors I get. Here is a code sample I tried that gave me: ForeachEvaluateTest.tex:0:File ended while scanning use of ...
7
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} ...
7
votes
4answers
520 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.
6
votes
1answer
152 views

Why doesn't a \foreach variable work in \draw in an \axis?

I'm finding some weird behavior with TikZ's \foreach within an \begin{axis} environment. This works: \foreach \x in {-2,-1,...,2} { \addplot[thick, domain=0.05+(\x*2-1)*pi/2:+(\x*2+1)*pi/2-0.05] ...
6
votes
1answer
234 views

\foreach problem

I have the following problem I want to avoid a \foreach loop in a macro if not necessary, but I don't know how to do a test in a tikz picture, I undersand why \ifthenelse can not work. In my example ...
6
votes
1answer
303 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 ...
5
votes
2answers
448 views

(x)ifthen in TikZ

I am searching for a way to conditionally handle some items in a TikZ \foreach specially using an \ifthenelse condition. However, the compiler responds ! Undefined control sequence. <argument> ...
5
votes
1answer
104 views

TikZ: Can a \foreach command run inside decoration = {markings,?

Can I use \foreach inside of decoration = {markings, ...? \documentclass[convert = false, tikz]{standalone} \usetikzlibrary{decorations.markings} \usetikzlibrary{arrows} \begin{document} ...
5
votes
2answers
163 views

Color Management with a Family of Curves

I am struggling with following fairly simple looking problem of plotting a family of curves. \documentclass[border=1mm]{standalone} \usepackage{pgfplotstable} \pgfplotsset{compat=1.8} ...
5
votes
2answers
337 views

How to use declared TikZ functions in \foreach condition?

How is it possible to use a declare function in the condition of \foreach? Here's my MWE: \documentclass{article} \usepackage{tikz} \begin{document} \begin{tikzpicture}[declare function={a = 0; b = ...
5
votes
1answer
632 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 ...
4
votes
1answer
202 views

A list of pairs in pgfkeys

Having learnt a lot about the pgfkeys package as a result of my last question, I'm now trying to do some more adventurous things. Let's say I have a command called \blob, whose value is the ...
3
votes
2answers
403 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 ...
2
votes
2answers
357 views

TikZ: drawing an evolution of an ellipse to a hyperbola with the same focus

Is there a high speed way to draw the evolution of an ellipse, to a parabola, and then into a few hyperbolas with the same focus? From the picture, we see that we start out with three elliptical ...