{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

7
votes
2answers
256 views

Using foreach loop in TikZ to plot at non-uniform locations

As a Teaching Assistant for a class, I am trying to draw a seating chart for the professor. The seating chart will have a photo of each student, the student's name, and where the student is sitting in ...
6
votes
1answer
743 views

tikz label coordinates using mathematical expression from foreach variables

\documentclass{article} \usepackage{tikz} \begin{document} \begin{tikzpicture} \foreach \x [count=\xi from 1] in {-3.0,-2.8,...,1.8} \draw[thin] (0,0) ++(\x,2) rectangle ++(0.2, 0.2) ...
2
votes
2answers
163 views

What is the correct way to generate notation for a finite set using for loop in Latex?

I am trying to create a command that will take an index n as a parameter, and generate the the expanded form of this {a_1, a_2, a_3,...,a_n}. For my first attempt, I tried to use the syntax suggested ...
4
votes
2answers
261 views

Tikz: Using relative positioning and foreach variables

I want to generate a picture that replicates itself and slowy fades out. For that i plan to use a foreach statement and redraw the picture to its rigth. How can i use the foreach variable to position ...
4
votes
0answers
234 views

Drawing a gear with Tikz [duplicate]

I need some help to modify the \gear macro of this answer in order to be able to fill the drawn gear with a solid color. I've tried to move the \draw command outside the loop, but it does not work as ...
6
votes
2answers
248 views

tikz/foreach: Protect space after “iteration variable”

I try to draw some nodes in tikz, and as they are very simple I wanted to "shorten" this using a \foreach command: \foreach \xnum in {1,2,3,4,5} { \node at (X\xnum center) {$x_\xnum$}; } You ...
3
votes
1answer
166 views

Using foreach to generate computed indices in a plot

I have a table with 8 columns, and I want to generate 4 plots (1 vs 0, 3 vs 2, 5 vs 4, and 7 vs 6). I was going to use foreach to generate the plots using \begin{tikzpicture} \begin{axis} \foreach \x ...
1
vote
0answers
229 views

tikz foreach used for number in filename for addplot [closed]

I have quite a few tables generated which I want to plot in the same TikZ figure (using pgfplots). I am currently trying to use a foreach loop to plot them all, but it is only plotting the last ...
3
votes
1answer
241 views

How to make hyperlinked tabs with specific colors?

I modified this project How to print section titles like tab-list? in the following way: usletter, 8 tabs on the edge of paper, odd pages only, text within the tab is set by the user (\TabText), ...
6
votes
3answers
150 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 ...
5
votes
1answer
251 views

tikz gives null character in \document

Please why does \begin{document} \begin{tikzpicture} \foreach \x [remember=\x as \lastx (initially A)] in {B,...,H}{% $\overrightarrow{\lastx\x}$, } \end{tikzpicture} \end{document} give Missing ...
11
votes
1answer
251 views

\foreach has a problem with ‘initially’ argument in remember part

Is there an error in my code or did I find a bug? It seems like the \foreach statement doesn’t increase \lastn if there is an initally (value) but it works with a manually given initially value ...
6
votes
2answers
330 views

How to make global a color list definition with \@namedef done inside a foreach loop

Motivation As stated in the title, I'm trying to define a command able, from a list of colors as input, to create a list in which colors are numbered. This, manually, has been done in Arrows ...
7
votes
3answers
168 views

Defining commands using two loops

I would like to define a series of command using two loops. For instance I want to create a series of variables that can have YY or NN as subscripts. I would like to define commands \yy and \nn that ...
11
votes
3answers
396 views

Using \foreach loop in Cubic B-spline curve refinement

This question is only about TikZ (/PGF) implementation. This is a MWE of what I want to ask for: \documentclass{article} \usepackage{tikz} \usetikzlibrary{calc} \begin{document} ...
7
votes
3answers
374 views

How to invert list used for a tikz foreach loop

Given \def\stuff{a,b,c} I would like to be able to do \foreach \x in {c,b,a} {...} without inverting the list \stuff manually. For example is there some sort of \invert macro that would allow me ...
5
votes
2answers
180 views

Have \foreach restart or loop

I understand that trying to have a "loop function" like \foreach loop sounds like a paradox but I'll try to explain what I mean. Recently, I've asked a question about automatising the creation of ...
9
votes
3answers
792 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
2answers
194 views

Using etoolbox macros in tikz foreach loops

I am trying to wrap my head around etoolbox for use with TikZ. In this example (I know there are probably simpler ways to do this), I would like to draw a series of rectangles that are next to each ...
4
votes
1answer
737 views

How to loop through a list of tikzpicture within animateinline?

I have the following code which uses animateinline and several tikzpicture for it's animation effects: \documentclass{beamer} \usepackage[utf8]{inputenc} \usepackage{tikz} \usepackage{ifthen} ...
4
votes
2answers
764 views

TikZ: Drawing the same data with scatter plots and parallel coordinates

I am new to TikZ and trying to recreate the following image (which was created in R): (Translation: correlation = +1, correlation = -1, two clusters, circle, normal distribution) Each "column" ...
14
votes
6answers
463 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 ...
5
votes
2answers
495 views

tikz \foreach dynamic list with environment arguments

I'm making an axis environment that recieves 5 arguments (the first is optional and it's 1 by default). To make the x marks on the x axis I use \foreach \x in {#2,...,#4} {\ifthenelse{\x = 0}{}{\draw ...
11
votes
1answer
417 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 ...
2
votes
3answers
198 views

Macro to check if an element is a member of a list, and access associative lists

Inspired from Macro to access a specific member of a list. I want to have a macro implementing the lisp functions (member item list) and (assoc id alist). The first checks if an item is contained in ...
6
votes
1answer
304 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
1answer
370 views

How to assign values with foreach?

I have trouble to assign values with \foreach. Can somebody help me? The following code is wrong. The purpose is to asing the values: \lx1=1, \ly1=2, \lz1=3, for example. \foreach \x/\y in ...
12
votes
1answer
343 views

How to expand a macro used in the range of a foreach loop?

Motivating Example: Suppose that I want to draw the following tree: root / \ / \ / \ A B C D This is easy enough to do by explicitly drawing the two subtrees as children of ...
8
votes
2answers
413 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
271 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} ...
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% } ...
6
votes
3answers
1k 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
1k 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
336 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 ...
12
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 ...
9
votes
3answers
4k 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
190 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
366 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
734 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
208 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
291 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
640 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
957 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
238 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
771 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
392 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
378 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
273 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
140 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} {<...>} ...