# Iterating over two lists of text elements (or a list of pairs)

Let's assume I have two lists of text elements - L1,L2,L3,...,Ln and R1,R2,R3,...Rn - or a list of pairs - (L1,R1),(L2,R2),(L3,R3),...,(Ln,Rn). I want to iterate over them in order to produce an itemize list within a 2-column paracol environment, like this:

\begin{paracol}{2}
\begin{itemize}
\item L1
\switchcolumn
\item R1

\switchcolumn*

\item L2
\switchcolumn
\item R2

\switchcolumn*

\item L3
\switchcolumn
\item R3

\switchcolumn*

...

\switchcolumn*

\item Ln
\switchcolumn
\item Rn
\end{itemize}
\end{paracol}


How could I achieve this?

Here is a plain LaTeX solution. Add the following lines to your preamble.

\makeatletter
\newcommand\dopairs[1]{\@for\@tmp:={#1}\do{\expandafter\dopair\@tmp}}
\makeatother
\newcommand\dopair[2]{\item #1\switchcolumn\item #2\switchcolumn*}


The command \dopair is responsible for typesetting a pair, where #1 corresponds to the left item and #2 to the right one. \dopairs takes a single argument, a comma separated list of left-right pairs.

\documentclass{article}
\usepackage{paracol}
\makeatletter
\newcommand\dopairs[1]{\@for\@tmp:={#1}\do{\expandafter\dopair\@tmp}}
\makeatother
\newcommand\dopair[2]{\item #1\switchcolumn\item #2\switchcolumn*}
\begin{document}
\begin{paracol}{2}
\begin{itemize}
\dopairs{
{$L_1$}{$R_1$},
{$L_2$}{$R_2$},
{$L_3$}{$R_3$},
{$L_4$}{$R_4$},
{$L_5$}{$R_5$}
}
\end{itemize}
\end{paracol}
\end{document}


Edit: There are several ways to keep the processing of pairs flexibel.

• Redefine \dopair before calling \dopairs.

\renewcommand\dopair[2]{...}
\dopairs{...}

• Prepare all the ways that pairs can be processed, and \let the macro \dopair to the required definition before calling \dopairs.

\newcommand\dopairA[2]{...}
\newcommand\dopairB[2]{...}
\newcommand\dopairC[2]{...}
...
\let\dopair\dopairB
\dopairs{...}

• Prepare all the ways that pairs can be processed as separate macros, and hand the name of the macro over to \dopairs as another argument. In this case you have to redefine \dopairs as follows.

\makeatletter
\newcommand\dopairs[2]{\@for\@tmp:={#2}\do{\expandafter#1\@tmp}}
\makeatother
\newcommand\dopairA[2]{...}
\newcommand\dopairB[2]{...}
\newcommand\dopairC[2]{...}
...
\dopairs\dopairA{{}{},{}{},...}

• Specify the processing of pairs ad hoc when calling \dopairs. Redefine \dopairs as follows.

\makeatletter
\newcommand\dopairs[2]{%
\def\dopair[2]{#1}%
\@for\@tmp:={#2}\do{\expandafter\dopair\@tmp}%
}
\makeatother


For the example, \dopairs has to be used as follows:

\dopairs{\item #1\switchcolumn\item #2\switchcolumn*}%
{{}{},{}{},{}{},...}


Here is the complete code of the example, giving the same output as shown above.

\documentclass{article}
\usepackage{paracol}
\makeatletter
\newcommand\dopairs[2]{%
\def\dopair[2]{#1}%
\@for\@tmp:={#2}\do{\expandafter\dopair\@tmp}%
}
\makeatother
\begin{document}
\begin{paracol}{2}
\begin{itemize}
\dopairs{\item #1\switchcolumn\item #2\switchcolumn*}%
{
{$L_1$}{$R_1$},
{$L_2$}{$R_2$},
{$L_3$}{$R_3$},
{$L_4$}{$R_4$},
{$L_5$}{$R_5$}
}
\end{itemize}
\end{paracol}
\end{document}

• Thank you very much! In @gemot example, \dopair is hardcoded in the definition of \dopairs. How could I improve \dopairs definition so that I can be able to pass my mandler macro name (in place of \dopair) as a parameter? – Laércio de Sousa Jan 22 at 13:50
• @LaérciodeSousa See my extended answer. – gernot Jan 22 at 16:19