3

Suppose I have the following latex file:

\documentclass{article}
\usepackage{amsmath}

\begin{equation}
  a+b
\end{equation}

\begin{equation}
  a^2+b^2
\end{equation}

\begin{equation}
  a^2+b^2=c^2
\end{equation}

\end{document}

I would like to avoid rewriting the equation environment for every new equation. Is it possible to do the following in LaTeX?

  • Assign the equations to a variable
  • Loop through the equations such that I only explicitly write the equation environment once

If so, how? I saw that TikZ does supply a for-each type command, but it seems like an overkill to import all of TikZ just for that, since I am not generating any diagrams.

5
  • 2
    note you should never have one equation after another (tex will insert spurious white paragraphs in between) this should be marked up as a single gather environment (or align if you want to align on =) and then it only needs \\ between the equations Oct 8, 2021 at 7:08
  • I have standard text both before and after each equation. The .tex file I provide above is just a minimum working example.
    – user32882
    Oct 8, 2021 at 7:09
  • I assumed you didn't have any text (as how do you want to insert that in the loop, or do you mean it is the same text in each case?) Oct 8, 2021 at 7:13
  • It's the same text in each case.
    – user32882
    Oct 8, 2021 at 7:13
  • (La)TeX is not an ordinary programming language. It seems to me that what you want can best be accomplished using an editor tailored for TeX. There are several such, depending on the hardware/operating system you are using. One of the goals of TeX is to have a human (as well as computer) readable input file, and what you are asking for is to cripple that goal. Oct 8, 2021 at 17:39

3 Answers 3

8

Using consecutive equation environments is wrong, more so if you separate them with blank lines. Use the appropriate amsmath environments such as align or gather.

I'm not sure that a syntax such as

\printequations{a+b=c \\ a^2+b^2 \\ a^2+b^2=c^2}

is really better than

\begin{gather}
a+b=c \\
a^2+b^2 \\
a^2+b^2=c^2
\end{gather}

but you can obtain what you want in a simple way:

\documentclass{article}
\usepackage{amsmath}

\newcommand{\printequations}[1]{%
  \begin{gather}#1\end{gather}%
}

\begin{document}

\printequations{a+b=c \\ a^2+b^2 \\ a^2+b^2=c^2}

\end{document}

enter image description here

If you prefer a different syntax,

\documentclass{article}
\usepackage{amsmath}

%\usepackage{xparse}% uncomment for LaTeX prior to 2020-10-01

\ExplSyntaxOn
\NewDocumentCommand{\printequations}{m}
 {
  \begin{gather}
  \seq_set_split:Nnn \l_tmpa_seq {} { #1 }
  \seq_use:Nn \l_tmpa_seq { \\ }
  \end{gather}
 }
\ExplSyntaxOff

\begin{document}

\printequations{{a+b}{a^2+b^2}{a^2+b^2=c^2}}

\end{document}

will produce the same as before.

If your aim is to define a set of equations and use them one after the other, you can do

\documentclass{article}
\usepackage{amsmath}

%\usepackage{xparse}% uncomment for LaTeX prior to 2020-10-01

\ExplSyntaxOn

\seq_new:N \l_egreg_equations_list_seq

\NewDocumentCommand{\defineequations}{m}
 {
  \seq_set_split:Nnn \l_egreg_equations_list_seq {\\} { #1 }
 }
\NewDocumentCommand{\nextequation}{}
 {
  \seq_pop_left:NN \l_egreg_equations_list_seq \l_tmpa_tl
  \begin{equation}
  \l_tmpa_tl
  \end{equation}
 }

\ExplSyntaxOff

\begin{document}

\defineequations{a+b \\ a^2+b^2 \\ a^2+b^2=c^2}

Some text before the first equation
\nextequation
Some other text between the first and the second equation
\nextequation
Some other text between the second and third equation
\nextequation
Some final text

\end{document}

enter image description here

If you want to index your equations here is a possibility. The kay before the = is an arbitrary string.

\documentclass{article}
\usepackage{amsmath}

%\usepackage{xparse}% uncomment for LaTeX prior to 2020-10-01

\ExplSyntaxOn
\NewDocumentCommand{\defineequations}{m}
 {
  \prop_set_from_keyval:Nn \l_egreg_equations_list_prop { #1 }
 }
\NewDocumentCommand{\useequation}{m}
 {
  \begin{equation}
  \prop_item:Nn \l_egreg_equations_list_prop { #1 }
  \end{equation}
 }

\ExplSyntaxOff

\begin{document}

\defineequations{
 1={a+b},
 2={a^2+b^2},
 3={a^2+b^2=c^2}
}
Some text before the first equation
\useequation{2}
Some other text between the first and the second equation
\useequation{1}
Some other text between the second and third equation
\useequation{3}
Some final text

\end{document}

enter image description here

6
  • 1
    If using consecutive equation environments is wrong, how do you propose having multiple equations in one publication?!
    – user32882
    Oct 8, 2021 at 12:46
  • 1
    @user32882 Use gather (as I did) or align.
    – egreg
    Oct 8, 2021 at 12:49
  • 1
    gather doesn't allow you to put explanatory text before and after the equation... Please see my response to @David Carlisle
    – user32882
    Oct 8, 2021 at 13:04
  • 1
    So, sorry, but it's quite unclear what you want to achieve.
    – egreg
    Oct 8, 2021 at 13:39
  • 1
    I think I'm looking for LaTeX to behave like some of the more "common" programming language. In python I'm accustomed to doing something like x = ["eq1", "eq2", "eq3"] and following that with equation(x) for item in x. However, I'm realizing that TeX does not really work in this way. I might have to get more familiar with plain TeX to understand how it deals with iteration.
    – user32882
    Oct 8, 2021 at 13:49
6

\@tfor is part of the LaTeX format:

\@tfor\Macroname:=Tokenlist\do{stuff to be done}

In order to use @ in the macro name \@tfor, you have to wrap its use within \makeatletter and \makeatother (unless the definition is in a file loaded with \usepackage). The % signs avoid spurious spaces.

\documentclass{article}
\makeatletter
\newcommand\manyequations[1]{%
  \@tfor\Equation:=#1\do{%
    \begin{equation}
      \Equation
    \end{equation}%
  }%
}
\makeatother
\begin{document}
\manyequations{{a+b}{a^2+b^2}{a^2+b^2=c^2}}
\end{document}

enter image description here

5

There are several looping macros available including \@for in the format but it's simpler (and more efficient actually) just to use a custom macro that is directly executed, as all looping macros essentially end up being syntactic sugar around such an execution.

enter image description here

\documentclass{article}
\usepackage{amsmath}


\begin{document}


\section{testing\ldots}
\newcommand\mydo[1]{Some text\begin{equation}#1\end{equation}}

\mydo{a+b}\mydo{a^2+b^2}\mydo{a^2+b^2=c^2}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .