7

Is there a way to create a command which adds to a table or otherwise typesets in a place specified by the user? Example of what I'm after

\documentclass{article}
\begin{document}
\tableofequations

\begin{equation}
e^{i\pi} = -1
\end{equation}\rememberequation{e^{i\pi}=-1}{Euler's Equation}

\end{document}

Where the \tableofequations command would render something like

%with \usepackage{array}
\begin{center}
\begin{tabular}{m{0.3\linewidth} m{0.3\linewidth} m{0.3\linewidth}}
Equation & Name & Page \\\hline
\[e^{i\pi} = -1\] & Euler's Equation & Page 1\\\hline
\end{tabular}
\end{center}

equation table

The effect I'm after is almost exactly recorded in this answer with the only difference being the facility to place the equation table before the equation is rendered (as a table of figures renders the captions before the figures are typeset). Possibly to be achevied through an auxilliary file, although I have almost no knowledge of how these work.

3
  • Does Table of Equations help you?
    – hpesoj626
    Commented Feb 5, 2013 at 16:10
  • Unfortunately not, I'm really after having the equations themselves in the table, and I can't find a way to use \addcontentsline to create a table or something similar with the maths in. Maybe an answer based on the source code for \addcontentsline would be helpful, but I can't find that either! Commented Feb 5, 2013 at 16:41
  • Nitpicking: Euler's equation is usually the one involving zero as well, i.e. $e^{i\pi}+1=0$ Commented Apr 3, 2013 at 15:19

1 Answer 1

5

This code uses the .aux file to store the relevant information. \rememberequation both typesets and saves the equation. On the second run, the data is automatically read in at the beginning of the document, building (a long) macro \listofequations, which is then used by \tableofequations to typeset the table (anywhere in the document).

\documentclass{article}
\usepackage{etoolbox}

\makeatletter
\newcommand\rememberequation[2]{%
  \begin{equation}#1\end{equation}%
  \protected@write\@mainaux{}
    {\detokenize{\gappto\listofequations}{%
        (\theequation)&
        \detokenize{$#1$}&
        \detokenize{#2}&
        \thepage\\
      }%
    }%
}
\makeatother
\newcommand\tableofequations{%
  \preto\listofequations{%
    \begin{tabular}{cccc}
      \#&Equation&Name&Page\\\hline
  }%
  \appto\listofequations{%
    \hline
    \end{tabular}
  }%
  \begin{center}
    \listofequations
  \end{center}
}

\begin{document}
\tableofequations

\rememberequation{e^{i\pi}=-1}{Euler's Equation}
\rememberequation{e^{i\pi}=-1}{Euler's Equation, again}
\newpage
\rememberequation{e^{i\pi}=-1}{Euler's Equation, yet again}

\end{document}

EDIT: the fancy version requested in the comment

So, the idea is that we write equations in the usual equation environment and trigger the "remembering" code by \caption. (I believe \caption is a better choice than \label.)

Before the code, a disclaimer. This is a hack. I have no idea what trouble it might cause in conjunction with some package ...

\documentclass{article}
\usepackage{etoolbox}
\usepackage{environ}

\makeatletter

\AtBeginEnvironment{equation}{%
  \Collect@Body\rememberequation
}

\newtoks\currentequation
\def\rememberequation#1{%
  \typeout{DEBUG: \detokenize{#1}}%
  % #1 starts with \csname equation\endcsname
  \expandafter\currentequation\expandafter{#1}%
  \let\caption\storecurrentequation
  #1%
}

\def\storecurrentequation#1{%
  \protected@write\@mainaux{}%
    {\detokenize{\gappto\listofequations}{%
        (\theequation)&
        $\expandafter\detokenize\expandafter{\the\currentequation}$&
        \detokenize{#1}&% #1 = caption
        \thepage\\
      }%
    }%
}

\newcommand\tableofequations{%
  \preto\listofequations{%
    \begin{tabular}{cccc}
      \#&Equation&Name&Page\\\hline
  }%
  \appto\listofequations{%
    \hline
    \end{tabular}
  }%      
  \begin{center}
    \let\equation\relax
    \let\label\@gobble
    \let\caption\@gobble
    \listofequations
  \end{center}
}

\makeatother


\begin{document}
\tableofequations

\def\caption#1{}

\begin{equation}
  \label{eq:1}
  e^{i\pi}=-1
  \caption{Euler's Equation}
\end{equation}

\begin{equation}
  \label{eq:2}
  e^{i\pi}=-1
  \caption{Euler's Equation, again}
\end{equation}

\newpage
\begin{equation}
  \label{eq:3}
  e^{i\pi}=-1
  \caption{Euler's Equation, yet again}
\end{equation}

\end{document}
4
  • That is incredible, works perfectly. Thanks! How possible do you think it would be to adapt it so that it could be used ala \label in figures with captions? Commented Feb 7, 2013 at 18:37
  • Re: fancy version, did it compile on your machine? I've just copied the code into a couple of machines and neither will compile, complaining of missing { inserted where the \captions are. I tried to fix it but... I really don't understand most of the tex! Commented Feb 7, 2013 at 21:56
  • yes it compiles, i've copied it back to check. if you're using the same file as before, try compiling several times, or manually deleting the .aux file. Commented Feb 7, 2013 at 21:58
  • Found the problem, had a rendering LaTeX extension running in the background on my firefox and it interfered with the verbatim section. Works like a charm now. Thanks for all your help! Commented Feb 7, 2013 at 22:25

You must log in to answer this question.

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