# Is it possible to write all mathematical formulas in a separate file and add them in main file on-demand?

Consider this scenario: I have a report to write and I want to write all mathematical formulas in a separate file and then add them in the main report file on demand (i.e. include/import/add/call a certain formula in specific part of my report).

Please note that I'm not talking about referencing or citation (\ref or \cir) to that formula. What I want, is to have the formula itself in the generated pdf. Somehow like a function call in programming languages!

Is this possible?

-
@mafp Sure :) For two purposes mainly. First, sometimes I prefer to rewrite a same formula again, instead of referencing to that. I found it more convenient for the reader to see the formula again instead of finding it somewhere else with reference number. This is useful specifically when the paper is going to be read on paper (i.e. no hyper-references). Needless to say, this is doable only for unofficial reports. – Pouya Dec 15 '12 at 10:44
Secondly, there are many formulas that I have to use them again and again in different reports (You know what I mean if you are a master or phd student!). Instead, if I achieve what I asked here, I can start to make a database of necessary formulas and after a while, all I need to do is to add this file to my report and simply call a certain formula on demand (instead of rewriting it). – Pouya Dec 15 '12 at 10:48
Storing equations in external files might be an option, and it certainly has its advantages, but maybe this can also solve your problem (as you can see from reading it, the same problem seems to pop up every now and then... :)): tex.stackexchange.com/q/75831 Hope this helps! – Count Zero Dec 15 '12 at 11:03
Function calls in TeX can only be emulated by macros, so you'd have to define a macro for each formula. What this macro is supposed to expand to is your choice: either you read a whole block of formulas, or each macro points to a small file that's \input. – egreg Dec 15 '12 at 11:17
One might think to a two pass process; \formula{name} writes something in the .aux file and then a postprocessor fetches the needed entries from a database, building a .tex file to be \input, where for each name a macro can be defined. – egreg Dec 15 '12 at 11:56
show 2 more comments

You can place (almost) arbitrary LaTeX code in macros for later use. So you can use these to store your equations. Here is a simple example showing some possibilities:

\documentclass{article}

%% Can be in external file which can be inserted with \input
\newcommand{\myeqintf}{f(x) = \int_a^x g(t)\,dt}
\newcommand{\myeqintt}{\begin{displaymath} f(x) = \int_a^x g(t)\,dt \end{displaymath}}
\newcommand{\myeqint}{$$f(x) = \int_a^x g(t)\,dt \label{eq:int}$$}
\newcommand{\myeqintl}[1]{$$f(x) = \int_a^x g(t)\,dt \label{#1}$$}
%%%%%%%%%%%%%%%%%%%

\begin{document}
An equation $$\myeqintf$$ inline.  The same equation displayed
\begin{displaymath}
\myeqintf .
\end{displaymath}
A stored display.
\myeqintt
A stored display with fixed label
\myeqint
don't reuse!!  A stored display with specified label
\myeqintl{eq:intl}

\end{document}


As you see there are a number of issues you will have to think about. Is it just the body of the formula that should be saved or a whole displayed environment? Should equation numbers be included? What about punctuation? (The breqn package could help with the latter issue.) Also this will make it hard to tweak the formulae for use in different situations.

-
 Thank you very much. In fact, when I was posting the question, I was thinking what would happen to 1-equation number and 2-references to those equations and formulas. Allow me study your answer and wait for other suggestions. – Pouya Dec 15 '12 at 11:53

I heard that equations will be stored in separate files -- each equation in a separate file and you want to build a database of these files!: This is possible.

You can save an equation as a tex file, for example massenergy.tex:

E = mc^{2}


and curl of electric field to be saved as curlE.tex

\nabla \times \bm{E} = 0



But actually, the above approach can be useful if you are generating your equations programmatically (e.g., as output from some computer algebra system).

-

You could also use the catchﬁlebetweentags package. This package enables you to use a separate file to store you equations and then refer to them using tags that you specify.

This blog post, Loading equations from an external file, explains this quite well but for the lazy I'll include an example:

First you put your equations in a .tex file, delimiting them using tags (<*eq01>):

%<*eq01>
$${x = \frac{ { - b \pm \sqrt {b^2 - 4ac} } }{2a}}$$
%</eq01>


Then in you main file you do something like this:

\documentclass[11pt]{article}

\usepackage{amssymb,amsmath}
\usepackage{catchfilebetweentags}

}

% Begin document
\begin{document}

Look at equation eq01!

\end{document}


And vola!

Example derived from the aforementioned blog post.

-

Once having such an external file, you most likely will have a problem to collaborate and share your sources - you will always have to include in the collaboration the equations file (which might be located somewhere else in your directory tree). In other words you will have a problem of non-standard code. In this blog post I offered a solution to this issue in the form of using code snippet manager. This way, you could set a file called my_equations.yasnippet which will hold all your equations, and instead of typing them by hand you could simply hit eq_foo_bar TAB and this will be expanded to the right equation.

There is one downside to this approach: If you want to change something in eq_foo_bar you could only do it for future documents. Naturally, a change in the snippets database file will not effect existing files.

-