Questions tagged [loops]

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

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

In plain TeX and in LaTeX, one can implement this using counters and \ifnum checks or \loop\unless. Some LaTeX packages even provide their own looping constructs, e.g. TikZ/PGF provides \foreach, pstricks provides \psforeach, multido provides \multido, etc. If your question specifically involves or , add that tag to your question along with this one.

Frequently Asked Questions

  1. How can I iterate over a comma-separated list?
  2. How can I store strings in an array?
  3. How can I fill a line with a repeating string?
  4. How can I put a loop in a \newcommand definition?
  5. How can I insert figures in a loop?
780 questions
33
votes
1 answer

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: kitty!kitty!kitty!kitty!kitty!kitty!kitty!kitty!kitty! Is there any way to do this?
Andrey
  • 441
11
votes
3 answers

question in using \foreach to iterate over lists

I am learning \foreach to see if it will work for what I want to do. I can't figure why the following does not generate the expected output.…
Nasser
  • 20,220
10
votes
2 answers

Loop until the end of a page

I want to fill a page with a single word or phrase repeated over and over and over again. I will then print out this page. For…
user1729
  • 817
  • 7
  • 18
9
votes
4 answers

Simplifying Manual Recursion

The following is literally copied/pasted out of a *.tex…
user13546
9
votes
4 answers

Results of xfor loop failing with numerical arguments

I am trying to write a macro that selects an item from a comma separated list, \select{2}{A,B,C,D,E} results in B. I am using the xfor package to process the list, but would try etoolbox if that offered a better solution. Now for the problem: the…
user39714
  • 167
8
votes
2 answers

LaTeX Loops in \newcommand

I have used LaTeX for about a year now and every time I write a paper I try to improve on my knowledge so recently I've been creating my own commands to help save time. I am interested if it is possible to use "for-loops" in LaTeX to create…
JTZ
  • 91
7
votes
1 answer

LaTeX's \@tfor loop

Here is an interesting case. Please why is case 1 failing? \def\expandsecond#1#2{\begingroup\edef\x{\endgroup\unexpanded{#1}#2}\x} \def\maybe@ic@{% \ifdim\fontdimen\@ne\font>\z@\else \maybe@ictrue % Case 1: …
Ahmed Musa
  • 11,742
7
votes
2 answers

Can I automatically draw my cards from a list?

So, I've made a format to print cards out (Shown below). Is it possible for me to fill each card environment from some sort of list, instead of having to write out \cards{foo} for each card? I could use a script to do this, but I thought it would…
Canageek
  • 17,935
6
votes
2 answers

Calling several tex files with a loop?

I have 160 tex files named "regj.tex" for j=1,...,160. Each file contains a table. These tex files do not contain the preamble/begin{document}/end{document}. For example reg1.tex is…
user
  • 61
6
votes
4 answers

Defining a command to generate a sequence

I'm trying to define a command to make a list of subscripted \varphi characters. Let's call it \phiseq. I want to be able to use \phiseq{1,2,3} in the body of my document to expand to $\varphi_1,\varphi_2,\varphi_3$. Any ideas? I've tried…
Kellvyn
  • 135
5
votes
6 answers

Partition string in substrings of equal length and iterate through them

To avoid the XY problem, I'll describe what I am looking for (the X) and the issue that I found while attempting X (the Y). To be clear: I'm not trying to troubleshoot Y, I'd be grateful if only X is solved. What I'm looking for I want to define a…
rdrg109
  • 565
5
votes
4 answers

Loops: iterating through a space-separated list

I wanted to iterate through a space-separated list of words, so I created the following macro: % % % \withword\variable\do{run for each member}\inlist space-separated list\endlist %…
madmurphy
  • 233
5
votes
1 answer

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% \ifnum#1=#2\relax \hfill\null\endgraf \else …
Ahmed Musa
  • 11,742
5
votes
4 answers

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 % Initializing. \pages=9% Try it for an odd or even…
5
votes
2 answers

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: \foreach \n in {a_1,a_2,a_3}{a_i, } would give: a_1,…
nate
  • 835
1
2 3