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

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.


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}

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.