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:


F = ma

det(A-\lambda I)=0

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


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

Lorem Ipsum text

 \geteq{eigValProb} %made up this command also


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?


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.

enter image description here

\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 \begin{equation}
\let\endoldequation\endequation% Store \end{equation}

  \gdef\eq@gr@bbed@l@bel{#2}}% This macro extracts the \label inside \begin{equation}...\end{equation}
\providecommand{\env@equation@save@env}{}% To keep environ happy...
  \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 \begin{equation}
  \let\endequation\endoldequation% Restore \end{equation}

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

  F = ma \label{newton2ndLaw}

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

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


  \geteqn{eigValProb} %made up this command also

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


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

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.