# Individually importing a common set of equations into multiple unique documents

Suppose I have a large mathematical model with many equations and equation descriptions in many sections. This model, in various forms, is used in other documents.

For example, one document may just have the equations one-by-one, in raw form. Another might have an introduction, then the equations with descriptions after each, and then a discussion and conclusion. I may also have a beamer presentation with many of the key equations on various slides.

When I discover a problem with the model, or a better way to express an equation, I have to update every document. This is both time-consuming and highly error-prone.

Slowly, over time and after many revisions, each document starts to diverge.

I know LaTeX is a very advanced package with many useful commands. Is there any way to solve this dilemma? Perhaps store the equations and descriptions in a segmented text file and only import specific segments into each document?

-
you might be able to steal some code from tex.stackexchange.com/questions/57223/… –  David Carlisle Jun 18 '13 at 23:52
Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. –  Jubobs Jul 1 '14 at 20:42

How about creating a local package containing the equations?

For example, create “eqns.sty” in the same directory as your LaTeX documents, and create commands for your equations:

\ProvidesPackage{eqns}

\newcommand{\Einstein}{\ensuremath{E=mc^2}}
\newcommand{\DescrEinstein}{This is the most famous equation from Relativity.}

\newcommand{\Linear}{\ensuremath{y=mx+c}}
\newcommand{\DescLinear}{The general equation for a straight line.}

These commands can be used in both in-line and display maths environments:

\documentclass{article}
\usepackage{eqns}  % Use the equations
\begin{document}

The Einstein equation ($$\Einstein$$): \DescrEinstein

$$\Einstein$$

The straight-line equation ($$\Linear$$): \DescrLinear

\begin{displaymath}
\Linear
\end{displaymath}

The quadratic formula ($$\Quadratic$$): \DescrQuadratic

$$\Quadratic$$

\end{document}
-

So it seems like you have a general theme for a document that you want to typeset in a number of different ways. For this I would define a number of boolean variables/conditions and group your document content accordingly. That way you can make single changes to the document structure, yet the content will flow naturally from it.

Here's a quick example of your document structure:

\documentclass{<class>}
% <packages>
% <conditional definitions>
% <conditional declarations>
\begin{document}
% <condition 1>
% <document content>
% <end condition 1>
% <condition 2>
% <document content>
% <end condition 2>
% <more document content>
% ...
\end{document}

Here is a MWE implementing the above use case:

\documentclass{article}
% <packages>
\usepackage{lipsum}% http://ctan.org/pkg/lipsum

% <conditional definitions>
\newif\ifdocumenttext
\newif\ifequationdescriptions
\newif\ifequations

% <conditional declarations, default is all false>
\documenttexttrue% In-/exclude document text
\equationstrue% In-/exclude equations
\equationdescriptionstrue% In-/exclude equation descriptions
\begin{document}

\ifdocumenttext%
\section{Introduction}
This is my introduction
\fi%
\ifequations%
$f(x) = ax^2 + bx + c$
\ifequationdescriptions%
The above equation is a second-order polynomial in~$x$.
\fi%
\fi%
\ifequations%
$$g(x) = ax^3 + bx^2 + cx + d \label{eqn:poly}$$
\ifequationdescriptions%
In~(\ref{eqn:poly}), $g(x)$ has at most three roots.
\fi%
\fi%

\ifdocumenttext%
\section{Conclusion}
This is my conclusion
\fi%
\end{document}

The above MWE compiles to:

Commenting out \documenttexttrue (or using \documenttextfalse) compiles to

Additionally commenting out \equationdescriptionstrue (or using \equationdescriptionsfalse) compiles to

The general intent is similar to creating a full document will all the content in it, but selectively commenting out the parts you don't want in a global yet systematic way as part of your document content using conditionals. You can, of course, expand on the conditionals, include them in different ways, or even use packages that provide this functionality (like etoolbox). I've just shown a use case above to steer you in the right direction. You could even include conditionals to properly typeset the document under different classes, like beamer. Note that conditional nesting is also possible.

Further abstraction is possible by adding the content to separate files and including them in your document using \input{<filename>}. The definitions of the conditionals can also be made into a separate file that you either \input or as part of a local .sty that you use via \usepackage{<style file>}. However, at this point, I'm not sure whether this is something you're after.

Liberal use of % in the above example allows the document to compile correctly under the different conditional settings. See What is the use of percent signs (%) at the end of lines? for the meaning of its use.

-
This is useful and great, and I will probably end up using it one day, but unfortunately it doesn't really help very much with my original question. I have completely separate documents with very different scope and purpose. E.g. research paper with computational study; a set of slides for a conference; description paper for my supervisor; or just the model as a reference. Each of these are wildly different, with the only common factor being (some) of the (hundreds of) equations. –  Ozzah Jun 19 '13 at 1:17
This is what I envisioned: A text file with latex code. use some magic comment to delimit sections. %%%BEGIN[flow balance equations]\n$$a = b$$\n%%%END[flow balance equations] and then in my latex document \magicImport{model.txt}[flow balance equations]. That way, I have a central repository for my model, of which various parts can be used in many documents that are very different. If I update an equation or description, the changes are reflected everywhere. –  Ozzah Jun 19 '13 at 1:57
@Ozzah: This would require you to update the code in all the files that use the model. In that case, you can create a separate file for each of the ones you want to include via something like filecontents, all inside a separate modelequations.tex file. Once you compile modelequations.tex, it will produce all the equation files *.tex, which you then include in your relevant documents using \input{flow_balance_equations}. It's not as clean, but it would work. –  Werner Jun 19 '13 at 2:02