Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [foreach]

{foreach} is for questions regarding the TikZ/PGF module of the same name which can also be used as a standalone package pgffor.

0
votes
0answers
19 views

If condition inside a foreach loop with TikzPicture [duplicate]

I'm trying to show only the indices I set in an argument. Like this: \usepackage{ifthen} \newcommand{\myfunction}[2] { \foreach \x in {0, ..., #1} { \ifthenelse{\isin{\x}{#2}}{ ...
1
vote
2answers
45 views

TikZ Loop through array of arrays

I would like to loop over an array and access each x- and y-coordinate separately, because I want to modify the coordinates before using them on a node. Currently it is only working when inserting ...
5
votes
3answers
362 views

Wrong calculation in Tikz foreach

I've a misunderstanding about the method Tikz compute some numbers. I draw a graph with some scale. Here is a MWE : \documentclass{standalone} \usepackage[french]{babel} \usepackage{tikz} \begin{...
2
votes
2answers
39 views

Latex macro lists in \foreach

I have the following code, where I define two separate lists. I want "foreach" to pick items of these two separate lists. This question is updated to make it clear that "AA" and "BB" are also needed ...
2
votes
1answer
23 views

Looping over existing colours in addplot in pgfplots

In Change color in addplot in for loop of pgfplots It was discussed how to loop over nonexistent colours in \addplot and axis I would however like to loop over existing colours, but: \documentclass[...
1
vote
1answer
27 views

TikZ-matrix: Generating a table from two nested foreach loops

For the following MWE, I would like to know how I can reduce the code by having two nested for loops. \documentclass[border=10mm]{standalone} \usepackage{tikz} \usetikzlibrary{matrix} \begin{...
5
votes
2answers
382 views

Simplifying \foreach to repeat a drawing

I use the following code to repeat a drawing. Can it be simplified? \documentclass{beamer} \setbeamertemplate{navigation symbols}{} \usepackage{tikz} \begin{document} \begin{frame}[fragile,t] \...
0
votes
1answer
33 views

LaTeX error: illegal parameter number in definition of \pgffor@b

I am trying to use a loop. The following does not work: \foreach \a in {no,has}{ \begin{table}[H] \footnotesize \captionsetup{width=.9\linewidth} \caption{text} \vspace{-22pt} ...
3
votes
1answer
35 views

The node positions in chain changed unexpectedly

As shown below, the node positions have been changed when using chain. How to fix it? \documentclass[tikz]{standalone} \usetikzlibrary{positioning, chains} \begin{document} \begin{tikzpicture}[...
8
votes
3answers
101 views

foreach path operation - confused about syntax

MWE: \documentclass[tikz]{standalone} \begin{document} \begin{tikzpicture} \draw (0:2) foreach \a in {30,60,...,330} { -- (\a:2) } -- cycle; %\draw foreach \a in {0,30,...,330} { (\a:2) -- } ...
2
votes
1answer
31 views

Loop newcommand with variable arguments

Following my previous question, I would like to know how to have variable inputs for a newcommand in order to do the following \documentclass{article} \newcommand*{\repsum}[VARIABLE NUMBER]{ \...
9
votes
4answers
490 views

Loop code for repeated sums

I tried to read other topics regarding \newcommand* to create a macros in order to have repeated summations with a single command but I was not able to understand the logic behind it. Let me explain ...
2
votes
0answers
35 views

\savebox doesn't work inside tikz foreach loop [duplicate]

I need to process listed content passed to a \foreach loop inside a tikz environment. But \savebox seems not to behave the same as outside tikz. In particular, the following code: \documentclass{...
11
votes
1answer
112 views

Smarter way for a drawing

Consider the following MWE: \documentclass[border=5pt]{article} \usepackage{tikz,anysize} \marginsize{0cm}{0cm}{0cm}{0cm} \definecolor{forest}{RGB}{34,139,34} \definecolor{rot}{RGB}{220,20,60} \...
6
votes
1answer
69 views

how to draw a gray coded disk

I can draw a coded reglet with the binary code, the Gray code or any other code from the code table. \documentclass[border=7pt,10pt]{standalone} \usepackage{tikz,ifthen} \usetikzlibrary{ ...
3
votes
1answer
30 views

Why the last element of the foreach loop is not displayed correctly

in the double foreach loop below, the last element is not displayed vertically. I do not see why? \documentclass[border=7pt,10pt]{standalone} \usepackage{tikz} \usetikzlibrary{ positioning } \begin{...
5
votes
3answers
88 views

How to compute the *key* of a pgfkeyssetvalue command?

I have a list of (angles/value) pairs. I'd like to use TikZ to draw nodes with those values at these angles (easy), then draw an edge from node i to i+1, with the value of node i as a label (not ...
5
votes
1answer
71 views

Access node anchors in tikz foreach

I want to plot some dynamical systems with tikz but when I need to iterate on the node names, there is an error because the node name is broken when I need to work on the anchors. I found multiple ...
5
votes
0answers
58 views

Visibility of path names declared in \foreach loops

Buildup Basic setting Using the intersection library, I encountered some behaviour of path names that I do not understand. The following works in the intended way: \documentclass{scrartcl} \...
1
vote
2answers
60 views

Fractions and counters in a \foreach list of pairs

I'm trying to create a macro that will take a list of pairs as an argument in order to draw a pie chart. I'm trying to make it so that portions of the pie are specified as a fraction less than 1. Here'...
5
votes
1answer
82 views

Tikz, graphdrawing: How to use \foreach variables to set edge node labels?

I wanna create and label edges using a \foreach loop to avoid having to write every single node/edge when creating large graphs. It works fine when I use it to create nodes, but when I try to label ...
7
votes
2answers
144 views

Simplify things in TikZ

How can I simplify some positioning with some foreach loops or some own styles? Here is the original picture: And here is my MWE: \documentclass[border=5pt,tikz]{standalone} \usetikzlibrary{calc} \...
6
votes
2answers
63 views

foreach-loop in align

I want to write a macro for optimisation problems and my current approach is to write a macro, that takes the target function and a list of constraints. The entries are separated by comma, and each ...
3
votes
3answers
81 views

How to store an array of values?

I need to calculate the mean of a bunch of values generated by a function inside a loop. Is it possible? \documentclass{article} \usepackage{tikz} \usepackage{pgfplots} \begin{document} \begin{...
1
vote
1answer
32 views

How to do arithmetic with variables used in for loop?

How can I use arithmetic inside a node's curly brackets? I have this, and it prints for example "0+1" in a circle. I would like to have circles that show the sum of their coordinates. \begin{...
1
vote
1answer
19 views

How to suppress a join edge in chains when using foreach?

How to remove/suppress the edge from node 3 to node 4 in the following code using chains and join? Do I have to split the foreach into two smaller ones? \documentclass[tikz]{standalone} \...
4
votes
3answers
313 views

Why does “\x-1” not work in node name?

How to fix the error in referring to a node using \x-1? It works in [below left = \x-1] but does not work in \draw[->] (\x-1) to (\x). \documentclass[tikz]{standalone} \usepackage{pgffor,pgfmath}...
3
votes
2answers
84 views

Update pgfmath macro in foreach loop

I need to update a variable defined in a pgfmathsetmacro command inside a foreach loop. Apparently, the var is only updated when it is used after the update command. Here is a MWE: \begin{tikzpicture}...
4
votes
3answers
105 views

Shading different fractions of the same equilateral triangle using `\foreach`

In the following diagram, I have three views of an equilateral triangle partitioned by smaller congruent equilateral triangles. May I have a suggestion for using \foreach to reduce the repetitive ...
13
votes
8answers
633 views

Shading 25 squares within a square using `\foreach`

In a square of edge length 5*sqrt(2), I have 45 congruent squares of edge length sqrt(2) drawn. I would like to shade 25 of them - 5 of them in each row. I shaded the 5 squares in the bottom row and ...
0
votes
0answers
26 views

Accessing list of key-value lists as macro argument

I'm looking to create a macro that takes a list of key-value lists as an argument. I don't know the right data structure that can be used for this kind of situation in LaTeX and also didn't find it ...
5
votes
2answers
406 views

Color names in foreach loop

Edit: I can't delete this question despite it being based on a silly mistake. Please don't waste time reading/answering. I'm trying to draw a row of boxes which alternate colours, and I'm using a \...
0
votes
0answers
35 views

pgfplotsforeachungrouped with >2 entries

I'm having trouble understanding why \pgfplotsforeachungrouped is breaking in certain spots. Why does this work: \documentclass{article} \usepackage{pgfplotstable} \def\macroOne#1{ #1 } \...
1
vote
0answers
58 views

Drawing a (4x4)-grid in TikZ using `\foreach` - riding the wave of `\foreach` fun [closed]

How do I draw a 4x4 grid over the following TikZ diagram using \foreach? Is it preferable to do so with one \foreach command? \documentclass{amsart} \usepackage{mathtools} \usepackage{tikz} \...
7
votes
2answers
206 views

Macro not working in foreach loop

I'm trying to color the cells in a few columns with different colors, depending on bounds passed as an argument. I would like to have one macro that takes two arguments: a .csv file and a comma ...
7
votes
4answers
605 views

How to draw a curve from a previously calculated list of points

I need to draw a curve from a list of points whose coordinates have been calculated beforehand in a foreach loop I manage to generate my points and to use them independently, I think to manage to ...
4
votes
1answer
50 views

Fail to get flash effect by given times when using animate package

Text flash effect is the purpose of the following simple code. Some text should flash for a given times(here is nine times) by a \foreach loop command. However, it can not pass compile. This error ...
8
votes
2answers
290 views

How to iteratively define coordinates in TikZ?

I'm attempting to iteratively define a coordinate system in TikZ. Essentially, I'd want to replicate the following using a \foreach command: \begin{tikzpicture} \coordinate (A1) at (0,0); \...
1
vote
1answer
52 views

Passing List in Pgfplotstableset [duplicate]

I'm pretty new to LaTex, so please forgive my sloppy code. I'm trying to create a table from a csv file, and pass a list of colors in so that LaTex will color each cell of the a column of the table ...
0
votes
0answers
32 views

How to have mix of numbers and letters in \foreach

I want to run this command \foreach \x [count = \xi] in {L,1} \matrix [matrix of math nodes,left delimiter={[},right delimiter={]},fill=white,ampersand replacement=\&] (mm\x)%give the ...
1
vote
0answers
18 views

Tikz: foreach loop and hard-coded version producing different output [duplicate]

It seems to me that the following two pieces of tikz code should be equivalent; why do they produce different output? Is there a way I can get the output of the hard-coded variant using a loop? \...
2
votes
2answers
43 views

Setting line start position does not work when using double foreach

I am drawing a nice diagram with Tikz. The following works perfectly: \foreach \m [ evaluate=\m as \nstart using ((\m - 1) * 4) + 1, evaluate=\m as \nstep using ((\m - 1) * 4) + 2, evaluate=\m as \...
0
votes
1answer
90 views

Insert multiple pictures

I would like to add all my certificates in my LaTeX document as pictures. Is there any way that lets me insert multiple images without captions as follows: Each image fits on one page for example ...
4
votes
2answers
82 views

Defining a sequence for repeated use in foreach loops

I have a macro that does exactly what I want when I used it with a literal sequence, but not with a sequence that has been defined for reuse: \documentclass[tikz,14pt,border=10pt]{standalone} %%%< ...
5
votes
2answers
393 views

\foreach with two or more variables

I often find myself wanting to use some feature that allows me to loop through several sets where the nth item in each list is paired together with the nth item in the other lists. \begin{tikzpicture}...
3
votes
1answer
78 views

Do not evaluate variable in foreach

I have a bunch of variables defined with similar names, e.g.: \def\wstepsA{1/2/0,2/3.5/1}; All variables have a name wsteps + a letter, and I want to iterate over them using tikz's foreach: \...
3
votes
2answers
61 views

Using a string as a coordinate fails

When I use \pgfresult to get a node label in a \foreach, I have no problem. However, I can't seem to get the syntax right to create a coordinate with the same method. \documentclass{article} \...
1
vote
1answer
35 views

Defining lists for multiple indices in pgfplots

I am trying to plot several column pairs using the \foreach loop command with pgfplotstable in the following way. \documentclass[tikz]{standalone} \usepackage{pgfplots,pgfplotstable} \begin{document} ...
2
votes
2answers
49 views

positioning label node depending on slope of a edge

I am creating a set of nodes with a foreach. After that i am drawing a edge between them that should show a value in the middle above the line. Unfortunately there are some situations where the line ...
2
votes
1answer
39 views

Increase all x-coordinates with help of a foreach loop

Given a figure of two lines with x and y coordinates, I want to duplicate the figure and increase all x coordinates by the same integer (in this example by 3). I was trying to use a foreach loop, but ...