{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
4answers
98 views

Incomprehensible behaviour of calc library with a “foreach variable”

If I compile the following code : \documentclass[12pt]{article} \usepackage[french]{babel} \usepackage{tikz} \usetikzlibrary{calc} \begin{document} \begin{tikzpicture} \foreach \i in ...
3
votes
2answers
179 views

How to cut the upper part of the plot?

\begin{tikzpicture} \draw[->] (-1,0) -- (5,0) node[right] {$x$}; \draw[->] (0,-1) -- (0,8) node[left] {$y$}; \foreach \a in {0,...,5} \draw[scale=1,domain=1:3,smooth,variable=\x,purple] ...
4
votes
1answer
62 views

TikZ: How can I use \foreach variable to reference intersections?

Using the \foreach statement, I generate a set of lines that form a grid. I want to use the loop variables to name the individual paths to later draw something at their intersections. ...
3
votes
1answer
83 views

Making a random number

I have a problem. I have a graph, and I must paste a random number in every vertex of this graph. Vertices have coordinates. A part of code : \foreach \pos/\name in {{(0,0)/a}, {(1,-1)/b}, ...
7
votes
3answers
216 views

cannot combine \foreach and \draw

I just cannot debug this code. After typesetting it keeps on giving the error of: "Undefined Control Sequence" When individually coding rectangles for each set of coordinates the code works though, ...
0
votes
0answers
22 views

escape special characters in `\foreach` loop

Is there a way to escape underscores (or other special characters) in variable names in a \foreach loop when calling file names? E.g., \foreach \x in {file\_1, name\_2}{Here is \x.} gives correct ...
3
votes
1answer
71 views

How to append colored text to a macro in a loop?

I'm trying to append a couple of colored numbers to a macro in a loop. That is, I'm hoping to achieve the following but with a loop: \documentclass{article} \usepackage{tikz} \colorlet{color1}{red} ...
4
votes
1answer
76 views

Setting \tikzstyle inside \foreach

I am building a tree structure in tikz and am following this texample, which suggests adding styles for the children according to their depth in the tree (first, second and so on). The purpose is that ...
4
votes
1answer
46 views

Name pgfplotstable Column within foreach loop

I recently stumbled over pgfplotstable and its ability to process table information and create new columns. To hone my skills, I tried to create a script that would calculate an exponential smoothing ...
4
votes
2answers
210 views

TikZ: incrementing a foreach loop

I am working on plotting a random vector (X, Y) in the plane where X and Y are defined as a roll of the die and X is die1 plus die2 and Y is die1 minus die2. I am having trouble setting up a foreach ...
6
votes
1answer
367 views

Feature (bug?) of dots notation in foreach-statement

I want to draw something in tikz where the range of an inner loop depends on an outer loop (by construction of the math involved). I need to differentiate between even and odd number in the inner ...
2
votes
0answers
66 views

“indirect” variables in foreach

I'm trying to draw three "graphs" which are rectangles situated side-by-side. Each has four points to be placed on them. These four points are the "scores". Using a foreach loop is a natural for the ...
3
votes
1answer
55 views

Make catchfile work with pgffor's foreach

Consider the following MWE, extracted from my solution to this question: \begin{filecontents*}{demolist1.dat} 1,2,3 \end{filecontents*} \begin{filecontents*}{demolist2.dat} {1,0,2},{0,3},{1,1} ...
4
votes
3answers
157 views

TikZ picture error: No node named n1 is known, inside a \foreach loop

I get the following error: ! Package pgf Error: No shape named n1 is known. when I try to compile this code. \documentclass[border=1mm]{standalone} \usepackage{tikz,pgfplots} ...
5
votes
3answers
65 views

How can I get pgf's foreach to iterate over the contents of an input file

I would like to write something like: \foreach \myline in {\input{filehandle}} {\dosomethingwith\myline} If I have an outside_file.tex: this, is, the content, of, a file and then try to pass it ...
0
votes
0answers
22 views

Insert all the files in a directory [duplicate]

I try to import automatically in my tex file all tex files contained in a directory. The file name is arbitrary (no number) for now, I get a list of files that I put in a foreach loop, as shown in ...
7
votes
1answer
331 views

Tikz binomial distribution

I'm trying to plot the pmf of the binomial distribution for particular values of N and p. For example, when N=10 and p=0.5: \documentclass{article} \usepackage{amsmath} \usepackage{pgfplots} ...
3
votes
1answer
115 views

two variables foreach loop problem

I want to draw a pulse train on which I write some information like the corresponding angle, etc... But I think I've got a problem with the two variables foreach loop because I can't even compile ...
2
votes
0answers
76 views

Variable in \foreach,

I would like to show you my problem. Here you have the script to generate a picture.Now I need to generate a second picture in which the arrows are not equally spaced any more. ...
8
votes
1answer
110 views

drawing path in foreach loop with pgfsetmacro

I have N points on a circle, I want to draw the intersection of the convex hulls of all sets of N-k consecutive points. The points are not evenly spaced. The values of N and k (and the actual angles ...
5
votes
2answers
151 views

How to use \x from \foreach loop in the parameters of \draw in tikz

In order to make it simple, I want to draw concentric circles fading out with something like \foreach \x in {1,1.5,...,4} \draw[color=black!{100-10*\x}!white] (0,0) circle (\x); The problem is I ...
2
votes
1answer
98 views

Calculate label angle from a value list

As a beginner of tex and tikz i stuck on array handling together with foreach. I would like to draw two circle with text along its circumference. \documentclass[tikz]{standalone} ...
4
votes
2answers
122 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: ...
9
votes
3answers
329 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} ...
6
votes
0answers
86 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, ...
2
votes
1answer
539 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 ...
8
votes
2answers
263 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
83 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 ...
0
votes
1answer
71 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} ...
3
votes
2answers
74 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, ...
2
votes
0answers
114 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 ...
3
votes
1answer
92 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
19 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} ...
0
votes
1answer
248 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
3answers
208 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
248 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 ...
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 ...
5
votes
1answer
80 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 ...
7
votes
2answers
250 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: ...
6
votes
3answers
218 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 ...
4
votes
1answer
93 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 ...
4
votes
2answers
190 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
46 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 ...
3
votes
1answer
171 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
145 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 = ...
6
votes
1answer
238 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, ...
5
votes
1answer
83 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) -- ...
11
votes
1answer
1k 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 ...
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 ...
9
votes
2answers
262 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 ...