LaTeX Loop Statement: \foreach, \forloop, and \multido

Aug. 11, 2024

\foreach command

Example 1

Code source1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
\documentclass{standalone}
\usepackage{tikz,siunitx}

\begin{document}

\begin{tikzpicture}
	\draw[->] (0,0) -- ({rad(210)}, 0);
	\draw[->] (0,-1.2) -- (0,1.2);
	\foreach \t in {0, 90, 180} { 
	\draw ({rad(\t)}, -0.05) -- ({rad(\t)}, 0.05); 
	\node[below, outer sep=2pt, fill=white, font=\small] at ({rad(\t)}, 0) {\ang{\t}};
	}
	\foreach \y in {-1,1} {\draw (-0.05,\y) -- (0.05,\y);}
\end{tikzpicture}

\end{document}

png-1

And a more complete version1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
\documentclass{standalone}
\usepackage{tikz,siunitx}

\begin{document}

\begin{tikzpicture}[scale=1.5]
	\newcommand\iangle{120}
	\begin{scope}[xshift=-2cm]
		\draw[->] (-1.2,0) -- (1.2,0);
		\draw[->] (0,-1.2) -- (0,1.2);
		\draw[thick] (0,0) circle (1);
		\coordinate[label=\iangle:$P$] (P) at (\iangle:1);
		\coordinate[label=below:$P_0$] (P0) at (P |- 0,0);
		\draw (0,0) -- (P);
		\draw (P) -- (P0);
		\fill[fill=gray,fill opacity=0.2]
		(0,0) -- (0:1) arc (0:\iangle:1) -- cycle;
		\filldraw[fill=gray,fill opacity=0.5]
		(0,0) -- (0:0.3) arc (0:\iangle:0.3) -- cycle;
		\node[right] at (\iangle/2:0.3) {\ang{\iangle}};
	\end{scope}
	
	\draw[->] (0,0) -- ({rad(210)}, 0);
	\draw[->] (0,-1.2) -- (0,1.2);
	\draw[thick, domain=0:rad(210)] plot (\x, {sin(\x r)})
	node[right] {$\sin x$};
	\foreach \t in {0, 90, 180} {
	\draw ({rad(\t)}, -0.05) -- ({rad(\t)}, 0.05);
	\node[below, outer sep=2pt, fill=white, font=\small] at ({rad(\t)}, 0) {\ang{\t}};
	}
	
	\foreach \y in {-1,1} {\draw (-0.05,\y) -- (0.05,\y);}
	
	\coordinate[label=above:$Q$] (Q) at ({rad(\iangle)}, {sin(\iangle)});
	\coordinate[label=below:$Q_0$] (Q0) at (Q |- 0,0);
	\draw (Q) -- (Q0);
	
	\draw[dashed] (P) -- (Q);
\end{tikzpicture}
\end{document}

png-1

siunitx package is “A comprehensive (SI) units package.”2

Example 2

Actually, \foreach command is provided by pgffor package, hence can be independent independent of tikz package1.

1
2
3
4
5
6
7
8
\documentclass{standalone}
\usepackage{pgffor} % \usepackage{tikz}

\begin{document}
\foreach \i in {0,...,7}{
    \i  
}
\end{document}

png-1

Example 3

1
2
3
4
5
6
7
8
9
10
\documentclass{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
    \foreach \i\j in {1/A,2/B,3/C,4/D,5/E,6/F}{
        \node[draw,circle] at (\i,0){\Large\j};
    }
\end{tikzpicture}
\end{document}

png-1

Example 4

Code source3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
\documentclass{standalone}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[myNode/.style={circle,draw,fill=teal,text=white}]
  \foreach \x in {0,...,4}
    \foreach \y in {0,...,4}
       \node [myNode]  (\x\y) at (1.5*\x,1.5*\y) {\x,\y};

  \foreach \x in {0,...,4}
    \foreach \y [count=\yi] in {0,...,3}{
      \draw[-latex] (\x\y)--(\x\yi);
      \draw[-latex] (\y\x)--(\yi\x);
    }   

  \foreach \x [count=\xi] in {0,...,3}
    \foreach \y [count=\yi] in {0,...,3}
      \draw[-latex] (\x\y)--(\xi\yi);
\end{tikzpicture}
\end{document}

png-1

Other examples

More exquisite TikZ design with \foreach can be found at TikZ examples feature: Foreach.


\forloop command

Example 1

Code source34

1
2
3
4
5
6
7
8
9
\documentclass{standalone}
\usepackage{forloop}

\begin{document}
    \newcounter{x}
    \forloop{x}{1}{\value{x} < 10}{
        \arabic{x}
    }
\end{document}

png-1

Example 2

Code source3

1
2
3
4
5
6
7
8
9
10
11
12
13
\documentclass[varwidth]{standalone}
\usepackage{forloop}

\begin{document}
\newcounter{x}                     
\newcounter{y}
\forloop{y}{0}{\value{y} < 5}{     
   \forloop{x}{0}{\value{x} < 10}{ 
       (\arabic{x},\arabic{y})     
   }
   \\
}
\end{document}

png-1

Example 3

Code source3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
\documentclass{standalone}
\usepackage{forloop}
\usepackage{calc}

\begin{document}
\newcounter{x}
\newcounter{y}
\newcounter{z}
\begin{tabular}{||r||r|r|r|r|r|r|r|r|r|r||}
	\hline\hline
	\ & %skip the top left cell
	\forloop{x}{0}{\value{x} < 10}{ % generates the header row of the table
	   \arabic{x} \ifthenelse{\value{x}<9}{&}{}
	              % one less '&' than the number of columns
	}
	\\\hline\hline
	
	\forloop{y}{0}{\value{y} < 10}{
	   \arabic{y} \ifthenelse{\value{y}<10}{&}{}
	   \forloop{x}{0}{\value{x} < 10}{
	       \setcounter{z}{\value{x}*\value{y}}  % '*' needs the calc package
	       \arabic{z} \ifthenelse{\value{x}<9}{&}{}
	   }   
	   \\\hline
	}
	 &\multicolumn{10}{|c||}{Multiplication Table}\\\hline
\end{tabular}
\end{document}

png-1


\multido command

The general syntax for \multido is:

1
\multido{variables}{repetitions}{stuff}

stuff is whatever you want repeated; it can be any balanced TeX input. repetitions is the number times stuff is repeated.

The first argument is the interesting one. variables is a comma-separated list of variable declarations. Each variable declaration is of the form:

1
variable = initial value + increment

variable is a command sequence that can be used in stuff. It is initially set to initial value, and is then incremented by increment with each repetition.

Example 1

Code source5

1
2
3
4
5
6
7
8
9
10
11
12
13
\documentclass{standalone}
\usepackage{multido}

\begin{document}
	\setlength{\unitlength}{1cm}
	\small
	\begin{picture}(8,1)(0,-.5)
		\put(0,0){\vector(1,0){8}}
		\multido{\i=0+1,\n=0+0.25}{8}{%
		\put(\i,-.1){\line(0,1){.2}}
		\put(\i,-.2){\makebox(0,0)[t]{\n}}}
	\end{picture}
\end{document}

png-1

Example 2

Code source5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
\documentclass[varwidth]{standalone}
\usepackage{multido}

\begin{document}
    \multido{}{10}{\TeX\ }

    \multido{\d=2pt+3pt}{5}{\d, }

    \multido{\n=2+3}{10}{\n, }

    \multido{\i=2+-3}{10}{\i, }

    \multido{\r=2+3.05}{6}{\r, }

    \multido{\n=2.00+-3.05}{8}{\n, }
\end{document}

png-1


References