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.

865 questions
Filter by
Sorted by
Tagged with
1 vote
68 views

How to create a command to plot a series of square lattices on a given line?

As the figure below, for a fixed line on a square-lattice layer with the scale of 30*30, it y-coordinate is 12, then I'd like to plot squares on the x-coordinates of 1,6-13,21,28-30, 13 square dots in ...
• 548
25 views

Displaying table content within a pgfplot [duplicate]

I've got a table with a binary column named xrayOn. Instead of plotting this, I want to write each xrayOn value (i.e. 0 or 1) at the position (index, 1) in a node. However, in the code below only the ...
• 13
53 views

Fuzzy Number Line

I'm trying to create a command that generates a number line for an arithmetic progression. For example \SNL{10}{5}{9} generates a number line with the first value of 10, common difference of 5 and ...
• 593
73 views

Polynomial long division with macro command in a for loop (to get different coefficients)

I would like to create problem sheets with polynomial long division, where each student gets different polynomials. I tried to use the polynom package, but the following code does not compile (it does ...
• 984
267 views

List of numbers in an arithmetic progression

I need to generate a list of numbers in an arithmetic progression. For example 3,7,11,15,19. I was hopeful there was a way to achieve this by specifying the first term (a), the common difference (d) ...
• 593
1 vote
52 views

Infinite loop in foreach in pgfplots axis?

I'm trying to generate a staircase plot by computing the coordinates with a foreach loop, but the compilation never stops and gives nothing. A minimal example follows: \documentclass{article} \...
• 5,551
84 views

Print macro in \addplot node label

I use a foreach loop to plot multiple curves via the \addplot command. The values that are necessary to define the relevant curve in each iteration are defined through some macros with the command \...
53 views

Fill area under curve points

So, I have this code and I need to fill area under curve points. How can I do that? a) First, I need to fill area under curve points from x = 1 to x = 5 b) I need to complete fill area. Thanks. \...
1 vote
71 views

Draw arrow between two points in a list in TikZ

I am trying to connect a set of dots with arrows using tkiz. I am trying to demonstrate the motion of something as a function of time with the arrow indicating the motion. I can get the dots where I ...
34 views

Creating a yearly training log with calendar.sty and loops

I'm quite new to LaTeX and just playing around learning new things. So i want to create a PDF that i can print as a book to log my training and still be able to write some things with a pen. It should ...
65 views

pgf list with length registers

Is it possible in tikz to generate a list using length registers? for exemple: \foreach \x in {0,1,...,6} with produce the following list: "0,1,2,3,4,5,6" but now I want to produce a list ...
• 1,028
1 vote
47 views

foreach does not work together with ifthenelse and mod

In the minimal example below, the commented out line does not work. Other lines work correctly. I can't understand what the reason is. Here is a similar question, but my variable \im takes integer ...
• 616
110 views

Loop to input content into tabular

I'm trying to input files that represent rows of a table into a tabular environment using a loop, but I'm having trouble using input. Here is a MWE of my code without using loops. I have files with ...
115 views

How to pass an argument to a macro called with \csname

I have a macro that generates lists of random numbers and converts them into letters to print. I'd like to create another macro that prints those lists with intervening text. That is, if \first ...
• 23
62 views

pgfplots: find the intersection point of two curves and determine the y value of the intersection point

Thank you for reading my question! Introduction Now I want to draw a picture, in which I need to calculate the intersection point of the two curves and determine whether the y value of the ...
• 39
131 views

Repeating drawing isosceles triangle

From the answer to this question, I would like to add repeated circles, rectangles, and isosceles triangles. I applied this code to draw the following figure, but I could not use \foreach to repeat ...
• 4,927
57 views

Use foreach count index as variable in pgfplots

I have a beamer frame with an axis environment that contains a foreach loop. I would like to use the count index deriving from such loop as a variable within the loop. Specifically, I need such ...
1 vote
43 views

I was using tikz and I had a problem using \draw[] inside a \foreach loop, so I found \pgfplotsinvokeforeach and I have doubts about how it works. This is an example \documentclass{article} \...
287 views

How to use \foreach and \newtheorem together?

I want to use \foreach and newtheorem together, so I've assigned an array with the package listofitems \setsepchar{;/,}\ignoreemptyitems \readlist\baz { theorem, lemma, definition, corollary, ...
• 548
1 vote
49 views

Getting overlapping circles (or any shapes) along a closed curve in tikz

I have been trying to figure out how I can have overlapping circles along the path of a closed curve. I have seen that if I use the Bezier curve commands, then I can almost kind of get what I'm ...
• 23
55 views

Rectangle Split in tikz Node using Loop Statement

I want to dynamically loop through the Instruction Fields so as to draw the register containing them. The code I came up with is the following: \documentclass{standalone} \usepackage{amsmath} \...
1 vote
15 views

saving computed values in a file or table from a foreach loop in a regression plot

I want to save the regression slopes of those data sets for a further table. I manage to add rows in a initial table in the foreach loop but adding the plots doesn't work anymore.... Thanks in advance ...
• 41
32 views

TikZ: collecting defined coordinates in a list

I want to collect previously defined coordinates in a list and print them later in a \foreach-loop for debugging purposes. The following MWE shows the list \points which ideally should be defined and ...
1 vote
57 views

Using foreach and cs coordinates, foreach evaluate and axis

I was trying to draw dots over a pgfplots axis using axis cs coordinates. This works just fine typing the coordinates by hand, but fails as soon as I try to use the value coming from a foreach ...
• 803
1 vote
73 views

Using the result of TikZ foreach evaluate

I was trying to display the value of an evaluated variable in a TikZ foreach loop on a very basic example (tweaked from https://tikz.dev/pgffor#pgf./pgf/foreach/evaluate), but somehow it fails. See ...
• 803
75 views

Loop Through Array of Strings (Actually an Array of Array of Strings, e.g. {{black, ab}, {white, cd}})

I'm drawing stones on a Go (game) board with SGF coordinates (which I only managed to do from the the help people gave me on this question) like this \drawStoneFromSgfCoords{ab}. However, there might ...
• 438
1 vote
113 views

How to make a loop-generated table depending on the outcome of a calculation?

As a Dutch teacher my grading scale goes from 1,0 to 10,0. The calculation of the grade depends on three variables: the N-term; the score of the student; the total number of points for the exam. I ...
• 139
1 vote
43 views

Iterating over list of lists?

Following up on Customized list of elements using etoolbox in particular, second solution: Is there a convenient way to iterate over any of those lists? Example: \documentclass{article} \usepackage{...
• 415
75 views

How to do length calculations with based on a \foreach array

I'm trying to improve the readability of my \drawHoshis macro by creating variables for the x and y coordinates, besides also running a calculation with a minus inside parentheses (e.g. x * (y - 1)). ...
• 438
196 views

Change position within a tikz picture

I've been working on some code to support young children with their counting. A rectangle grid with some dots in it is called a tens frame The above image is the tens frame for 3. I am wanting ...
• 593
54 views

Define foreach step dynamically by arithmetic computations

Consider the following: \newcommand{\numA}{4} \foreach \j in {\numA,8,...,50}{...} As expected, \j will be 4,8,12,... and so on. Now, my question is: how to automatically set the second value as 2*\...
• 37.5k
40 views

Numbering nodes based on calculation in a for loop

This may be one duplicate of this QA. But I failed to use its answer. Here is my example code: \documentclass{article} \usepackage{tikz} \usepackage{fp} \newcommand*{\xMin}{0}% \newcommand*{\xMax}{4}%...
• 163
134 views

LaTeX gives error when looping in tabular environment [duplicate]

Package used: pgffor Function used from package: foreach Compiled with: latex the minimised document: \documentclass[a4paper]{article} \usepackage{pgffor} \begin{document} \def\colourNames{red, ...
• 33
194 views

tikz \foreach (miss?) behaving

In the code below, I expected the 3 cases (commented out) to be fully equivalent. Unfortunately (for me) the 3rd one produces a straight line from coordinate (X) to coordinate (Y). I always thought ...
• 156
93 views

Get the output of foreach rather than foreach itself

I want to get the output of the following macro stored in a variable: \foreach \m in {1,...,9}{\csname h\m \endcsname}; I tried to do: \def\lkj{ \foreach \m in {1,...,9}{\csname h\m \endcsname}; } ...
1 vote
42 views

Anchors not working on foreach-generated node names: "Unknown operator c' or ce' (in '0.center')."

So I try to generate a tikzpicture for the Pascal Triangle. This is my code & result so far: \begin{figure} \centering \begin{tikzpicture} \foreach \n in {0,...,7}{ \foreach \k in {0,.....
77 views

Iterate with forloop instead of foreach [closed]

I have this {C/4/a/n,D/4/b/n,E/4/c/n} This is how I iterate it with foreach: \foreach \x\y\w\z in {C/4/a/n,D/4/b/n,E/4/c/n} { \x - \y - \w - \z } But I need to do it with forloop. It is ...
251 views

Tikz and foreach

Here is a TikZ code. How improve it ? Thanks \documentclass{article} \usepackage[latin1]{inputenc} \usepackage[T1]{fontenc} \usepackage[french]{babel} \usepackage{tikz} \begin{document} \begin{...
• 407
1 vote
78 views

How to uniformly distribute pics on a curve?

I want to create a path in the form of a Bezier curve and place pic figures evenly along the path. In this example, the pics are only placed between the last two points of my curve and they are not ...
• 11
80 views

colours in loops [duplicate]

How can I get the following by a loop? \documentclass{standalone} \usepackage{tikz} \begin{document} \begin{tikzpicture} \draw[fill=green!20!black] (0,0) rectangle (1,1); \draw[fill=green!40!black] (0,...
• 321
62 views

Unknown function corner' in for-loops

I would like to draw this graph: each side of the regular pentagon is replaced by five circular dots. \documentclass[border=1mm]{standalone} \usepackage{tikz} \usetikzlibrary{shapes.geometric} \...
• 795
57 views

I am trying to pass a command array to the new command or a definition with a for loop with includesvg`

I have learned from the following questions: "For loop" in newcommand Use pgffor to define a new command? \foreach with \newcommand how to use them together? I was trying to pass an ...
• 269
362 views

How to simplify the code below by using cycles?

\documentclass{article} \newcommand[3]{\mycounter}{a=#1, b=#2, c=#3} \begin{document} \mycounter{1}{2}{3} \mycounter{4}{5}{6} Too much… and the last line is \mycounter{28}{29}{30} \end{document} I ...
• 548
1 vote
92 views

Changing order of objects in a grid

The following code draws tens frames to show adding. The input is two numbers which present in different colours. \documentclass{article} \usepackage{tikz} \newcommand{\tensframeh}[2]{\begin{...
• 593
104 views

Changing value of an array inside foreach doesn't effect the array outside of foreach

Editor: TexMaker Tex distribution: Latex I have a foreach statement, and I'm changing some of array cell's values inside of the foreach block. But it seems that the changes have not applied on the ...
• 51
1 vote
83 views

confused with tikz looping logic

In an earlier question asked here and resolved partially, an extra cycle of execution within this function happens. As a result, there are extra labels published. Any idea what loop logic needs to be ...
• 229
92 views

Conditionals in \foreach loops

I drew a ruler using the MWE below then I realized that It draws each medium (5 mm step) vertical line over the small ones (1 mm step) and each large (numbered) vertical bar over both medium and small ...
• 1,380
68 views

I cannot define tikz coordinate with foreach statement

I got a weird bug when I want to define and name a list of coordinates \tikz{ \foreach \name/\pos in {{a/(1,0)}, {b/(0,1)}} \coordinate (\name) at \pos ; } The error is {/usr/local/texlive/2023/...