4

I would like to automatically generate a list of equations present in the document, which includes the equation itself. Here is a MWE:

\documentclass{article}
\def\caption#1{}
\begin{document}
First Euler observed that
\begin{equation}
1 + e^{i \Pi} = 0.
\caption{Euler's equation tying together the five fundamental constants of mathematics}
\end{equation}

Then, Gauss discovered the normal distribution:
\begin{equation}
P(x) = \frac{1}{{\sigma \sqrt {2\pi } }}e^{{{ - \left( {x - \mu } \right)^2 } \mathord{\left/ {\vphantom {{ - \left( {x - \mu } \right)^2 } {2\sigma ^2 }}} \right. \kern-\nulldelimiterspace} {2\sigma ^2 }}}
\caption{Normal distribution}
\end{equation}

Finally, Einstein proclaimed: 
\begin{equation}
E = m C^2
\caption{Equivalence of mass and energy}
\end{equation}

\end{document}

Here is an example of how the output might look, but any other format may be fine; it would of course be nice to have hyperref links, page numbers, etc, but these are not essential.

\section*{List of equations}
\begin{enumerate}
  \item Euler's equation tying together the five fundamental constants of mathematics
        \[
          1 + e^{i \Pi} = 0.
        \]
  \item Normal distribution:
        \[
          P(x) = \frac{1}{{\sigma \sqrt {2\pi } }}e^{{{ - \left( {x - \mu } \right)^2 } \mathord{\left/ {\vphantom {{ - \left( {x - \mu } \right)^2 } {2\sigma ^2 }}} \right. \kern-\nulldelimiterspace} {2\sigma ^2 }}}
        \]
  \item Equivalence of mass and energy
        \[
          E = m C^2
        \]
\end{enumerate}
  • Off-topic: Are you sure you want to have this in your document? – Johannes_B Jan 24 '15 at 17:06
  • Absolutely. I produce course notes for my students, and it would be useful to generate a cheat sheet for them. – Yossi Gil Jan 24 '15 at 17:27
  • 1
    One design possibility: use the environ package to wrap equations in a NewEnviron that writes its BODY both to the current document and an external file. At the end of your document, input some macros to change the behavior of the saved BODY and input the external file. You could then produce your cheatsheet as a separate document if you wished. – Ethan Bolker Jan 24 '15 at 17:36
  • 1
    @EthanBolker Technically prett close to: How to defer content to a later part of the document? Right? – Johannes_B Jan 24 '15 at 19:47
5

Since you want a cheat sheet, I'd recommend the extract package. With a slightly different MWE:

\documentclass{article}
\usepackage[
  active,
  header=false,
  copydocumentclass=true,
  generate=\jobname-Cheatsheet,
  extract-env={equation},
  extract-cmdline={synopsis},
  ]{extract} % http://ctan.org/pkg/extract
\begin{extract*}
% Items executed in both the main and extracted document
% (extract manual, section 5.1)
\usepackage{amsmath}
\end{extract*}
\begin{extract}
% Items executed only in extracted document
\def\synopsis#1{#1}
\end{extract}
\def\synopsis#1{}
\begin{document}
First Euler observed that
\synopsis{Euler's equation tying together the five fundamental constants of mathematics}%
\begin{equation}
1 + e^{i \Pi} = 0.
\end{equation}

Then, Gauss discovered the normal distribution:
\synopsis{Normal distribution}%
\begin{equation}
P(x) = \frac{1}{{\sigma \sqrt {2\pi } }}e^{{{ - \left( {x - \mu } \right)^2 } \mathord{\left/ {\vphantom {{ - \left( {x - \mu } \right)^2 } {2\sigma ^2 }}} \right. \kern-\nulldelimiterspace} {2\sigma ^2 }}}
\end{equation}

Finally, Einstein proclaimed: 
\synopsis{Equivalence of mass and energy}%
\begin{equation}
E = m C^2
\end{equation}
\end{document}

you get an original document of:

enter image description here

and an extracted document of:

\documentclass{article}
% Items executed in both the main and extracted document
% (extract manual, section 5.1)
\usepackage{amsmath}
% Items executed only in extracted document
\def\synopsis#1{#1}

\begin{document}

\synopsis{Euler's equation tying together the five fundamental constants of mathematics}%

\begin{equation}
1 + e^{i \Pi} = 0.
\end{equation}

\synopsis{Normal distribution}%

\begin{equation}
P(x) = \frac{1}{{\sigma \sqrt {2\pi } }}e^{{{ - \left( {x - \mu } \right)^2 } \mathord{\left/ {\vphantom {{ - \left( {x - \mu } \right)^2 } {2\sigma ^2 }}} \right. \kern-\nulldelimiterspace} {2\sigma ^2 }}}
\end{equation}

\synopsis{Equivalence of mass and energy}%

\begin{equation}
E = m C^2
\end{equation}

\end{document}

and:

enter image description here

This also works with your original \caption command.

  • Elegant indeed! – Yossi Gil Jan 31 '15 at 7:20
2

I highly recommend you to look into the float package, with it comes the command \listof that can be used to generate lists of self defined floating environments.

You may then define your own equation environment and use the \listof command to generate a list of equations.

Here is a minimal example:

\documentclass{article}
\usepackage{float}
\floatstyle{plain}
\newfloat{myequation}{H}{eq}[section]
\floatname{myequation}{Equation}
\begin{document}
\listof{myequation}{List of Equations}
\section{First Section}
First Euler observed that
\begin{myequation}
\begin{equation}
1 + e^{i \Pi} = 0.
\end{equation}
\caption{Euler's equation tying together the five fundamental constants of mathematics}
\end{myequation}
\end{document}

However now the equation caption is numbered as well as the equation so you may want to use the stard version of the equation environment to generate your inner equation, so to suppress the numbering of the inner equation. The equation* environment may be found in the amsmath package.

Inclusion of the actual equation in the list of equations may be hacked using the optional parameter to the \caption command like this:

\caption[$$1 + e^{i \Pi} = 0$$]{caption part not to be included in the list of equations}

However, if the equation spans more than one line, like say a fraction, this method does not work...

2

The answer based on extract environment sounded really promising and elegant, but it turned out to be a bit fragile, especially with beamerarticle. At times it was fussy for apparently no reason until spaces prior to the \begin{equation} where changed.

Here is an alternative using the environ package. Here is how is used:

\documentclass[twocolumn,10pt]{article}
\title{Demo of \texttt{generate-list-equations}}
\author{Yogi the bear}
\usepackage[fleqn]{amsmath}
\usepackage{generate-list-of-equations}

\begin{document}
\maketitle
\section{Start with a list of all equations}
\listofequations

\section{Main matters}
People say that \begin{equation}1+1=2\end{equation} but how do we know that this is true?

First, Euler observed that
\begin{equation}
  1 + e^{i \pi} = 0.
  \synopsis{Euler's equation tying together the five fundamental constants of mathematics}%
\end{equation}
Then, Gauss discovered the normal distribution:
\begin{equation}
  P(x) = \frac{1}{{\sigma \sqrt {2\pi }}} e^{-\frac{(x - \mu)^2} {2 \sigma^2}}
  \synopsis{Normal distribution}%
\end{equation}
Finally, Einstein proclaimed:
\begin{equation}
  E = m C^2
  \synopsis{Equivalnce of mass and energy}
\end{equation}

Only then, it became clear that 
\begin{equation}
  2 + 2 = 4
  \synopsis{Two and two before!}
\end{equation}

\section{The END}
That's all folks!
\section{PS. The list of equations can be placed anywhere}
\listofequations
\end{document}

enter image description here

enter image description here

Here is the actual code; it is written in an .sty file, but it is not polished enough to be considered a package yet. If you need to use it, it is likely that you would need to tweak it.


\RequirePackage{amsmath}
\RequirePackage{environ}

% The main command generates a list of equations by
% simply \input ting `\jobname.leq`
\newcommand\listofequations{%
  \IfFileExists{\jobname.leq}{\input{\jobname.leq}}{\relax}
}

% File "\jobname.eqn" collects all equations
\AtBeginDocument{%
  \newwrite\equationsListFile
  \immediate\openout\equationsListFile=\jobname.eqn%
  \immediate\write\equationsListFile{\unexpanded{\begingroup\description}}
  \immediate\write\equationsListFile{
    \unexpanded{\def\synopsis}
    \hashchar
    \unexpanded{1{}}
  }
  \immediate\write\equationsListFile{\unexpanded{\setlength{\itemsep}{2pt}}}
  \immediate\write\equationsListFile{\unexpanded{\abovedisplayskip=3pt plus 3pt minus 2pt}}
  \immediate\write\equationsListFile{\unexpanded{\belowdisplayskip=2pt plus 3pt minus 2pt}}
}

% Copy "\jobname.eqn" to "\jobname.leq"
\AtEndDocument{%
  \immediate\write\equationsListFile{\unexpanded{\enddescription\endgroup}}
  \immediate\closeout\equationsListFile
  \newread\in
  \openin\in=\jobname.eqn
  \newwrite\out
  \immediate\openout\out\jobname.leq
  \endlinechar-1
  \loop \unless\ifeof\in
  \readline\in to\l
  \immediate\write\out{\l}
  \repeat
  \immediate\closeout\out
  \closein\in
}

% Save previous \begin{equation} and \end{equation} commands:
\let\beginEquation=\equation
\let\endEquation=\endequation

% Undefine them, so that that the environment can be redefined:
\providecommand*\@nameundef[1]{%
  \expandafter\let\csname #1\endcsname\@undefined
}
\@nameundef{equation}
\@nameundef{endequation}

\NewEnviron{equation}{{%
  % Default equation name is empty:
  \gdef\equationName{}
  % Let the synposis command record the equation's name
  \def\synopsis##1{\gdef\equationName{##1}}
  % Generate equation in document as usual (but hook on \labe)
  \beginEquation
  \BODY
  \label{eq:\theequation}
  \endEquation
  \@onelevel@sanitize\BODY
  \@onelevel@sanitize\equationName
  % Generate an entry in \jobname.leq (tweak as necessary)
  \immediate\write\equationsListFile{\unexpanded{\pagebreak[3]}}
  \immediate\write\equationsListFile{\unexpanded{\item}}
  \immediate\write\equationsListFile{[(\unexpanded{\ref}{eq:\theequation})]}
  \immediate\write\equationsListFile{\equationName\unexpanded{~~\dotfill~~}\thepage}
  \immediate\write\equationsListFile{\unexpanded{\nopagebreak[3]}}
  \immediate\write\equationsListFile{\unexpanded{\\\begin{equation*}}}
  \immediate\write\equationsListFile{\BODY}
  \immediate\write\equationsListFile{\unexpanded{\end{equation*}}}
}}

% globally defines a macro \hashchar which holds a verbatim `#`
\begingroup
\catcode`\#=12
\gdef\hashchar{#}%
\endgroup

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.