\documentclass[10pt]{article}
\usepackage{rotating,graphics,psfrag,epsfig}
\oddsidemargin=-0.25in
\topmargin= -0.5in
\textwidth=7.0in
\textheight=9in
\pagestyle{empty}
\sloppy

\begin{document}

\begin{center}
\begin{Large}
Incorporating \LaTeX $\;$ in your Postscript files
\end{Large}
\end{center}

Programs which generate Postscript (ps) or Encapsulated Postscript (eps)
output, such as MATLAB or xfig, are
somewhat limited in their ability to produce mathematical expressions.
For example, the current version of MATLAB has no easy provision for
combining regular text and Greek symbols, equations, subscripts, etc.,
for axes labels or for plot legends. Although xfig allows you to
create these marvels by hand, it is more efficient to incorporate
\LaTeX $\;$ typesetting commands into your figure.  The {\em psfrag}
macro allows any string in a {\em ps} or {\em eps} figure to be
replaced with \LaTeX $\;$ generated text.  This handout shows you how
to do it.  An example using a MATLAB plot is given, but you can use the
same tricks with any eps file.

\subsection*{Generating the MATLAB plot}

You probably know how to generate a MATLAB plot and store it using
the {\tt print} command.  To get axes labels on the plot
using  \LaTeX, you have to label the axes slightly differently.
For example, if you want the X--axis label as ``Fancy X--label:
$\int_{-\infty}^{\infty} f(t)\,dt$", type the
following (in MATLAB):
\begin{verbatim}
>> xlabel('xl')
\end{verbatim}
Now we have to replace {\tt xl} with ``Fancy X--label:
$\int_{-\infty}^{\infty} f(t)\,dt$".  This is done using the
\verb+\psfrag+ command.  This command is to be used in the \LaTeX
$\;$ source file.  Its syntax is: \\[0.05in]
\verb+ \psfrag{name}[position][psposition][scale][rot]{LaTeX text}+\\[0.05in]
As an
example, consider
\verb+\psfrag{xl}[c]{Fancy X--label: $\int_{-\infty}^{\infty} f(t)\,dt$}+,
which will be a line in your \LaTeX $\;$ source file. 
This will replace every occurrence of \texttt{xl} that appears in the
MATLAB plot with ``Fancy X--label: $\int_{-\infty}^{\infty}
f(t)\,dt$".  In a similar vein, if you want to label a graph $A_2(n)$,
then place a string \texttt{p1} in a suitable location inside the plot
(using either MATLAB's \texttt{text} or \texttt{gtext} command).  Once
the sequence of commands given below are executed, the final
PostScript file should contain the plot with the legend $A_2(n)$.  In
the \LaTeX $\;$ command
\verb+\psfrag{p1}[c]{$A_2(n)$}+, the option \texttt{[c]} will center the
replaced string around \texttt{p1}.  If you want the replacement string to
be right-justified, then the option should be \texttt{[r]}.  (No prizes for
guessing what the option is for left-justifying the text.)

Figure~1 shows the MATLAB plot as it would appear before
replacing the strings.  (These strings will later be
replaced with \LaTeX $\;$ text.)
\begin{figure}[h!t]
\centering
\resizebox{3.0in}{!}{\includegraphics{figure.eps}}
\caption{MATLAB plot as it appears on your screen.}
\end{figure}
The first thing to do is to save the figure as an {\tt eps} file.  This is
done using the MATLAB command
\begin{verbatim}
	>> print -deps2 figure.eps
\end{verbatim}
This will generate the file {\tt figure.eps}.  Next, use the command
(outside MATLAB, of course) {\tt ps2frag} on the file just created:
\begin{verbatim}
	ps2frag figure.eps
\end{verbatim}

\subsection*{Additions needed in the \LaTeX $\;$ source file}
The \LaTeX $\;$ source file has to be modified slightly before you can
accomplish string replacement on {\tt .eps} files.  The package
\texttt{psfrag} has to be included using the
\texttt{usepackage} command: \verb+\usepackage{psfrag}+.

The following is a portion of a \LaTeX $\;$ file \texttt{example.tex}.
The \verb+\psfrag+ commands between the \verb+\begin{figure}+ and
\verb+\end{figure}+ are the ones that take care of string replacement.
The \verb+\includegraphics+ is needed for including the
EPS file \texttt{figure.eps} into the \LaTeX $\;$
document.

\begin{verbatim}
\documentclass[11pt]{article}
\usepackage{rotating,graphics,psfrag,epsfig}
..........
\begin{figure}[h!t]
  \centering
  \psfrag{xl}[c]{\footnotesize Fancy X--label: $\int_{-\infty}^{\infty} f(t)\,dt$}
  \psfrag{yl}[c]{\footnotesize Fancy Y--label: $\sum_{n=-\infty}^{\infty} f[n]$}
  \psfrag{p1}[c]{\begin{turn}{75} \footnotesize Dangerously Leaning Tower of
      $\pi\iota\sigma\alpha$ \end{turn}}
  \resizebox{3.0in}{!}{\includegraphics{figure.eps}}
\caption{An example of string replacement using \texttt{psfrag}.}
\end{figure}
\end{verbatim}

The following commands are needed to
obtain the final output: (1) \texttt{latex example.tex}
(2) \texttt{dvips example.dvi}. The PostScript file
\texttt{example.ps} will contain the \LaTeX $\;$ output and figures  
in the final form.

To summarize the steps required to iuse psfrags: 
\begin{enumerate}
\item Store the MATLAB plot using the 
\texttt{print -deps2 filename.eps}
command.  
\item Use the \texttt{ps2frag} command on the EPS file.  
\item \LaTeX $\;$ your source file; the source file should contain string
replacements using the 
\verb+\begin{psfrag}+ commands. 
\item Run 
\texttt{dvips} to get the postscript file. 
\item Preview the file with 
\texttt{ghostview}
to see if the string replacements have been done properly, before
printing the document.
\end{enumerate}

Figure 2 is a sample output in which the strings have been
replaced with the desired \LaTeX $\;$ text.

%\section*{Example}
\begin{figure}[h!t]
    \centering
    \psfrag{xl}[c]{\footnotesize Fancy X--label: 
	$\int_{-\infty}^{\infty} f(t)\,dt$}
    \psfrag{yl}[c]{\footnotesize Fancy Y--label: 
	$\sum_{n=-\infty}^{\infty} f[n]$}
    \psfrag{p1}[c]{\begin{turn}{75} \footnotesize Dangerously Leaning Tower of
     	$\pi\iota\sigma\alpha$ \end{turn}}
    \resizebox{3.0in}{!}{\includegraphics{figure.eps}}
\caption{An example of string replacement using \texttt{psfrag}.}
\end{figure}


\end{document}
