{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

9
votes
3answers
241 views

How to tell TikZ to first compute and then display a variable?

I want TikZ to first compute and then display the value of a variable. The question is, for sake of simplicity, based on this concrete example from TeXample.net: \documentclass{article} ...
4
votes
2answers
62 views

Using pgffor's \foreach to place graphics into a tabular

I want to arrange EPS graphics (included via \includegraphics) in a tabular using \foreach. Currently this is done without any loops like this: ...
6
votes
0answers
51 views

tikz foreach loops: is there an equivalent of metapost's step?

I apologise if I'm using and searching with incorrect terminology. This seems an obvious question (to me) so I'm surprised I've not found anything (and so assume I'm missing something). In metapost, ...
32
votes
3answers
736 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 ...
2
votes
1answer
20 views

Illegal unit of measure (pt inserted) error when using \foreach with node[pos]

I am trying to draw a path and position nodes on it and fill them using this code and it works \documentclass[varwidth=20cm]{standalone} \usepackage{tikz} \begin{document} \begin{tikzpicture} \draw ...
0
votes
1answer
53 views

How can I connect centers of nodes defined using let in a foreach loop?

I want to create a star polygon using a \foreach loop to make nodes for the vertices and another \foreach loop connecting, say, every 4th node. \documentclass[12pt]{amsart} \usepackage{tikz} ...
8
votes
2answers
216 views

Dynamic upper bound for foreach loop

I want to read the upper bound for my foreach loop from a file. I've tried the following, but it gives all manner of errors. \documentclass{article} \usepackage{tikz} \begin{filecontents*}{foo} 42 ...
1
vote
1answer
54 views

\pgfdeclareshape: how to define anchors in a loop?

I think my question is quite similar to this and this, however I didn't manage to modify the provided solutions to fit my problem. In the following MWE I am trying to define 8 pin anchors in a ...
3
votes
2answers
66 views

Using \foreach to generate functions

I wish to plot a function based on collection of simple functions. For instance, the maximum of some functions: \begin{tikzpicture}[] \begin{axis} \addplot[smooth, color=black] { max( 2*x, ...
6
votes
3answers
174 views

Long List of Files Names with Paths

I need to keep a list of files (max of around 500) along with the path. Based on the fact that each file name and path could be up to 100 characters, should I just save it in a comma separated list ...
2
votes
0answers
57 views

Passing an array type argument for new command

I've looked around and can't seem to find anywhere, but I was wondering if there is somewhere you can do something like the following: \newcommand{\loopy}[1]{ \begin{tikzpicture} \foreach ...
0
votes
1answer
64 views

Generate table using the foreach command from pgffor [duplicate]

I want to generate a table from a list. However for some reason it fails to work. I searched: PGFplots foreach equivalent to TikZ's with multiple variables separated by a slash Copy table row n ...
3
votes
1answer
38 views

Using \pgfplotsinvokeforeach in foreach loop

I am trying to run a \pgfplotsinvokeforeach loop in a \foreach loop to plot the variables var1_roll1 - var1_roll10 in one plot and var2_roll1 - var2_roll10 in another. Both loops work fine ...
0
votes
0answers
16 views

How to debug the “Argument of \XC@definec@lor has an extra }” error in Foreach statement? [duplicate]

First, I draw three nodes each of which has three parts. Everything is OK. The code and the image are as follows. \documentclass{article} \usepackage[utf8]{inputenc} \usepackage[]{tikz} ...
3
votes
3answers
186 views

Summation of a list of numbers

The next code gives : 1 1.2 3 But 1 2.2 5.2 is expected. What is wrong ? \documentclass[tikz]{standalone} \begin{document} \xdef\Sum{0} \foreach \i in {1,1.2,3} { \pgfmathsetmacro\Sum{\Sum+\i} ...
4
votes
1answer
93 views

Using knitr, beamer, and /foreach to produce unique section titles in pdf from R string variable

Using R, knitr, and beamer, I have created a pdf that contains a graph for each school in our district. I read in each graph using the \includegraphics command within a /foreach loop. I want to ...
3
votes
1answer
70 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 ...
7
votes
2answers
156 views

How can I draw an arrowhead only at the end of a path drawn with \foreach?

I want to define a command for drawing some special polygonal lines with pgf/tikz. In these polygonal lines, the line styles are of alternating change. The following is my latex code: ...
5
votes
1answer
44 views

csuse with foreach

Am having difficulty determining the correct syntax for using \csuse from the etoolbox package with a \foreach. I have a list, and for each element in this list I need to create a new list. To ...
4
votes
1answer
66 views

Extract from a token containing a list with math content

I have a token which contains a list consisting of: a date in YYYY-MM-DD format, followed by a colon, and some text which may contain math. The text will consist of a sentence or two (i.e., no ...
14
votes
6answers
446 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 ...
4
votes
2answers
118 views

Piecewise linear plot with custom grid

I try to plot a piecewise linear interpolation of a function w on a grid \xn={x1,x2,...,xN} using tikz. I tried using a foreach loop and store the grid as a vector to do that, but it doesn't seem to ...
4
votes
1answer
42 views

Why does it matter where I'm trying to call my \foreach loop from? [duplicate]

I was trying to post a solution to How can I draw 25 horizontal lines instead of these 25 marked samples?, but I got stuck with a \foreach loop which wasn't behaving as I expected it to. Here's my ...
8
votes
2answers
266 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}{ , ...
3
votes
1answer
121 views

My personal TikZ template to be easily changed

Every time I need to plot a simple function (eg. a parabola), I use a self-made TikZ template that is: \documentclass{minimal} \usepackage{pgfplots} \pgfplotsset{/pgf/number format/use ...
5
votes
1answer
120 views

How can I automate the generation of this dynamic-programming graph?

I would like to draw the following dynamical programming graph. I believe it could be sped by using the foreach syntax. The code I have started to write is \documentclass[tikz, convert = ...
5
votes
1answer
159 views

No shape is known for just created foreach nodes

This can be similarly formulated to Why are just-created TikZ shapes "unknown"?; but its slightly different. Here is the MWE: \documentclass[% 12pt, journal, onecolumn, twoside, draftcls, ...
8
votes
1answer
387 views

Increments in \foreach loop with two variables, TikZ

I'm trying to draw a stack of arrows at different angles in a \foreach loop in TikZ. I don't want to have to declare every z coordinate and angle manually so I was using the {1,2,...,10} syntax but ...
4
votes
1answer
58 views

TikZ: issue with lines in foreach loop

\documentclass[tikz]{standalone} \usetikzlibrary{calc} \begin{document} \begin{tikzpicture} \coordinate (h) at (0, 3); \coordinate (O) at (0, 0); \draw (O) -- +(-3, 0); \draw[-latex] (O) -- ...
5
votes
1answer
76 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 ...
9
votes
2answers
209 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 ...
3
votes
1answer
104 views

Retain coordinate value in tikz foreach loop

I would like to retain a computed coordinate value from one step of a tikz foreach loop to the next. In the code below, I would like the increment in the y-value to depend on the previous y-value, ...
5
votes
1answer
101 views

Why does this \pgfmathparse not work outside the \foreach loop?

The LaTeX code below builds in Sublime Text 2 and gives the results: 7, -3.0, 4, -9.0, 11, -9.0, 1, two, 3.0, IV, cinq, sechs, 7.0, But it gives the following errors when I remove the % ...
3
votes
2answers
82 views

Using Counters in foreach, animate

I´m trying to create an animation of rectangles approximating the area below a curve (x^2). I´ve tried to use a counter to specify the lenght of the rectangles but Latex won`t let me use \divide or ...
6
votes
1answer
113 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] ...
8
votes
2answers
189 views

An application of \foreach command in TikZ to generate labels

I would like ask you to help me solve the following application of foreach of TikZ package. By means of the following code: \documentclass{article} \usepackage{tikz} \begin{document} ...
2
votes
1answer
24 views

\the\thislevelcount is available inside a ForEachX loop, but \thislevelitem is not (the forarray package)

I'm trying to loop though a comma delimited list and print out the elements in a table. Here is the code that works half-way for doing this: \documentclass[10pt,a4paper]{article} % ...
11
votes
3answers
272 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} ...
7
votes
1answer
66 views

How to add a \global before \pgfmathsetmacro?

It seems that one can't add \global before a \pgfmathsetmacro as that yields: You can't use a prefix with `\begingroup'. But that is the functionality I need here. I am trying to compute the ...
8
votes
5answers
413 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
110 views

\foreach from 1 to 1 [duplicate]

How to make a command (without testing) that contains a foreach loop that may count from 1 to 1 if necessary ? \documentclass{minimal} \usepackage{tikz} \newcommand{\boucle}[1]{% \foreach \x in ...
2
votes
1answer
77 views

Pointing to a node with calculated name, why do I get to the east, not to the center?

It seems like I'm doing something wrong when using foreach variables to indicate node numbers. The two foreach loops are anchoring the paths to the east of each node. Why does this happen? ...
6
votes
1answer
156 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 ...
2
votes
1answer
71 views

Loop in pgfplots doesn't work

I'm trying to do a loop in pgfplots to draw a bunch of rectangles under a curve, but it doesn't work. This is my code \begin{tikzpicture}\begin{axis}[ width=160pt,compat=1.5.1,grid ...
6
votes
3answers
140 views

How can we correctly implement a list of two (calculated) variables?

I want to use foreach to loop through two variables at the same time, in Tikz. I'd like to go through the variable \y from 0 to 7. At the same time, I'd like to go through \yy from 0 to 14, by ...
6
votes
1answer
98 views

Create filled shape with \foreach

I now did it manually, but I'm interested in how I can do this with a loop: \documentclass{standalone} \usepackage{tikz} \begin{document} \begin{tikzpicture} \tikzstyle{Punkt} = [coordinate] ...
9
votes
3answers
139 views

Why is the \endcsname not found in the following code

This is a precursor to my real question. But for whatever reason, when I tried reducing my code for something to post here, I got more errors. In particular, the following code gives me an error ...
7
votes
1answer
113 views

Cycle not working with pgfplots \foreach

I'm trying to plot some data from a text file, and I would like to be able to cycle the styles of the markers using a custom list. I was able to get the cycle list working, but when I added error ...
7
votes
4answers
356 views

How to access i'th argument of a macro?

How can I access the i'th argument of a \newcommand declaration via an iteration variable? Assume I want a command \foo like: \newcommand{\barr}[1]{argument 1 is: #1} \newcommand{\foo}[6]{ ...
2
votes
1answer
43 views

PGF’s \foreach can’t replace in [start chain=\here]?

Here in TikZ/PGF, I need two lines of dots to connect them later. – I make those dots with chains in a \foreach loop. But I can’t get the naming to work in the loop. The code below works, but ...