5

I have this code:

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[margin=2.5cm]{geometry}
\usepackage{lipsum}

\title{Test}
\author{Andreas Melsen}
\date{September 2018}

\begin{document}

\section*{Finance Notes}
\lipsum[2]
\begin{equation}
    PV=\sum_{n=1}^{\infty}\frac{C}{(1+r)^n}
\end{equation}

\lipsum[4]
\begin{equation}
    r_r=\frac{r-i}{1+i}\approx{}r-i
\end{equation}

\section*{List of Equations}

\begin{equation}
    PV=\sum_{n=1}^{\infty}\frac{C}{(1+r)^n}
\end{equation}

\begin{equation}
    r_r=\frac{r-i}{1+i}\approx{}r-i
\end{equation}

\end{document}

which produces this output:

enter image description here

I am currently following a course on corporate finance, where a sample of my notes could look something like the image above. Throughout the notes, I will be writing various equations, and my question is, if it is possible to at the end of the document automatically list all the equations used, as shown in the "List of Equations" section? Thanks in advance!

  • 1
    Does Table of equations like list of figures help? – Bobyandbob Sep 12 '18 at 9:04
  • 2
    @Bobyandbob Thanks for your comment. Hmm, not exactly (if I understand the answer to that question correctly). There it seems that the person wants to have a ToC of the equations used. What I am trying to do, is to "extract" all the equations from the document, such that I have an easy overview at the end of the document of all the equations used. So that every time I write an equation, it would be duplicated at the end of the document, if that makes sense. – Andreas Sep 12 '18 at 9:16
  • 3
  • @StevenB.Segletes Thanks for your comment. As I understand it, the one you're referring to allows to recall an equation one by one, where I am seeking write some code that recalls all equations from the document. Does that makes sense? – Andreas Sep 13 '18 at 15:41
  • 1
    A loop could easily be set up to automate the recapitulation. However, the method still does require you (via added syntax) to pick and choose which equations to qualify for recapitulation. – Steven B. Segletes Sep 14 '18 at 1:51
4

The following code should do what you want. It uses a box register to store all of the equations in your document, and is not based on either of the two answers that are linked to in the comments.

\documentclass{article}

\usepackage{amsmath} %% Necessary for collect@body and \savecounters@/\restorecounters@
\usepackage{lipsum}  %% Unnecessary, for \lipsum

\newbox\savedeqs %% <- box register that will store your equations
\makeatletter %% <- change @ so that it can be used in command names
\newcommand\saveandprinteq[1]{% %% <- saves equation in a box register, then prints it
  \begingroup
    \expandafter\let\csname \@currenvir\expandafter\endcsname\csname listeq@\@currenvir\endcsname
    \expandafter\let\csname end\@currenvir\expandafter\endcsname\csname listeq@end\@currenvir\endcsname
    %% ^^ restore original environment definitions
    \edef\listeq@temp{% %% <- the full environment, with its original name
      \noexpand\begin{\@currenvir}%
        \unexpanded{#1}%
      \noexpand\end{\@currenvir}%
    }%
    \savecounters@ %% <- store counter values
      \global\setbox\savedeqs=\vbox{\unvbox\savedeqs\listeq@temp}% %% <- append to \savedeqs
    \restorecounters@ %% <- restore them
    \listeq@temp %% <- print the environment
  \endgroup
}
\newcommand*\listeqpatch[1]{% %% <- patches equation environment
  \expandafter\let\csname listeq@#1\expandafter\endcsname\csname #1\endcsname
  \expandafter\let\csname listeq@end#1\expandafter\endcsname\csname end#1\endcsname
  \renewenvironment{#1}{\collect@body\saveandprinteq}{}%
}
\newcommand\listofequations{ %% <- prints the list of equations
  \section*{List of equations}
  \unvbox\savedeqs
}
\makeatother %% <- change @ back

%% Patching equation environments
\listeqpatch{equation}
\listeqpatch{align}
\listeqpatch{gather}
\listeqpatch{multline}
\listeqpatch{flalign}

\begin{document}

\section{Finance Notes}
\lipsum[2]
\begin{equation}
    PV=\sum_{n=1}^{\infty}\frac{C}{(1+r)^n}
\end{equation}

\lipsum[4]
\begin{align} %% <- Using align just to show that it works
    r_r &= \frac{r-i}{1+i}\\
        &\approx r-i
\end{align}

\listofequations

\end{document}

(Note that I'm using an align environment for the second equation, just to show that it works.)

enter image description here

What this does

I'm defining the command \listeqpatch and using it to patch equation and the amsmath equation environments like align. This stores the original definition of these environments and then redefines these environments to call \saveandprinteq{<equation body>} instead of what they would normally do. (I'm using \collect@body from amsmath for this.)

What \saveandprinteq{<equation body>} does is (restore the original environment definition and use it to) typeset the equation and append a copy to the box register \savedeqs. This box register starts out empty, but by the end of your document it contains a copy of each equation up to that point. The command \listofequations prints \section*{List of equations} and then calls \unvbox\savedeqs to print the contents of this box register.

A few remarks

  • Calling \listofequations prematurely will print copies of all equations since the last time this command was called. If you have multiple chapters you could thus create separate lists of equations for them by adding \listofequations at the end of each chapter. You can clear the box register with \setbox\savedeqs=\vbox{}.
  • Because the box register contains a typeset version of the equation, things like equation numbers are preserved in the list of equations. If macros used in these equations are changed after the equation, this likewise won't affect the copy.
  • It is impossible to insert the list of equations before the actual equations themselves with this approach because box registers can't, to my knowledge, be written to the aux file.
  • Amazing, thank you very much for your in-depth answer! – Andreas Sep 14 '18 at 7:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.