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

2
votes
2answers
425 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. ...
2
votes
1answer
432 views

Place symbols/nodes at intersection between an arc and some horizontal lines with \foreach in TikZ

I'd like to place an symbol I've defined at all the intersection point between an arc and some horizontal lines: I know how to use \foreach for other cases, but I'm really not sure how to setup the ...
2
votes
3answers
878 views

TeX Nested Loops With \iftoggle

This forum has helped me understand how to use nested loops in TeX, but now I am having trouble using \iftoggle (from the etoolbox package) with loops. My goal is to have a toggle that can keep track ...
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} ...
3
votes
4answers
279 views

What is the TeX equivalent for the following C# nested loop?

I need a nested loop to animate PSTricks diagrams. In C# or C or C++, I usually write as follows. int N=6; for(int x=0; x<N; x++) for(int y=x+1; y<N; y++) Console.WriteLine("({0}, ...
8
votes
2answers
272 views

Preparing macro content in a loop (calling \foreach from \edef)

I need to do something like \def\MyArray{{1, 2, 3, 4}} but with a dynamical number of elements. The following command makes the string: \newcommand{\MakeArray}[1]{\{ 1 \foreach \x in {2, ..., #1}{ , ...
3
votes
1answer
263 views

How to translate this scenario to LaTeX looping macro?

I want to slice an image into 8 parts, i.e., 2 rows by 4 columns. Each part is on a single page as follows. The gray frame is the Acrobat Reader background, so don't think it is a part of the output I ...
5
votes
3answers
903 views

Tex Nested Loops

I am trying to write a simple nested loop in LaTex, without using any special packages, using only the \loop...\if... \repeat syntax. I think I may be lacking a basic understanding of the rules for ...
15
votes
1answer
3k views

Using double for-loop in tikz

In the manual one can find the ... option to obtain a range in the for-loop. For example: \foreach \x in {0,0.1,...,6} {\x, } On the other hand, one can have a for-loop which runs over two ...
2
votes
3answers
555 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} ...
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 ...
1
vote
1answer
1k views

How do I add parfor in algorithmic environment?

I want to have a parfor loop in my algorithm listing (it should work exactly as standard for loop works). How to either change the displayed name of for loop or add my own loop which will keep the ...
2
votes
1answer
707 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. ...
10
votes
5answers
2k views

How to define a parshape command with LaTeX?

I want to typeset a book with some \parshape command, for example: \documentclass{article} \usepackage{lipsum} \begin{document} \parshape=4 0.2\textwidth 0.8\textwidth 0.2\textwidth 0.8\textwidth ...
3
votes
1answer
519 views

How to include multiple images inside a TikZ loop

I have a case, where my files are named images_001.png .... images_011.png. I tried to put them like this: \def\filelists{1/{"t0_01"},2/{"038"}, 3/{"050"},4/{"060"}} \foreach \i\element in ...
7
votes
1answer
858 views

Generating tables with \@for command

I'm making a report template that requires the list of authors to be presented in several different ways, one of which is a table that lists each author and provides a space for them to sign the ...
2
votes
2answers
815 views

pgfplots: setlength inside foreach

I have the following code, where the size of the cube marks is set to \x inside a foreach loop: \begin{tikzpicture} \begin{axis}[axis lines=none] \newlength{\mylen} \foreach \x in {1,2,...,10} ...
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 ...
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 ...
6
votes
2answers
4k views

How to use parameter numbers and \foreach

Is there a way to use the TikZ \foreach command to do operations with parameters? E.g. I would like to be able to write (just a silly example): \newcommand{\foo}[9]{ \foreach \x in{1,2,...,9}{ ...
9
votes
2answers
554 views

Why is this loop so much faster under Windows?

In a recent question, Yiannis Lazarides was asking about benchmarking loops. Strangely his results were about 800 times faster than mine. I couldn't quite accept that my computer is that outdated (as ...
6
votes
2answers
491 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. ...
4
votes
1answer
344 views

Are there list building macros in PGF/Tikz?

In Mathematica, C# linq, or other languages, we can use auxiliary functions to produce list of certain pattern. For example: In Mathematica, we can use Table[] or Range[] to produce a list 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 ...
4
votes
1answer
231 views

Why does the following code run in an infinite loop?

\def\hcAuxText{} \newread\hcInputStream \immediate\openin\hcInputStream=myfile.tmp \loop\unless\ifeof\hcInputStream \read\hcInputStream to \hcAuxLine \let\hcAuxAcumm\hcAuxText ...
6
votes
2answers
871 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 ...
3
votes
2answers
501 views

Unexpected tikz/counter behaviour

What I've been trying to do, futilely, is to use the foreach[counter=\xi] x in {1,2,3} statement in tikz/pgf with LaTeX. This fails, every time. Whatever. So I moved on to trying to use a counter, ...
4
votes
1answer
1k views

Tikz array as argument

Problem I'm drawing flow diagrams in Tikz, and they basically include a header, followed by a list of flow components set below an arrow. I've written functions to take arguments in Tikz before, but ...
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 ...
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} ...
7
votes
3answers
476 views

Strange recursive macro output

Strange recursive macro output The code that follows describes a macro that can iterate recursively (i.e, a forloop) and uses e-TeX numexpr. It is based on an example found in the e-TeX manual (page ...
5
votes
2answers
3k views

algorithm package: Comment on “for” loop does not work?

It seems that \COMMENT in the algorithm package is broken. It gives an error when used in a FOR or FORALL line, so the first comment gives a compilation error: \begin{algorithm}[ht] \caption{My ...
17
votes
7answers
2k views

What is the canonical way to write a for-loop macro?

At a basic level TeX does not provide a for-loop construct, but allows tail-end recursion, however Knuth defined \loop in the plain macros. Coders have been coming with variations of their own ever ...