Tagged Questions

{loops} is about looping constructs which allow a programmer to apply the same operation multiple times, each time with possibly different parameters.

learn more… | top users | synonyms (1)

9
votes
4answers
392 views

Simplifying Manual Recursion

The following is literally copied/pasted out of a *.tex file: \newcommand{\bA}{\mathbf{A}} \newcommand{\bB}{\mathbf{B}} \newcommand{\bC}{\mathbf{C}} \newcommand{\bD}{\mathbf{D}} ...
2
votes
1answer
137 views

dynamic for loop

I'm trying to create a dynamic for loop: \def\forinmacro{{a,b,c,d}} \iftb{SomeBool}{\def\forinmacro{{a}}} \foreach \m in \forinmacro {...} Note if I change the last line to \foreach \m in ...
4
votes
1answer
878 views

Creating variables with index (using counters)

Is there a way to add an index to a command? For example: \newcounter{count} \loop<condition> \def\var\Roman{count}{some data} \repeat The \var\Roman{count} should become the variable ...
5
votes
1answer
407 views

Is there something like \renewcounter or \providecounter?

Is there a command that creates a counter or, if it already exists, redefines it? Thing is that I'm working with nested loops. The second loop is however in a command. This means if I want to use a ...
12
votes
3answers
284 views

Why does the package babel[french] destroy \@for loops?

Why does this minimal example code: \documentclass{article} \usepackage[francais]{babel} \begin{document} \def\names{Toto,Titi,Tete,Tutu} \makeatletter \@for\next:=\names\do{\textbf{\next} } ...
5
votes
1answer
2k views

Using \end in internal vertical mode

Please how do I stop at all cost in the following? Note: \leavevmode is no rescue. \documentclass[crop]{standalone} \def\z#1#2{% \ifnum#1<\numexpr#2+1\relax \noindent\hfill#1% ...
5
votes
4answers
160 views

Is there a trick to reduce the number of counters in the following case?

I want to avoid using \tmp. Is it possible to remove it while keeping the same output? % Declare counters. \newcount\pages% \pages is given so you cannot remove it! \newcount\x \newcount\tmp % ...
11
votes
5answers
1k views

Teach me how to use \loop…\repeat constructs, etc

The following code snippet is a part of the solution given here: \def\IncludeOutput#1{ \parskip=2mm \def\examplename{#1} \pdfximage{\Directory/\examplename.pdf}% ...
2
votes
2answers
317 views

What makes a loop inside \IfFileExists fail?

The following MWE is the simplified version of my project. But it is complete enough to produce the problem. If you disable (comment) the looping parts, compilation succeed. Otherwise you will get ...
3
votes
1answer
128 views

forarray's \ForEach isn't robust

\ForEach and related commands of forarray package aren't robust. They can't deal with lists whose separators are active (e.g., active comma). Also any trailing space after the list item is retained ...
2
votes
3answers
225 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 ...
4
votes
2answers
236 views

l3prop property stores and etoolbox loops: weird results

I'm writing a class for handling multi-language jargon formulas. My idea is to use l3prop to store, for each formula, its language variations -- i.e., a sort of hash table. Building on one of the ...
5
votes
1answer
311 views

Auto generating table

In the following scheme, why do I have to hide & in a macro (\samp) before it works? \documentclass{article} \usepackage{catoptions} \makeatletter \cptnewcounts{rowcnt,colcnt,maxrow,maxcol} ...
7
votes
3answers
505 views

Nested \loop macro

Please are there 'nestable' variants of Knuth's favourite looping macro \loop without the brace-scope introduced in the answer at nested loops. I am developing a package called loops, where I am ...
3
votes
1answer
176 views

How do I add a for loop to a class file function?

I'm using this XeLaTeX class file to write my resume. One of the features is for section headings it colors the first 3 letters: \section{experience} This is hardcoded to the first 3 letters, and ...
9
votes
6answers
868 views

Defining new commands inside a for loop

I would like to define a number of related commands by using the counter in a for-loop. For example, I want to define 26 commands like this: \newcommand{\calA}{{\cal A}} \newcommand{\calB}{{\cal B}} ...
2
votes
1answer
541 views

conditional inside a pgfplotsinvokeforeach loop

I'm using \pgfplotsinvokeforeach to do mathematical computations inside the \addplot options. Now I need a conditional to decide whether to plot a line or not depending on the current index of the ...
9
votes
5answers
4k views

Inserting figures using loops

I have a set of pictures in my working directory and I want to make a PDF report using these images. I want to include all of them, but the number of the pictures changes in each case. I would like to ...
1
vote
0answers
452 views

Generate trees based on number of nodes and children allowed [closed]

I am doing an experiment, and would like to generate images of Binary Trees and Exponential Trees using LaTeX. The difference between these two structures is the number of children allowed per node ...
3
votes
2answers
240 views

\DTLdeletedb (from datatool package) has no effect within \foreach

This question led to a new feature in a package: datatool \DTLgdeletedb is available in (v2.13) and later. I am trying to use a \foreach loop to iterate over a few items. For each iteration ...
6
votes
1answer
275 views

\foreach with indexed names of curves

I am constructing several curves, which are all constructed differently, but they have path name=lambda1, ..., lambda4. Later, I intersect each of them with one path. Attached is a working minimal ...
9
votes
5answers
863 views

problem with using loop inside the tabular environment

I am having trouble with using a loop inside a tabular environment: \newcounter{it} \setcounter{it}{0} \begin{tabular}{ll} \loop\ifnum\theit<4 \addtocounter{it}{1} Q & A\\ \repeat ...
3
votes
1answer
195 views

How can I create copies of a document with a running number?

I am making a questionnaire, to be filled out with pen and paper. I need 40 copies of it, with around 13 pages each. I expect that some participants might take the questionnaire apart, so I have to be ...
3
votes
4answers
236 views

How can I write a nested loop if the inner loop doesn't always have the same number of repetitions?

I am writing a questionnaire. It lists 16 software features, and the participants should answer things like "I find this feature: Very important - Somewhat important - Not at all important" once for ...
6
votes
2answers
1k views

Less than or equal comparison within a whiledo

I am in the process of cleaning up the code for Sieve of Eratosthenes in tikz, but saw that when I initially did the code, I could not figure out how to do a <= comparison within the \whiledo, and ...
12
votes
4answers
4k views

PGF/TikZ: How to store strings in array?

I'm trying to generate a lot of name badges. I'm wondering, is it possible to store the names as strings in one array, according to the PGF manual: ...
6
votes
1answer
562 views

Problem setting a flag inside a TikZ `\foreach` loop

I'm pulling my hair out trying to understand why the following doesn't work. The intention is to calculate whether a given number #1 is prime, and to set \isprime to either 1 (true) or 0 (false). The ...
5
votes
1answer
172 views

Infinite loop on a non-macro token

The following document infinite-loops: \documentclass{article} \usepackage{breqn} \begin{document} \newcommand{\allowbreaks}[2][,]{\begingroup \catcode`#1=\active \scantokens{% \edef#1{% ...
4
votes
2answers
659 views

For loop print out array contents sequentially

I have already some experience getting \forloop to work in LaTeX but I can't figure out how to get it to print off each item sequentially in a given "array"... Like this pseudo-code would do: ...
19
votes
2answers
1k views

Repeated slides in beamer

I have about 200 JPEG files that I need to include in a PDF document, one JPEG per page (i.e. 200 pages + intro slide). The following code successfully does this with one JPEG. ...
7
votes
1answer
649 views

Using \Alph on \foreach loop argument

Here's basically what I try to do: \begin{tikzpicture} \foreach \x in {1,2,3,4,5} { \draw (\x,1) node{\Alph{\x}}; } \end{tikzpicture} However if I do that, I get ERROR: Missing number, ...
12
votes
5answers
844 views

A grid of images to fill A4 -paper for economic printing with some space for scissors

I want to print a A4 paper full of the same QR image. I want more elegant way to this answer here, yes I can get it working like that but I want to leave just enough space for scissors and no space ...
6
votes
1answer
575 views

Pgffor and the alignment character (&)

I need to construct a table that looks like: \begin{tabular}{ccc} a(1,1) & a(1,2) & a(1,3)\\ % <--- a(2,1) & a(2,2) & a(2,3)\\ % <--- I need this a(3,1) & a(3,2) & ...
9
votes
2answers
913 views

While loops in TikZ?

Is it possible to have while loops in TikZ, or some equivalent loop which only terminates after a certain condition?
5
votes
1answer
484 views

Use a loop to generate a list for another (foreach) loop

Is it possible to use a loop to generate a list for a foreach loop? More specifically I want the following: Consider the following answer to a question I asked some time ago: ...
5
votes
1answer
2k views

Including all files within a directory [duplicate]

Possible Duplicate: How to iterate through the name of files in a folder Using \input{filename}, I can input a file canned "filename.tex". I have a directory containing all of the files I ...
18
votes
1answer
2k views

Want to fill line with repeating string

Similar to the \dotfill command, but with your own text, rather than dots. If the string was: kitty! The entire line would look like the following: ...
2
votes
1answer
353 views

looping in tabular

I am curious to know why the following failed. A similar thread is on comp.text.tex unanswered. Of course, other manipulations of \whileexpr work, but why does this fail? \documentclass{article} ...
11
votes
1answer
987 views

Possible to combine “…” and “x/y” in TikZ's foreach?

I'll start with a simple, minimal non-working example: \documentclass{article} \usepackage{tikz} \begin{document} \begin{tikzpicture} \foreach \x/\y in {1/20,2/18,...,10/2} \node at (\x,0) {\y}; ...
16
votes
4answers
881 views

Understanding a \@for loop

I'm having trouble with a \@for loop. I've got a macro that takes a comma delimited list and puts each element into a row of an array. When I do this using a \@for loop, I get an extra row that I ...
7
votes
1answer
193 views

expansion of loop macro

I have a macro that contains a loop using \@for and it works until I try to write its output to a file. The following document shows the problem. \documentclass{article} \makeatletter ...
12
votes
2answers
2k views

Variables in TikZ

I use TikZ to draw graphics in LaTeX. I'd like to do arithmetic on variables. For example I haves nodes named after N-1 and N-2 and I'd like to create the N-3 and N-4 nodes as +2 from the first nodes. ...
5
votes
2answers
443 views

Special handling of first and/or last item in an etoolbox list

Is there a simple way of handling the first and/or the last item in an etoolbox-based list in a special way? Here’s a simple example: \documentclass{article} \usepackage{etoolbox} \begin{document} ...
4
votes
1answer
3k views

LaTeX algorithmic package: Undefined Control Sequence in for-Loop

I'm trying to specify an algorithm in pseudocode in a LaTeX document, but I'm having trouble with the for-Loop. My statement: \for{$bin=0 to binCount$} \item blablabla \endfor keeps on generating ...
5
votes
1answer
600 views

How to generate named nodes from the counter used in `foreach` in TikZ?

I'm trying to generate multiple nodes in TikZ using the foreach operator, but whatever I try I get compilation errors. I need all nodes to be named in order to draw edges between them. Having read ...
1
vote
1answer
202 views

Looping and linking with TikZ

How would you build a grid using a nested foreach, defining one node for each iteration? If I were coding in Python, I would write: nodes = {} for i in range(10): for j in range(5): ...
0
votes
2answers
316 views

Environment with customizable number of table columns

I want to make macros for drawing group of questions with same answers with customizable text, number of answers and design (column widths, alignment, etc.), like this: ...
3
votes
1answer
776 views

How to create lists in tikz

I would like to create lists and use them in tikz \foreach loops. Example: \newcommand{\createSquareList}[1]{ this is the part I do not know } \foreach \n in \createSquareList{5} { % do ...
2
votes
3answers
303 views

How to simulate 2 dimensional loop using 1 dimensional loop?

animate package does not allow nested loops. Only a single loop is allowed. However, we often need to simulate 2 dimensional space in a single loop. Please see the following C# code that I want to ...
2
votes
2answers
427 views

How to detect a blank line inserted by TeX when reading an external file line by line?

I want to make a macro to import all TeX input files in a directory in which the input file containing the macro exists. When reading an external file, TeX appends a blank line to the last iteration. ...