# For-loops with different iterations

I have to print tickets for a theater. The seating plan of the theater is as follows:

Blocks (A right, A left, B right, B left, etc., through H)

Rows (Block A could have rows from 1 to 15, Block B from 1 to 20, and so on: they are not all the same in number)

Seat (Block A right row 1 could have seats from 1 to 4, Block A right row 2 from 1 to 22, and so on: again, not all the same in number)

I'm aware of the existence of for-loops in Latex, and I have successfully used them before, but I have the problem of how to tell the loop that it has to stop at row 15 if the Block is A, but it has to go to 20 if the Block is B, etc. Same for the seats (whereas if I solve the problem for the rows, I can solve it for the seats).

I though about putting the information about the number of rows per Block and the number of seats per row in a table, but then I don't know how to get the for loop to read the table. Or maybe the for is not right in this case? Maybe a while ... do would be more appropriate? But then I don't know how to implement that in Latex.

• Which for-loop-construction do you use currently (please show some code)? Jul 3, 2017 at 12:14
• @TeXnician. I didn't even know how to start writing some code for this example, but I normally like the \foreach of package pgffor. Jul 3, 2017 at 13:12

# Using the pgffor package

One possibility:

\documentclass{article}
\usepackage{pgffor}

\begin{document}

\foreach \block/\rows in {
A right/{4,22,31},
A left/{4,5},
B right/{10,4,3,4,5},
} {
\foreach \seats [count=\row] in \rows {
\foreach \seat in {1, ..., \seats} {

Block: \block\  Row: \row, Seat:\seat
}
}
}
\end{document}


Each block is defined using a block name and a list of the row sizes.

• Nice and compact solution. I ended up using this one, modifying for "symmetrical" theater (one more for-loop for printing "right" and "left"). Jul 3, 2017 at 13:21

Since the OP gave no indication that a row's seat number may begin with something other than 1, I assumed that, but could change accordingly. Syntax:

\printtickets{<section name> : <row 1 seats>, <row 2 seats>,.../ ...}


The MWE:

\documentclass[12pt]{article}
\usepackage{listofitems,pgffor,stackengine}
\usepackage[margin=.5cm]{geometry}
\newcommand\printtickets[1]{\noindent%
\setsepchar[@]{/@:@,}%
\foreachitem\x\in\seatcount[]{%
\foreach\y in {1,...,{\listlen\seatcount[\xcnt,2]}}{%
\foreach\z in {1,...,{\seatcount[\xcnt,2,\y]}}{%
\Longstack{Block~\seatcount[\xcnt,1] Row~\y{} Seat~\z}}}}\kern3pt\allowbreak%
}%
}%
}%
}
\begin{document}
\sloppy
\printtickets{
A right : 3,5,7     /
A left  : 3,5,6     /
B right : 4,5,6,9,11/
B left  : 4,5,6,9,11
}
\end{document}


• Your assumption is correct. All the rows start with seat number 1. Jul 3, 2017 at 13:07

something like this

\documentclass{article}

\makeatletter

\def\seats#1#2#3#4{%
\edef\minrow{\beforedash#2-\relax}%
\edef\maxrow{\afterdash#2-#2-\relax}%
\tickets{#1}\minrow\maxrow{#3}{#4}%
}
\def\beforedash#1-#2\relax{#1}
\def\afterdash#1-#2-#3\relax{#2}
\def\tickets#1#2#3#4#5{%
\count@#4 %
\loop
Block: #1, Row: \number#2, Seat: \the\count@\par
\ifnum\count@<#5 %
\repeat
\ifnum#2=#3 %
\expandafter\@gobble
\else
\expandafter\@firstofone
\fi
{\tickets{#1}{\numexpr#2+1\relax}{#3}{#4}{#5}}}

\begin{document}

%     block    row   low high
\seats{A Left} {2-6} {1}  {10}
\seats{A Left} {1}   {2}  {8}
\seats{B Right}{1-8} {1}  {12}
\seats{B Right} {9}  {10}  {12}
\seats{B Right} {10} {10}  {12}

\end{document}

• Nice solution for the case where the rows do not start with seat number 1. Also nice for not using extra packages. Jul 3, 2017 at 13:08