# Use foreach loop within formulas?

I was trying to use the \foreach loop from tikz to shorten a list of formlas. Within each formula, only an index changes, so I thought I could use foreach within an align environment write each formula on one line. This does not work unless I remove the linebreaks \\ after r^2. Is there a way to use line breaks within foreach?

\documentclass[10pt]{article}
\usepackage{tikz}    % for foreach
\usepackage{amsmath} % for align

\begin{document}

\begin{align*}
\foreach \i in {1,2,3}{ (x_\i - x_c)^2 + (y_\i - y_c)^2 =  r^2  \\ }
\end{align*}

\end{document}

• \foreach cannot straddle alignment cells – egreg Jan 24 '17 at 11:41

You're using a sledgehammer for a simple task with copy-paste. Anyway, this can't work for at least two reasons: the code of every \foreach cycle is executed in a group and align builds a table the same as tabular, so each cell forms a group as well.

Even if \foreach didn't enclose each cycle in a group, it couldn't straddle cells.

You could do

\documentclass[10pt]{article}
\usepackage{pgffor}    % for foreach
\usepackage{amsmath} % for align
\usepackage{etoolbox}

\newcommand{\aligntemp}{}

\begin{document}

\gdef\aligntemp{}%
\foreach \i in {1,2,3}{%
\xappto\aligntemp{(x_\i - x_c)^2 + (y_\i - y_c)^2 &=  r^2  \noexpand\\ }%
}%
\begin{align*}
\aligntemp
\end{align*}

\end{document}


Is it worth the trouble? Note that “risky” commands such as \text should be prefixed by \noexpand (not only \text, of course). Also, \\ at the end would produce an undesired line.

Here's a better version:

\documentclass[10pt]{article}
\usepackage{xparse}
\usepackage{amsmath}

\ExplSyntaxOn
\NewDocumentCommand{\mathrepeat}{O{align*}mm}
{
\seq_clear:N \l_flawr_mathrepeat_seq
\clist_map_inline:nn {#2}
{
\seq_put_right:Nn \l_flawr_mathrepeat_seq { #3 }
}
\begin{#1}\seq_use:Nn \l_flawr_mathrepeat_seq { \\ }\end{#1}
}
\seq_new:N \l_flawr_mathrepeat_seq
\ExplSyntaxOff

\begin{document}

\mathrepeat{1,2,3}{
(x_{#1} - x_c)^2 + (y_{#1} - y_c)^2 &=  r^2
}

\end{document}


The output is the same as before.

The optional argument to \mathrepeat is the name of the environment to use; for instance \mathrepeat[align]{1,2,3}{...} would number the lines.

\documentclass[10pt]{article}
\usepackage{xparse}
\usepackage{amsmath}

\ExplSyntaxOn
\NewDocumentCommand{\mathrepeat}{O{align*}mm}
{
\seq_clear:N \l_flawr_mathrepeat_seq
\clist_map_inline:nn {#2}
{
\seq_put_right:Nn \l_flawr_mathrepeat_seq { #3 }
}
\begin{#1}\seq_use:Nn \l_flawr_mathrepeat_seq { \\ }\end{#1}
}
\seq_new:N \l_flawr_mathrepeat_seq
\ExplSyntaxOff

\begin{document}

\mathrepeat[align]{1,2,3}{
(x_{#1} - x_c)^2 + (y_{#1} - y_c)^2 &=  r^2 \label{myeqs#1}
}

We see in \eqref{myeqs1}--\eqref{myeqs3} that \dots

\end{document}


• Of course like this you also get the problem that the last line has \\ . – Torbjørn T. Jan 24 '17 at 13:38
• Great, thank you very much=) The reason I don't want to copy paste it, is that I was told that I cannot just use a general index $i$ for some reason, but have to write each equation out (not my decision...), so that saves quite some hassle. – flawr Jan 24 '17 at 13:53
• @TorbjørnT. Yes, that's true. But it was just to give an example how the code should be written, which is already a stopper, in my opinion. – egreg Jan 24 '17 at 14:03
• @flawr I added a higher level set of macros that you might enjoy more. – egreg Jan 24 '17 at 14:14
• Wow that is even a better solution for my purpose, thank you very much! – flawr Jan 24 '17 at 15:31
\documentclass[10pt]{article}
\usepackage{tikz}    % for foreach
\usepackage{amsmath} % for align

\begin{document}
\foreach \i in {1,2,3}{\expandafter\xdef\csname eq\i\endcsname{%
(x_\i - x_c)^2 + (y_\i - y_c)^2 =  r^2}}
\begin{align*}
\csname eq1\endcsname\\
\csname eq2\endcsname\\
\csname eq3\endcsname
\end{align*}

\end{document}