%====================================================================
% This is a LaTeX-2e document
%====================================================================

\documentclass[12pt]{article}
\parindent=0pt \parskip=8pt

\usepackage{verbatim}
\usepackage{fullpage}

\usepackage{amssymb,amsmath,amsthm}
\usepackage[mathscr]{eucal}

% Files in this directory:
\input{isomath}
\input{mathenv}
\input{syms}

\usepackage{graphicx}
\usepackage{psfrag}

\usepackage{hyperref}
\usepackage{ifpdf}

\ifpdf
\pdfinfo{
/Author (John Kerl)
/Title  (Graphics examples in LaTeX)
}
\fi

%--------------------------------------------------------------------
\begin{document}
\title{Graphics examples in \LaTeX}
\author{John Kerl $\bullet$ \texttt{http://johnkerl.org}}
\date{\today}

\maketitle
\tableofcontents

%% ================================================================
\section{Creating figures}

For plots, I sometimes use Matlab.  (See figure \ref{fig:matlab}.)  Go into
Matlab, do your plot, set up axis labels as you wish, etc.  Then save as EPS
format.

% * The scale parameter can be used to resize the EPS file within the .tex
%   file.  And/or, you may save a larger/smaller EPS file in the first place.
%   (These two choices affect figure text differently.)
%
% * Replace figures/matlab_figure.eps with your file name.
%
% * Replace fig:matlab with any name you want.  This is just a label for
%   internal cross-referencing:  mark a figure, table, equation, section,
%   chapter, etc. with \label{xyz}.  Then refer to figure \ref{xyz}, table
%   \ref{xyz}, equation \eqref{xyz}, section \eqref{xyz}, chapter \eqref{xyz},
%   etc. to get a cross-reference.
%
\begin{figure}[!htb]
\begin{center}
\psfragscanon
\includegraphics[scale=0.95]{figures/matlab_figure.eps}
\caption[Shorter caption for the list-of-figures page (if you have one).]
    {Caption for Matlab figure goes here.
	\label{fig:matlab}}
\end{center}
\end{figure}

More often, for plots I use Python's \texttt{pylab} module, which has
Matlab-like plotting functionality.  I also use my \texttt{pgr} script, which
is a handy wrapper around \texttt{pylab}:  it is a script which reads tabular
file data and command-line arguments, then calls the desired \texttt{pylab}
functions.  Please see \texttt{http://johnkerl.org/python/doc/pgr.html} for
more information about \texttt{pgr}.  See figure \ref{fig:pgr} for a plot.

\begin{figure}[!htb]
\begin{center}
\psfragscanon
\includegraphics[scale=0.8]{figures/tauint_eta_0.9.eps}
\caption[Shorter caption for the list-of-figures page (if you have one).]
	{Caption for \texttt{pylab} plot goes here.
	\label{fig:pgr}}
\end{center}
\end{figure}

For drawings, I use Inkscape:  figure \ref{fig:inkfigs}.  The difference
between the two sides of the figure (i.e. whether or not you see {\TeX} fonts
in the figure or not) will be discussed in section \ref{sec:psfrag}.

\begin{figure}[!htb]
\begin{center}
\psfragscanon
\includegraphics{figures/inkfig.eps}
\includegraphics{figures/inkfigtex.eps}
\caption{Caption goes here.
	\label{fig:inkfigs}}
\end{center}
\end{figure}

%% ================================================================
\section{Including figures in your document}

First, in the preamble (i.e.  above
\texttt{{\textbackslash}begin\{document\}}), put the following:

\begin{verbatim}
\usepackage{graphicx}
\usepackage{psfrag}
\end{verbatim}

Then, put the following in the body of your document:
\begin{verbatim}
\begin{figure}[!htb]
\begin{center}
\psfragscanon
\includegraphics[scale=0.8]{figures/tauint_eta_0.9.eps}
\caption[Shorter caption for the list-of-figures page (if you have one).]
    {Caption for \texttt{pylab} plot goes here.
    \label{fig:pgr}}
\end{center}
\end{figure}
\end{verbatim}

%% ================================================================
\section{Putting mathematical text in figures}
\label{sec:psfrag}

There are (at least) two options:  (1) use the plotting/drawing program's fonts
for the figure; (2) use {\TeX} fonts for the figure.

For Matlab:  If you type in things like \texttt{{\textbackslash}theta}, Matlab
will convert it to a symbol for you when it creates the EPS file.  See also
\texttt{help text} in Matlab.

For \texttt{pylab}:  here also, most everything like
\texttt{\${\textbackslash}theta\$} will be converted to a symbol for you when
\texttt{pylab} creates the EPS file.  Make sure to include dollar signs around
any {\TeX}-style expressions.

For Inkscape, or any drawing program which doesn't convert {\TeX}-style symbols
for you, you can use the program's own fonts for the figure.  In my experience,
drawing programs usually have very limited (or awkward-to-use) support for
mathematical symbols.  To get {\TeX} fonts for the figure, you can do the
following.  Instead of trying to get Inkscape to create a $\theta$ symbol, just
put the text
\begin{verbatim}
\tex{$\theta$}
\end{verbatim}
into a text box in Inkscape.  Then save your SVG file.  Then save as EPS also,
but be sure to uncheck the \emph{Convert text to paths} option.  Here's where
the {\LaTeX} package \texttt{psfrag} comes in.  When you run your \texttt{.tex}
file through the \texttt{latex} command, it interprets the symbols in your
figure's EPS file.  Voil\`a --- now the symbols in your figure match the
symbols in the body of your paper.

The disadvantages of using \texttt{psfrag} are (1) The DVI previewer
\texttt{xdvi} won't interpret the \texttt{{\textbackslash}tex\{\$\ldots\$\}}
strings correctly; instead, you can view the PDF version of your document to
see the strings.  (2) You no longer have WYSIWYG (what you see is what you get)
inside the drawing program.  Instead, you may need to repeatedly iterate:  save
the figure file as SVG, save as EPS, close the PDF viewer, build the PDF file
for your paper, open the PDF viewer and look at the figure; then go back
into the drawing program and moves the math symbols around \ldots .

To summarize:
\begin{itemize}

\item In the drawing program, use strings of the form
\texttt{{\textbackslash}tex\{\$\ldots\$\}}.  Don't bold these strings in the
drawing program, change the font size, etc.

\item In the drawing program, save the figure as EPS.

\item In your {\LaTeX} file, have
\begin{verbatim}
\usepackage{psfrag}
\end{verbatim}
in the preamble.  Also have the \texttt{psfragscanon} line for each figure, as
shown:
\begin{verbatim}
\begin{figure}[!htb]
\begin{center}
\psfragscanon
\includegraphics[scale=0.8]{figures/tauint_eta_0.9.eps}
\caption[Shorter caption for the list-of-figures page.]
    {Caption for \texttt{pylab} plot goes here.
    \label{fig:pgr}}
\end{center}
\end{figure}
\end{verbatim}

\item Generate a PDF file from your {\LaTeX} file as shown next.

\end{itemize}

%% ================================================================
\section{Generating DVI and PDF files}

This file (and the directory containing it) serve as a copyable example.
Files:

\begin{itemize}

\item \texttt{grex.tex}: The \LaTeX source for what you're reading now.

\item \texttt{isomath.tex}, \texttt{mathenv.tex}, \texttt{syms.tex}: misc.
environment and symbol definitions.

\item \texttt{figures/inkfig.svg}:  An Inkscape file, in SVG (scalable vector
graphics) format.  Just run Inkscape (\texttt{inkscape \&} at the Unix prompt
on the math department Linux machines) and draw.  Inkscape has nice on-line
help.  Then save.

\item \texttt{figures/inkfig.eps}:  In Inkscape, simply do save as EPS.  Then
accept the defaults when prompted: \emph{Make bounding box around full page} =
false, \emph{Convert texts to paths} = false, \emph{Embed fonts (Type 1 only)}
= false.

\item \texttt{figures/matlab\_figure.m}:  A Matlab script file.  Edit this file
to see how it works; consult Matlab documentation for more information.  Run
Matlab (\texttt{matlab \&} or \texttt{matlab -nodisplay} on the math department
Linux machines), then type the name of the file without the \texttt{.m}.  Note
that the file \texttt{figures/matlab\_figure.m} contains the name
\texttt{matlab\_figure.eps} inside it, so if you rename the \texttt{.m} file,
also don't forget to edit and change the last line.

\item \texttt{figures/tauint\_eta\_0.9.txt} and
\texttt{figures/mktauintplot.sh}:   The former is a data file.  The latter is a
shell script which invokes \texttt{pgr} with the command-line options necessary
to produce figure \ref{fig:pgr}.

\item \texttt{dbuild}:  A shell script to make a DVI file.  For this file, it
simply contains the line \texttt{latex grex.tex}.

\item \texttt{pbuild}:  A shell script to make the PDF file you're reading.  I
use \texttt{latex}, \texttt{dvips}, and \texttt{ps2pdf} to create the sequence
\texttt{.tex} $\to$ \texttt{.dvi} $\to$ \texttt{.ps} $\to$ \texttt{.pdf}.  You
can do this all in one step using \texttt{pdflatex}, but \texttt{pdflatex}
won't read EPS files.  So, my \texttt{pbuild} script contains the following
commands:
\begin{verbatim}
dbuild
dvips -o grex.ps grex.dvi
ps2pdf grex.ps
rm grex.ps
\end{verbatim}

\end{itemize}

\end{document}
