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.

Filter by
Sorted by
Tagged with
2 votes
1 answer
28 views

pst-electricfield: How to use multido for placing multiple charges

So I tried to place multiple charges via the pst-electricfield package. Doing so manually works fine, but I'm trying to mimic a capacitor by placing many charges along a line. Is there a way to ...
  • 405
1 vote
1 answer
31 views

pgfplots: Interaction of "fill between" with scope and foreach loop

I intend to create several plots in different places, each with fill between areas, using the foreach loop. While all other plot lines do appear as expected for every iteration of the loop, the areas ...
  • 1,599
0 votes
0 answers
21 views

How to Create an Array of Numbers Within a Given Range and Shuffle Them?

Is there a way I can create an array of integers, say between 1 and 10, and then shuffle that array? I would like it to work with tikz/pgf \foreach. I imagine it would work something like this. \...
  • 123
2 votes
2 answers
231 views

Problem with foreach- loop

I would like to draw some circles and some squares and I would like to put in into a loop. But that does not seem to work. Any help would be great. \documentclass[tikz]{standalone} \usepackage{xfp} % ...
  • 25
2 votes
1 answer
46 views

tikzkeys not working inside foreach statement

As an attempt to solve my Define a 2D canvas using preexisting points, I came across the idea of @andrew swann's answer to Pass a 3d coordinate via pgfkeys and tried to adapt it. This is how the 3d ...
  • 155
5 votes
2 answers
129 views

def in foreach Tikz

I am a beginner in foreach command ! \begin{tikzpicture} \foreach \i in {0,1}{% \def\n{23} \node at (0, -\i) {\n} ; \def\n{24} } \end{tikzpicture} Is it possible ...
  • 301
0 votes
1 answer
40 views

Strange characters added when using 'xstring' functions in 'foreach'

I'm trying to create a macro which display a list of string and if a string start with '*', it apply specific style (like bold). So I'm using a foreach and xstring macros but something goes wrong with ...
3 votes
1 answer
33 views

Using \foreach to add rows containing references to pgfplots objects in a matrix of nodes

This is a follow-up question to Using \pgfplotsinvokeforeach to create rows in a matrix of nodes. Consider the minimal working example below: \documentclass{standalone} \usepackage{pgfplots} \...
1 vote
2 answers
71 views

Is there a way to automatically generate a label and caption for a series of figures?

I have a list of figures which I insert into my document using a "foreach" loop. Is there a way to also generate labels for each of these figures from within the foreach loop? I have the ...
  • 149
1 vote
1 answer
28 views

Help with looping through a folder to insert all images within

I have a folder within overleaf of images, which I'd like to include as figures in the document. However, I'm having some trouble with calling the "for" loop. May I ask for some assistance ...
  • 149
2 votes
1 answer
30 views

Nodes anchors in foreach loop

I need to change the anchor of a node depending on the sign. For example, if the sign is "-", then the anchor is "north", otherwise "south". To do this, I implemented the ...
  • 5,194
1 vote
1 answer
31 views

Arbitrarily change axis properties in pgfplots within a foreach loop

I have a bunch of plots which I want to insert as subfigures within a larger figure. Because they share the same y-axes, I don't want to waste space repeating the same axis and tick labels for each ...
0 votes
1 answer
47 views

How to create an empty mxn table in latex using \foreach?

I want to create an mxn table using pgffor. The following code is my try and gives the following error: ! Undefined control sequence. \pgffor@endgroup ...d {\pgffor@remember@once@code ...
  • 1,884
0 votes
1 answer
35 views

Problem with foreach loop through parameters

I have a problem with foreach loop. When I pass parameters as list with '/' used as separator it works really well. However when any element in that list is defined with \def the separator seems to ...
  • 103
0 votes
1 answer
42 views

Use foreach (pgf) in an itemize environment with sagetex

I want to generate calculations randomly using sagetex, trying to have the simplest code possible. I ended up testing this, with pgf : \begin{sagesilent} from random import sample def alea(n, max): ...
  • 153
4 votes
2 answers
89 views

Expansion of foreach loop inside \draw

I have a hard time understanding the response to the GitHub issue pgf-tikz/pgf#356. The original question asker has posted the following MWE. I've wrapped it with \begin{document} for completeness. \...
2 votes
2 answers
90 views

Generate tabular data dynamically including `\hline`

My question is same as this question but with this different that I need an \hline after every row in \tabledata. How can I do it? Currently my code is as below: \documentclass{article} \usepackage{...
  • 147
1 vote
1 answer
32 views

Supplying an Undefined Command as an Argument

I am trying to make an environment where you supply a variable name via an argument: \documentclass{article} \usepackage{pgfplots} % Foreach loops inside align \pgfplotsset{compat=1.18} \...
0 votes
0 answers
49 views

Ways to create/reproduce an basic pattern/image via Latex and tools [duplicate]

what would be the (easiest) way to make such a plot/pattern in latex (lua and or xe - possible) (would be really nice to adjust the number of sticks and the degree of the lines - as parameters ? kind ...
  • 1
0 votes
2 answers
40 views

Wrapping a for loop inside an environment

I'm trying to make an environment that wraps a for loop: \documentclass{article} \usepackage[utf8]{inputenc} \title{Testing} \author{This is a Name} \date{June 2022} \usepackage{pgfplots} \...
1 vote
2 answers
49 views

How define a command with argument to specify which list to return?

I want to define a command that returns one or another list, as a list enclosed in braces, that, in my real document and not this MWE, I can use as an argument to something else (namely, the list over ...
  • 6,691
1 vote
3 answers
75 views

Input multi files at one time by \foreach

I have several files in the sub-folder ./tmp -- "a.tex" whose content is \def\aaa{aaa}, "b.tex" whose content is `\def\bbb{bbb}. Then I input them to my main doc at one time by \...
  • 2,565
6 votes
5 answers
591 views

How add 1 in math expression inside TikZ \foreach loop

How can the loop in the following be modified so that the printed subscript is not the current value of \n but rather the incremented value \n + 1 (as in integer, not floating point)? In other words, ...
  • 6,691
3 votes
4 answers
202 views

TikZ foreach and relative node positioning

I want to use a foreach loop to position tikz nodes below each other. \documentclass[tikz]{standalone} \usetikzlibrary[positioning] \begin{document} \begin{tikzpicture} \node (n1){First}; \foreach ...
  • 70.8k
0 votes
1 answer
39 views

foreach command no working

I want to replicate this figure using tikz. Right now I am stuck with the lower part of the figure, the ground. I tried using foreach \x command, but for some reason I do not understand, it is no ...
1 vote
1 answer
49 views

How to Produce a Circular Display of Images

I have an image (in this case, a colorized predefined one) that I would like to display in a circular fashion about a fixed point and specified radius. Consider the minimal (nonworking) example which ...
  • 5,625
3 votes
2 answers
72 views

Can xparse distinguish arguments inside braces and single-token arguments without braces?

The \foreach command from pgffor package has a good feature that it can distinguish arguments inside braces and single-token arguments without braces: \foreach \i in {1,2,3} {do some thing} \def\...
  • 8,933
3 votes
1 answer
87 views

Non-trivial usages of tikz foreach with a list of coordinates

I'm struggling to make the following code work: \documentclass{minimal} \usepackage{tikz} \usetikzlibrary{calc} \begin{document} \begin{tikzpicture} \foreach \point in {(0,0),(0,2),(2,0),(2,2)} { ...
  • 387
1 vote
1 answer
43 views

How do I use foreach in a tikzpicture environment with a macro input?

I am drawing a picture in my document using a foreach loop in a tikzpicture environment, like this stripped-down example: \documentclass{standalone} \usepackage{tikz} \newcommand{\drawBoxes}[1]{ \...
0 votes
1 answer
38 views

How to use math operation in an array index created by the package arrayjob

I want to put each number in the array on a grid by TikZ. \documentclass[tikz]{standalone} \usepackage{pgffor} \usepackage{multido} \usetikzlibrary{calc} \usepackage{arrayjob} \newarray\Values \...
0 votes
1 answer
58 views

Functions defines with a loop (\foreach) makes the tikz picture go far away

In the following MWE, the tikz picture is outside of the paper. When I replace 10 by a bigger number, the pictures goes even further to the right. Any idea ? \documentclass{article} \usepackage{tikz} ...
  • 165
4 votes
4 answers
361 views

Get value from \foreach

I build a macro named \fromlist to get value from a list. The value can be typeset, but can not be invoked directly, why and how to deal with it? Code: \documentclass[a4paper]{article} \usepackage{...
  • 2,565
12 votes
6 answers
1k views

How can I create a comma-separated list of characters from a text string?

How can I split the string Hello World! - stored into a macro \def\mystring{Hello Word!} - into a comma-separated list of characters (including spaces) which can be used as argument of a \foreach loop ...
2 votes
1 answer
98 views

Include \hrule within tabular rows generated using \foreach

This question extends the solution from Generate rows in table using \multido (or something similar). Based on the solution to the linked question, I understand how to use a \foreach loop to build ...
4 votes
1 answer
66 views

Loop variable in node anchor specification

Given a loop variable \x, I would like to use it as (foo.\x west), for example, where \x could be north. My attempt: \documentclass[tikz,border=1mm]{standalone} \begin{document} \begin{tikzpicture} ...
  • 595
0 votes
0 answers
31 views

Index variable appears to escape the \foreach loop and then wrecks stuff [duplicate]

I'm using pgfplotstable to create standalone graphs of test data. I've included pgffor and I'm trying to use a \foreach loop to iterate over the three output channels for a given unit under test ...
10 votes
3 answers
1k views

Labeling axis in π increments

i would like to lable my x-axis on a tikz-picture in π/4 increments, like in the wikipedia sin and cos graphic 1. This is my code (but it doesnt work): \documentclass{scrartcl} \usepackage{lmodern} % ...
  • 103
4 votes
3 answers
250 views

How to create numbered-named saveboxes in a foreach-loop?

Is there a principally way to create numbered saveboxes? For a usage like: \usebox{box-1}, \usebox{box-2}, \usebox{box-3} The follwing MWE gives no error, but no output. PS: It seems to be clear why ...
  • 6,357
1 vote
1 answer
38 views

foreach: print a polynomial using its terms

How to make the following work to have the output 5 x V^3 + 7 x V + 9 \documentclass{article} \usepackage{tikz} \begin{document} \newcommand{\mypower}{3, 2, 1, 0} \newcommand{\mycoeff}{5, 0, 7,...
  • 8,839
2 votes
1 answer
198 views

Use a variable in foreach

I am trying to use a variable in my foreach loop. It would make things a lot easier, because i only need to change one value and multiple loops are affected. Something like this: \begin{tikzpicture}[y=...
1 vote
2 answers
55 views

Use of \xappto, \noexpand and (for example) \emph

I'd like to accumulate some text from a macro and place it in a multipart TikZ node. Works as expected until there is a macro in the text being accumulated, at which point TeX fails. It would, as ...
  • 7,837
2 votes
1 answer
69 views

Create table with for loop (\Numpoint loops) and formula

Im trying to get a table added to my exam tex file that shows the grade earned based on all possible amounts of scorable points. I am running intwo two issues: The amount of \numpoints of the {exam} ...
1 vote
2 answers
100 views

How can I create a time chart with a non-uniform row height within TikZ loop?

The code below produces the given chart. (Thanks Sandy G! - see How do I create a timeline charts similar to this style? Suggestions ) I have made many modifications to this, even with additional ...
  • 161
4 votes
2 answers
362 views

Use a 'foreach' loop for drawing a staircase in TikZ

I’m trying to draw a staircase where I can set the number of stairs, i.e., \renewcommand{/stairs}{5} and it will create the stairs. I believe a for each loop can accomplish this. How can I simplify my ...
  • 195
1 vote
1 answer
206 views

How to procedually set column type in pgfplotstable for each column individually?

For a project I am working on, I would like to write a wrapper macro \fancyTable, which communicates with pgfplotstable in the background. The user should be able to specify the column alignment as a ...
  • 95
0 votes
2 answers
154 views

'Missing \endcsname inserted' error from a for loop of images

I have a for loop that goes over a list of defined names associated with images: \def \names{{3088452\_cutouts.png},{3088553\_cutouts.png},{3089892\_cutouts.png},{3090579\_cutouts.png},{3090786\...
0 votes
1 answer
61 views

Can I collect all the code in one code?

I want to write a single code for many polygons using foreach. How can I plot each one with the same code by using n variables and writing n values? I am repeating a few steps in the same way as below....
  • 33
2 votes
1 answer
99 views

Latex3 and \foreach: Do common operation over list of clist variables

I am trying to learn LaTeX3. In writing one of my projects I faced a situation in which is usefull working with short comma-separated lists, testing whether they are empty, have only one item, etc ... ...
0 votes
1 answer
45 views

How to do math with coordinate labels in tikz?

I have a set of TikZ pics defined as \documentclass{standalone} \usepackage{tikz} \usetikzlibrary{calc,positioning} \tikzset{ shape/.pic = { \node [draw] (xx) {}; ...
  • 258
0 votes
0 answers
82 views

Problem with \foreach and \input

I am having a problem using \foreach to import a variable amount of files into my latex template. This is the code I am using for this particular part: \documentclass[8pt,handout,t]{beamer} \...

1
2 3 4 5
16