- Package used: pgffor
- Function used from package: foreach
- Compiled with: latex
the minimised document:
\documentclass[a4paper]{article}
\usepackage{pgffor}
\begin{document}
\def\colourNames{red, green, blue}
\begin{tabular}{cc}
\foreach \n in \colourNames {
\foreach [expand list=true] \m in {\colourNames} { \n & \m \ }
}
\end{tabular}
\end{document}
the error I receive:
! Extra }, or forgotten \endgroup.
<template> \unskip \hfil }
\hskip \tabcolsep \endtemplate
l.11 }
?
! Emergency stop.
<template> \unskip \hfil }
\hskip \tabcolsep \endtemplate
l.11 }
adding \endgroup does not help. deleting the "offending" } would not help as it does close the brace on two lines before.
If I replace the \tabular environment with an \itemize environment (and do the necessary changes to fit the \item syntax, the code is OK.
[...]
\begin{itemize}
\foreach \n in \colourNames {
\foreach [expand list=true] \m in {\colourNames} { \item \n, \m }
}
\end{itemize}
[...]
Most probably, the special tabular characters & and/or // cause the problem. But if so, how can they be escaped so that the compilation works? Is there any good documentation that shows the most elegant way to solve the problem, with in depth explanation of options and their pros and cons?
Note: The "update" section below has a summary of and additional info to the comments and answers I have received. I am still actively processing the information and leads I have received. When finished, I will clear up, complete, and move the "update" section into an answer as recommended by David Carlisle.
update 0: what I found useful so far:
- David Carlisle's comment helped understand what caused the error.
- both Qrrbrbirlbel and pascal974 point at a solution in LaTeX3 (reference to tutorial will come especially useful)
- Steven B. Segletes' answer seems to offer a simpler solution (using TeX tokens), but even for that, I need some time and research to understand.
- all in all: both solutions suggested so far seem good and need further study on my part, so I do not yet know which I will select in solving the original problem.
update 1: A good series to help get familiar with TeX tokens:
- A New Series of Articles: TeX Tokens and Related Concepts—But Why (and How)?
- What is a "TeX token"?
- What is a TeX token list
Some further reference(s?) I found useful while reading the series above:
- for token expansion: The puzzling thing about \newtoks and the token list
update 2: The third answer (using OpTeX) is the simplest but it leads into a parallel universe (from LaTeX viewpoint). It is extremely useful for those who are just starting off and are not planning using LaTeX features. OpTeX Package documentation is excellent.
update 3: as it turns out, the LaTeX3 solution was the easier and quickest to expand to address the original problem, from which the minimum example was extracted. It accepts an additional user-defined function easier into the table. So, this is why the green tick goes there.
update 4:
The opportunity of adding a new answer has been blocked unfortunately. The linked etoolbox solution is plagued by the same problem as the TeX token solution. Adding a user defined command inside can kill it. Therefore, it is still the LaTeX3 option that seems to be the most bulletproof solution. (Regardless, for reference: etoolbox Package Documentation)


&acts like}{ending one group and starting another,\foreachhas to be in a single group – David Carlisle Jan 09 '24 at 22:34\int_step_inline:nnnnfrom Latex3. – Qrrbrbirlbel Jan 10 '24 at 13:54