4

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

\begin{equation}
F = ma
\label{newton2ndLaw}
\end{equation}

\begin{equation}
det(A-\lambda I)=0
\label{eigValProb}
\end{equation}

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

\begin{equation}
 \geteq{eigValProb} %made up this command also
 \label{anequationlabel}
\end{equation}

\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?

1

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

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

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

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

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

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

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

\begin{document}

\begin{equation}
  \geteqn{eigValProb} %made up this command also
  \label{anequationlabel}
\end{equation}

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

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.