Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
2  
@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
3  
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
2  
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
3  
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
1  
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

6 Answers 6

up vote 20 down vote accepted

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:

Sample output

\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}{\begin{equation} f(x) = \int_a^x g(t)\,dt \label{eq:int}\end{equation}}
\newcommand{\myeqintl}[1]{\begin{equation} f(x) = \int_a^x g(t)\,dt \label{#1}\end{equation}}
%%%%%%%%%%%%%%%%%%%

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

share|improve this answer
    
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

Your comments indicate that you'd like to have some kind of database of equations. An easy approach would be to have a separate file equations.tex, say, which defines two macros

\saveequation{<ID>}{<equation code>}
\useequation{<ID>}

and also contains the equations defined with \saveequation and maybe also calls some often needed related packages. Adapting Harish's example this could look as follows:

\documentclass{article}

% this is the separate file for the sake of this example
% created with {filecontents}:
\usepackage{filecontents}
\begin{filecontents}{equations.tex}
\makeatletter
% \saveequation{<ID>}{<equation>}
\newcommand\saveequation[2]{%
  \@namedef{equation@#1}{#2}%
}

% \useequation{<ID>}
\newcommand\useequation[1]{%
  \@nameuse{equation@#1}%
}
\makeatother
\RequirePackage{amsmath,bm}
\saveequation{massenergy}{E = mc^{2}}
\saveequation{curlE}{\nabla \times \bm{E} = 0}
\end{filecontents}

% input the file:    
\input{equations}

\begin{document}
% usage:
This is Einstein's relation: $\useequation{massenergy}$.
This is the curl of electric field:
\begin{equation}
 \useequation{curlE}\label{eq:curlE}
\end{equation}
and without number:
\[
 \useequation{curlE}
\]

\end{document}
share|improve this answer

You could also use the catchfilebetweentags 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>
\begin{equation}
{x = \frac{ { - b \pm \sqrt {b^2 - 4ac} } }{2a}}
\end{equation}
%</eq01>

Then in you main file you do something like this:

\documentclass[11pt]{article}

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

\newcommand{\loadeq}[1]{%
    \ExecuteMetaData[equations.tex]{eq#1}%
}

% Begin document
\begin{document}

Look at equation eq01!
\loadeq{01}

\end{document}

And vola! The Quadratic equation

Example derived from the aforementioned blog post.

share|improve this answer

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

Note that we have not used any math delimiters ($ or \begin{equation} or \[...\]) in these files so that they can be used inside variety of math environments. Now we have two .tex files: viz; massenergy.tex: and curlE.tex. For the time being, I assume that they are stored in the same folder as our main .tex file (you can store them elswhere and call them by providing the correct path).

Now we use them like this:

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents}{massenergy.tex}
  E = mc^{2}
\end{filecontents}
\begin{filecontents}{curlE.tex}
  \nabla \times \bm{E} = 0
\end{filecontents}

\usepackage{amsmath,bm}
\begin{document}
This is Einstein's relation : $\input{massenergy}$.
This is the curl of electric field:
\begin{equation}
\input{curlE}
\end{equation}
and without number:
\[
\input{curlE}
\]


\end{document}

I have used filecontents so that one need not create the equation files separately while testing this example.

share|improve this answer
3  
Of course the overhead on I/O operations will be noticeable when several formulas need to be input this way. –  egreg Dec 15 '12 at 11:48
2  
@egreg: I won't be doing this any way. Imagine one having many equations, then database management becomes headache :-) –  Harish Kumar Dec 15 '12 at 11:50
    
It will be funnier if you decompose each equation into several files, each just contains a single character. –  cyanide-based food Jan 22 at 15:34

If you wanted to, you could create a separate file for each formula, and then include each formula using \input{filename}. For example, if you have a file called euler.tex that is just

$e^{i \pi} + 1 = 0$

you would then just use $\input{euler}$. But this is a really bad idea. Use \newcommands. Or better yet, \newcommand your common subexpressions, but build the actual equations from those in each instance.

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

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.