# Library of reusable equations

I want to do something that sounds simple. I want to create an external file where I will store my equations. Then I would like to be able to these equations and input them in a document without writing them all over again.

For example I would like something like that:

equations.tex

$$F = ma \label{newton2ndLaw}$$

$$det(A-\lambda I)=0 \label{eigValProb}$$


And so on. Then I would go on and use the above file in a document.

document.tex

%other \usepackages ...
\usepackage{Magic package that does what I want}
\useeqlibrary{equations.tex} %I just made up this command

\begin{document}
Lorem Ipsum text

$$\geteq{eigValProb} %made up this command also \label{anequationlabel}$$

\end{document}


During compilation the equation TeX code would be inserted automatically in the document. Something like: "Type once, use many times, keep code clean". Does anyone of you have anything in mind?

• – clemens Jan 10 '14 at 0:01
• If the solution(s) provided in the link are sufficient, then this is a duplicate. If you want to stick to your (suggested) interface - that is, using \label as the referencing scheme for inclusion, then this is not a duplicate. Please give some feedback. – Werner Jan 10 '14 at 1:07
• Personally I would prefer extraction by label. Code is kept clean that way. – msmechanized Jan 10 '14 at 10:55

The following uses some concepts from Is it possible to write all mathematical formulas in a separate file and add them in main file on-demand? to achieve your goal. It focuses on a redefinition of the equation environment to

1. capture the give \label; and
2. store the content of the equation environment in a macro \equation@<label>.

Retrieval is possible via \geteqn{<label>}. Since the stored content includes the original \label, there's technically no need to re-\label equations, but that doesn't really matter.

\documentclass{article}
\usepackage{filecontents,environ,etoolbox}% http://ctan.org/pkg/{filecontents,environ,etoolbox}
\robustify\label% To avoid premature expansion of \label when storing content

\let\oldequation\equation% Store $$\let\endoldequation\endequation% Store$$

\makeatletter
\def\eq@l@bel@gr@b#1\label#2#3\@nil{%
\gdef\eq@gr@bbed@l@bel{#2}}% This macro extracts the \label inside $$...$$
\providecommand{\env@equation@save@env}{}% To keep environ happy$$...$$
\providecommand{\env@equation@process}{}
\newcommand{\useeqlibrary}[1]{%
\RenewEnviron{equation}{% Redefine equation environment to capture it's body
\expandafter\eq@l@bel@gr@b\BODY\@nil% Extract \label
\expandafter\protected@xdef\csname equation@\eq@gr@bbed@l@bel\endcsname{\BODY}% Store equation in macro
}%
\input{#1}% Parse input file
}

\AtBeginDocument{% Restore original equation environment
\let\equation\oldequation% Restore $$\let\endequation\endoldequation% Restore$$
}

\newcommand{\geteqn}[1]{\csname equation@#1\endcsname}% Extract macro based on label
\makeatother

\begin{filecontents*}{equations.tex}
$$F = ma \label{newton2ndLaw}$$

$$\mathrm{det}(A - \lambda I) = 0 \label{eigValProb}$$
\end{filecontents*}

\useeqlibrary{equations.tex} %I just made up this command

\begin{document}

$$\geteqn{eigValProb} %made up this command also \label{anequationlabel}$$

See~(\ref{eigValProb}) or~(\ref{anequationlabel}).

\end{document}


As mentioned, this only works with the equation environment. Could possibly be extended to other environments in a similar manner. Moreover, it extracts only the first \label (if more than one exist).

• I will try this out right after my finals. Right now I am a bit short of time. – msmechanized Jan 10 '14 at 23:25