user32882
user32882

Reputation: 5907

Simultaneously iterating through multiple "lists" in LaTeX

The pgffor package in LaTeX allows for looping through commands which contain "listlike" information. As mentioned in section 88 of the pgf manual, the pgffor package also allows for iterating simultaneously through two sets of "listlike" information, a bit like python's zip function. Here's an example of this:

\documentclass{article}

\newcommand\iterable{
  item1/description1,
  item2/description2,
  item3/description3}

\usepackage{pgffor}
\begin{document}
  \foreach \i/\q in \iterable{
    \noindent \i \q\\
  }
\end{document}

The above will iterate simultaneously through items in \iterable separated by a forward slash character /. However, if I have a high number of different "lists", then maintaining them all within the \iterable command could prove difficult.

Does pgffor allow for a way in which to separate the iterable items into different commands such that I can better keep track of the items that need to get iterated over? For example:

\newcommand\iterable1{item1,item2,item3}
\newcommand\iterable2{description1, description2, description3}

Upvotes: 2

Views: 1298

Answers (1)

Might not be the fastest solution, but you could iterate over both list:

\documentclass{article}

\newcommand\iterablea{item1,item2,item3}
\newcommand\iterableb{description1, description2, description3}

\usepackage{pgffor}
\begin{document}
  \foreach \i [count=\xi] in \iterablea {
  \foreach \q [count=\xq] in \iterableb {
    \ifnum\xi=\xq
      \noindent \i{} \q\par
    \fi
  }}
\end{document}

Upvotes: 1

Related Questions