{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)

34
votes
10answers
2k views

How to iterate over a comma separated list?

How can I iterate, loop, process, or otherwise do something, with each element from a comma separated list of things? This question has been asked perhaps many many times, and the are already lots of ...
15
votes
2answers
1k views

How to programmatically make tabular rows using `\whiledo` ?

The following code snippet does not work: \documentclass{minimal} \usepackage{longtable} \usepackage{ifthen} \newcounter{xyz} \setcounter{xyz}{0} \begin{document} \begin{longtable}{|*2{c|}} \hline ...
16
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: ...
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 ...
29
votes
3answers
1k views

Typesetting the entire Song That Never Ends

I've heard that LaTeX (and for that matter TeX) are Turing-complete, so it should be possible to produce an infinite loop. And indeed, a quick search reveals several questions in which users have ...
19
votes
3answers
2k views

Delete an element from a comma delimited list

LaTeX's low-level programming is poorly documented and the section on what is called control commands is even more so. LaTeX provides the \@for macro. This works by repeatedly assigning list items ...
9
votes
5answers
820 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 ...
27
votes
2answers
338 views

What's the difference between the various methods for producing for loops?

I've been wondering about the various methods available for looping over a comma separated list and the like. In particular, I was wondering about their various strengths and weaknesses. In other ...
22
votes
6answers
2k views

“For loop” in newcommand

I have this command \newcommand{\pdfappendix}[1]{ \includegraphics[scale=0.6]{pdf/#1.pdf} } It works well when I have 1 page pdf but is not including the other pages. Now I decided to change ...
5
votes
1answer
478 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: ...
7
votes
2answers
525 views

How to decrease the counter in tikz foreach loop

I have the following code: \documentclass[11pt]{article} \usepackage[a4paper]{geometry} \usepackage[utf8]{inputenc} \usepackage[english]{babel} \usepackage{tikz} \usetikzlibrary{positioning} ...
6
votes
2answers
858 views

Repeating a block of text until it fills a page

Might I please have some help with writing a loop that repeats a block of text as many times as there is remaining space for it on the page? I would like to produce an notebook in TeX which is ...
5
votes
1answer
87 views

\foreach not behaving in axis environment

Am hesitant to ask yet one more embarrassing question, but I can't seem to see the problem with the \foreach in the MWE. It should produce several horizontal lines but only produces the one with the ...
4
votes
1answer
84 views

Build tabular content via \foreach

I am trying to build up tabular data using the solution at How do I use the ampersand (&) inside a foreach or conditional (or other group/environment) when building tables?, but not having any ...
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 ...
2
votes
1answer
705 views

using foreach to draw a chain with a branch

This is an extension to How to draw the border of a node using late options I'm drawing a chain of nodes with different colors using foreach from tikz. Now I want to add a branch, also using foreach. ...
22
votes
2answers
1k views

naming paths inside a TikZ foreach loop

Is it possible to create named paths inside a foreach loop with the name depending on the counter? I want to write something like \documentclass{article} \usepackage{tikz} ...
30
votes
2answers
856 views

Color all vowels differently in a LaTeX document

Is it possible to give the letters a,e,i,o,u,y a different color in a piece of text? For instance, color all the a's red, the e's blue, the i's green, etc. EDIT: Without doing it manually of course.
12
votes
4answers
3k 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: ...
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}% ...
10
votes
2answers
1k views

How to break out of a loop

I have been trying to automate the production of stepped tables that contain conversion factors of all sorts. (See Protrusion of fractions in tabulars). As part of this I have a rather convoluted ...
9
votes
1answer
128 views

Create a document looping through images

I have about a hundred images named 001.png to 123.png. I want to create a document which includes each image in center of a page. i.e. a 123 page document with each image on a single page. I know ...
7
votes
3answers
1k views

How to get the list of predefined colors in xcolor such that the list can be used in foreach looping?

I need to get the list of predefined colors in xcolor. Hard-coding as follows is not a good coding practice. \documentclass[dvipsnames,cmyk]{minimal} \parindent=0pt \usepackage{pstricks} ...
6
votes
1answer
310 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
2answers
485 views

How to determine the run time of a loop?

pdfTeX, provides two macros that can be used to determine the time elapsed from a run start. The first one determines the elapsed time in "scaled seconds", that means seconds divided by 65536. ...
16
votes
4answers
874 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 ...
6
votes
2answers
105 views

Separators for author group (IF conditon)

I am new to LaTeX, just a beginner, basically a Perl developer. I have created a LaTeX file \begin{document} \cont{\iname{\isurname{Name1}\ifname{K. C. G.}}\iname{\isurname{Name11}\ifname{Y. ...
5
votes
3answers
642 views

Nested lists in \foreach loop in TikZ

I want to draw a bunch of nodes with different styles (a, b, ...) at different points (named 00, 01, ...). I am using the following code: \foreach \n/\lst in { a/{00, 14, 20, 34}, b/{01, 11, 23, ...
5
votes
2answers
288 views

How can I label TikZ-graphs on four vertices with a loop?

I need to draw all these simple labelled graphs on four vertices. Anyway, can I do this in a quick manner with a loop? And put them in an array? I have already drawn all unlabelled simple graphs on ...
5
votes
1answer
251 views

Package PGF Math Error: Sorry, an internal routine of the floating point unit

This type of error (! Package PGF Math Error: Sorry, an internal routine of the floating point unit got an ill-formatted floating point number ...) arises when trying to use a predefined list for the ...
4
votes
1answer
66 views

Package to run multiple iterations of a variable, with compilation?

I guess this is somewhat similar to Animations in LaTeX, except what I would want is something like this: Say you have a .tex document, which uses some macro \tester as parameter. I'd like to load a ...
2
votes
2answers
418 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. ...
18
votes
4answers
471 views

Comma delimited list - how to obtain the length?

It's simple. I have a list like {0,1,54,1,3} as input argument to a command in making. How can I obtain the length of this list? Something like \length(#1) where #1 is the list.
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. ...
10
votes
2answers
183 views

Dynamically define commands

I have some latex code that goes a bit like this: % Sp defines a generic command for an arbitrary space, % and all of the others just delegate the bulk of their % work to it so that when I decide to ...
9
votes
2answers
2k views

LaTeX for loop \@for

In user700902's answer to this thread, a for-loop with the syntax \@for is used. How does this syntax work? I tried searching about for-loops on other websites, but I could only find something like ...
7
votes
3answers
480 views

Function to define how many lines to be displayed

I have just made a command in my own document class wherein if I type: \mline it would automatically output 5 lines only. My problem would be producing lines by just defining how many lines I want to ...
10
votes
7answers
1k views

Iterating through two lists

I have two comma delimited lists: ListA(a,b,c,d,e,f,g) and ListB(name1,name2,name3,name4,name5) I would like to iterate through both lists at the same time, something like: loop readvalue ...
4
votes
1answer
298 views

Continuing a TikZ animation with \onslide after a \foreach loop

I want to produce an animation in a tikzpicture environment in Beamer. The animation consists in a series of slides produced by combining \foreach and \onslide, which works fine. I then want to ...
3
votes
1answer
350 views

Adding PDF pages with \includegraphics using a loop

I need to include PDF pages with \includegraphics. I know there is the package \usepackage{pdfpages}. So this is what I have: \begin{picture}(0,0)(75,272.5) ...
9
votes
6answers
845 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}} ...
5
votes
1answer
868 views

Loop for includegraphics

I try to make multiple slides in beamer with 3 figures on each slide and the title. I would like to loop through the list/array of figures and set the frame title. The name of the figure files and the ...
4
votes
2answers
181 views

Pascal's triangle in tikz with row headers

I am interested in creating Pascal's triangle with Tikz and LaTeX. I would like to add a column of text on the left of the triangle, with header "Row Number" and then the entries of each row. I am new ...
3
votes
1answer
763 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
2answers
327 views

TikZ: drawing an evolution of an ellipse to a hyperbola with the same focus

Is there a high speed way to draw the evolution of an ellipse, to a parabola, and then into a few hyperbolas with the same focus? From the picture, we see that we start out with three elliptical ...
2
votes
1answer
182 views

Compare two comma separated lists

Hi I would like to compare two comma separated lists. \documentclass[a4paper]{article} \usepackage{comment} \usepackage{ifthen} \usepackage{xargs} \usepackage{xkeyval} \usepackage{xfor} ...
2
votes
1answer
344 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} ...
2
votes
3answers
530 views

How to get rid of these excessive rules?

\documentclass{article}%cannot be minimal \usepackage{longtable} \usepackage{ifthen, xspace} \newcounter{xyz} \setcounter{xyz}{1} \def\and{&\xspace} \begin{document} ...
12
votes
3answers
280 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} } ...
9
votes
4answers
389 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}} ...