4

I'm looking for a way to generate in a loop the rows of an align environment, or IEEEeqnarray or some such thing. I can see that \foreach works in math mode (inside \[ \] e.g.), but something like this

\begin{align*}
   \foreach \i in {1,...,5}
   {
      V_\i & = 12345
   }
\end{align*}

fails with

! Extra }, or forgotten \endgroup.
<template> }
            $}\ifmeasuring@ \savefieldlength@ \fi \set@field \endtemplate
l.161 \end{align*}

Can I make this work without much hassle?

oarfish
  • 1,419
  • 1
    have a look at http://tex.stackexchange.com/questions/165126/how-do-i-use-the-ampersand-inside-a-foreach-or-conditional-or-other-group-e – cmhughes Dec 18 '15 at 15:47

1 Answers1

5

The problem is that \foreach is evaluated in a group to prevent overwriting of the contents of the iteration variable. TeX does not allow alignment tabs to grouped in alignments, hence the error. You could use \pgfplotsforeachungrouped from the pgfplots package to circumvent that restriction. Also you might want to have a look at the formatting again. The output doesn't look like this is desired. You should also brace the index or you will be surprised as you hit two digit indices.

\documentclass{article}
\usepackage{mathtools,pgfplots}
\begin{document}
\begin{align*}
   \pgfplotsforeachungrouped \i in {1,...,5}
   {
      V_{\i} & = 12345
   }
\end{align*}
\end{document}

enter image description here

Henri Menke
  • 109,596