[comp.text.tex] Gnuplot and LaTeX users

dfk@duke.cs.duke.edu (David F. Kotz) (03/05/91)

Here are some handy things for gnuplot users that use the latex driver.
These are also available for ftp from cs.duke.edu as
	   dist/sources/gnuplot/gnuplot-latex.shar 
This is an update to an older version of this stuff from last year.

These are some makefiles, shell scripts, and latex code that I use
with gnuplot to make latex documents with plots. You may tailor to
suit yourself. These are very short, really. I also do not plan to
really support them. I just found them handy. 

During revision of the document, when I am concentrating on the text,
I often tire of waiting for latex (and the printer, or previewer) to
process plots that I am going to ignore anyway. Or, when latex
requires multiple passes to get cross-references right, I hate
processing all those plots over and over. These macros (1) make it
easier to include plots and (2) make the initial or draft passes of
latex MUCH faster, by ignoring the plot contents.

#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create the files:
#	README
#	figs
#	plotcommand.tex
#	plotskel
# This archive created: Mon Mar  4 14:06:19 1991
# By:	David F. Kotz (dfk@cs.duke.edu)
# 	Duke University CS Dept.
export PATH; PATH=/bin:$PATH
if test -f 'README'
then
	echo shar: will not over-write existing file "'README'"
else
sed 's/^X//' << \SHAR_EOF > 'README'
XGnuplot-LaTeX handy things v2.0  March 1991
X
XHere are some handy things for gnuplot users that use the latex driver.
X
XThese are some makefiles, shell scripts, and latex code that I use
Xwith gnuplot to make latex documents with plots. You may tailor to
Xsuit yourself. These are very short, really. I also do not plan to
Xreally support them. I just found them handy. 
X
XDuring revision of the document, when I am concentrating on the text,
XI often tire of waiting for latex (and the printer, or previewer) to
Xprocess plots that I am going to ignore anyway. Or, when latex
Xrequires multiple passes to get cross-references right, I hate
Xprocessing all those plots over and over. These macros (1) make it
Xeasier to include plots and (2) make the initial or draft passes of
Xlatex MUCH faster, by ignoring the plot contents.
X
XTo effectively use this software, you must lay out your files as
Xfollows: make a subdirectory called "figs" in the current directory.
XPut the file Makefile.each, and pic.tex, in figs. Make a subdirectory
Xin figs for each plot.  Make a link to Makefile.each called "Makefile"
Xin each of the plot subdirectories. In each plot subdirectory, there
Xshould be a file called "plot", which is the gnuplot input. There
Xshould also be any data files needed by that plot. (If these have
Xstandard names, you may want to add them to the dependency list for
Xplot.tex and skel.tex in the makefile).
X
XThus, you should have:
X top:
X    main.tex plotcommand.tex figs/ ...
X figs:
X   Makefile.each makeall pic.tex one/ two/ three/
X one:
X   Makefile plot one.datafile
X two:
X   Makefile plot two.datafile
X three:
X   Makefile plot three.datafile
XNote here that all three Makefiles are linked to Makefile.each.
X
X
Xplotskel:
X  I find this shell script handy when I work on a large document that
Xcontains many plots. This shell script takes a gnuplot (or gnutex, for
Xthose with old gnutex files around) output file (in latex) and
Xconverts it to a minimum skeleton that has the same-size box. This
Xtakes latex no time at all to process, but fills the same place in the
Xdocument. So use the skeleton until you are ready to include the
Xplots, then use the real plot. The Makefile I supply automatically
Xcreates the skeleton skel.tex for the plot plot.tex. When latex is
Xrun, plotcommand.tex asks the user whether to include skeletons or
Xplots. This should be installed somewhere on your path; it is used by
Xthe Makefiles.
X
Xplotcommand.tex:
X  This defines some macros for conveniently including plots in your
Xdocument in a standard way. The files must be organized as described
Xabove. Input this at the top of your document before the
X\begin{document}. To include a plot, use 
X    \plot{plotname}{lof-caption}{caption}
XThe plotname is the name of the subdirectory of "figs" that contains
Xthe plot. Thus, if there is a directory called figs/foobar, then
Xfoobar is the plotname. The lof-caption is used only to generate a
Xlist of figures. If you don't do this, leave this empty. (You may cut
Xdown the \plot macro to remove this option). The caption for the
Xfigure should be specified. This command creates a \label called
X"f:plotname". 
X  To refer to the figure that has a given plotname, use
X\plotref{plotname}. This will generate text, eg "Figure 10". So you
Xcan say things like this:
X   \plotref{foo} shows this effect in gorey detail (see also \plotref{bar}). 
X  If you don't like the way it asks for input at each run, change the
X\typein to a \newcommand to define \plotskel to be either "skel" or
X"plot". But then this defeats the purpose of choosing plot or skel on
Xeach run. 
X  See the advanced usage, and "Misc figures" below.
X
XMakefile.each:
X  This is a makefile to help you automate the process of updating your
Xgnuplot runs. This should be linked to "Makefile" in each plot
Xsubdirectory. Just 'make' will make plot.tex and skel.tex, for
Xinclusion into your document. 'make dvi' will make a dvi file with
Xjust that plot (see pic.tex). 'make clean' cleans up dvi-making mess.
X'make spotless' reduces the directory to minimal size; ie this removes
Xplot.tex as well. 'make view' will make a dvi file and view it (you
Xcan change to your previewer). 'make print' will make a dvi file and
Xprint it. Note that making a dvi file often removes skel.tex; so you
Xshould 'make clean; make' after playing with dvi files.
X
Xmakeall:
X This script should be in figs/. It will run make in all
Xsubdirectories, with the given arguments passed to each make. Thus,
Xyou can 'makeall clean' or just 'makeall'.
X
Xpic.tex:
X This is a small wrapper for generating a dvi file with just one plot.
XIt does not draw the box like the \plot command (above) does. The
Xmakefile will use this. It should be accessable as ../pic.tex to all
XMakefiles.
X
XMiscellaneous figures:
X  If you have plots, then why not other figures? I have a similar
Xmacro that I use for normal everyday figures (eg, from "Fig"). This
Xworks just like \plot, except it is called \fig. There are \figref and
X\figrange, like \plotref and \plotrange. The \fig macro loads a .tex
Xfile by the given fig name from the figs/misc directory, and puts a
Xbox around it, like for the plots. This creates a label like
X"f:misc/figname". 
X
XAdvanced usage:
X If the document is really big, then you can divide the "figs"
Xdirectory into subdirectories. Put all your plots into one of the
Xsubdirectories of figs. Now, you might have:
X top:
X    main.tex plotcommand.tex figs/ ...
X figs:
X    chapA/ chapB/ 
X figs/chapA:
X    Makefile.each makeall pic.tex one/ two/ misc/
X figs/chapB:
X    Makefile.each makeall pic.tex one/ two/ misc/
X figs/chapA/one:
X   Makefile plot one.datafile
X figs/chapA/two:
X   Makefile plot two.datafile
X figs/chapA/misc:
X   foo.fig foo.tex
X figs/chapB/one:
X   Makefile plot one.datafile
X figs/chapB/two:
X   Makefile plot two.datafile
X figs/chapC/misc:
X   bar.fig bar.tex junk.fig junk.tex
XThen, in your document, you can use them like this:
X    \newplotdir{chapA}
X    \plot{one}{}{This is plot chapA/one}
X    \plot{two}{}{This is plot chapA/two}
X    \plotref{one} refers to chapA/one.
X    \fig{foo}{}{This is figure chapA/misc/foo}
X
X    \newplotdir{chapB}
X    \plot{one}{}{This is plot chapB/one}
X    \plot{two}{}{This is plot chapB/two}
X    \plotref{one} refers to chapB/one, NOT chapA/one. We have two
X	pictures, \figrange{bar}{junk}.
X    \fig{bar}{}{This is plot chapB/misc/bar}
X    \fig{junk}{}{This is plot chapB/misc/junk}
XTypically, I have one plotdir for each chapter or other major
Xsectional unit. This is a convenient way to avoid name clashes in plot
Xnames. For everyday usage, no \newplotdir is needed, and all plotdirs
Xshould be directly in figs/.
X
X
XDavid Kotz
Xdfk@cs.duke.edu
XMarch 1991
X
X
SHAR_EOF
fi # end of overwriting check
if test ! -d 'figs'
then
	mkdir 'figs'
fi
cd 'figs'
if test -f 'pic.tex'
then
	echo shar: will not over-write existing file "'pic.tex'"
else
sed 's/^X//' << \SHAR_EOF > 'pic.tex'
X\documentstyle{article}
X\pagestyle{empty}
X\begin{document}
X\sloppy
X\setlength{\textwidth}{8in}
X\setlength{\textheight}{9in}
X\setlength{\topmargin}{-0.5in}
X\setlength{\oddsidemargin}{0pt}
X\setlength{\evensidemargin}{0pt}
X\begin{center}
X\input {plot}
X\end{center}
X\end{document}
SHAR_EOF
fi # end of overwriting check
if test -f 'makeall'
then
	echo shar: will not over-write existing file "'makeall'"
else
sed 's/^X//' << \SHAR_EOF > 'makeall'
X#!/bin/csh -f
X# Make all figures
X# arguments are passed on to each make
X
Xif ($1 == clean || $1 == spotless) then
X	   foreach i (*/Makefile)
X	   	   if ($i:h == misc) continue
X	   	   echo $i:h
X	   	   (cd $i:h; make $*)
X	   end
X	   (cd misc; make clean)
Xelse
X	   foreach i (*/Makefile)
X	   	   echo $i:h
X	   	   (cd $i:h; make $*)
X	   end
Xendif
SHAR_EOF
chmod +x 'makeall'
fi # end of overwriting check
if test -f 'Makefile.each'
then
	echo shar: will not over-write existing file "'Makefile.each'"
else
sed 's/^X//' << \SHAR_EOF > 'Makefile.each'
X# This is linked into each plot directory as 'Makefile'
X
Xtex: plot.tex skel.tex
X
X# make a new tex plot file
Xplot.tex: plot
X	gnuplot plot
X	rm -f skel.tex
X
X# make a skeleton file for quicker draft latexing
Xskel.tex: plot.tex
X	plotskel plot.tex > skel.tex
X
X# or you view it
Xv view: pic.dvi
X	xtex pic
X
X# or you print it
Xp print: pic.dvi
X	texprint pic
X
Xremake:
X	rm -f plot.tex skel.tex
X
Xdvi: pic.dvi
X
Xpic.dvi: plot.tex
X	latex ../pic
X
Xclean:
X	rm -f *~ pic.dvi pic.aux pic.log
X
Xspotless: clean
X	rm -f plot.tex skel.tex
SHAR_EOF
fi # end of overwriting check
cd ..
if test -f 'plotcommand.tex'
then
	echo shar: will not over-write existing file "'plotcommand.tex'"
else
sed 's/^X//' << \SHAR_EOF > 'plotcommand.tex'
X% Some LaTeX macros for handling gnuplot plots
X% David Kotz dfk@cs.duke.edu
X
X% Find out whether user wants full plot or skeleton plots
X% User types either ``plot'' or ``skel'' (no quotes)
X\typein[\plotskel]{Do you wish full plots [plot] or skeleton plots [skel]?}
X
X% Define the current plot directory
X\newcommand{\plotdir}{}
X% Redefine the current plot directory
X\newcommand{\newplotdir}[1]{\renewcommand{\plotdir}{#1/}}
X
X\newlength{\plotwidth}
X\setlength{\plotwidth}{\textwidth}
X\addtolength{\plotwidth}{-0.1in}
X
X%%%%%%%%%%%% Plots %%%%%%%%%%%%%
X
X% Define \plot
X% Include a plot in the standard way
X%  Parm 1 is the plot name
X%  Parm 2 is the caption for the lof
X%  Parm 3 is the real caption
X\newcommand{\plot}[3]{
X \begin{figure}[htbp]
X  \framebox[\textwidth]{
X    \begin{minipage}{\plotwidth}
X      \begin{center}
X       \input{figs/\plotdir#1/\plotskel}
X      \end{center}
X      \caption[#2]{#3}
X      \label{f:\plotdir#1}
X    \end{minipage}
X  }
X \end{figure}  
X}
X
X% Reference one of the above plots (before or after it is input)
X\newcommand{\plotref}[1]{Figure~\ref{f:\plotdir#1}}
X
X% Reference a range of plots
X\newcommand{\plotrange}[2]{Figures~\ref{f:\plotdir#1}--\ref{f:\plotdir#2}}
X
X%%%%%%%%%%%% Misc figures %%%%%%%%%%%%%
X
X% Define \fig
X% Include a miscellaneous figure in the standard way
X%  Parm 1 is the fig name
X%  Parm 2 is the caption for the lof
X%  Parm 3 is the real caption
X\newcommand{\fig}[3]{
X \begin{figure}[htbp]
X  \framebox[\textwidth]{
X    \begin{minipage}{\plotwidth}
X      \begin{center}
X       \input{figs/\plotdir misc/#1}
X      \end{center}
X      \caption[#2]{#3}
X      \label{f:\plotdir misc/#1}
X    \end{minipage}
X  }
X \end{figure}  
X}
X
X% Reference one of the above figs (before or after it is input)
X\newcommand{\figref}[1]{Figure~\ref{f:\plotdir misc/#1}}
X
X% Reference a range of figs
X\newcommand{\figrange}[2]{%
XFigures~\ref{f:\plotdir misc/#1}--\ref{f:\plotdir misc/#2}}
SHAR_EOF
fi # end of overwriting check
if test -f 'plotskel'
then
	echo shar: will not over-write existing file "'plotskel'"
else
sed 's/^X//' << \SHAR_EOF > 'plotskel'
X#!/bin/csh -f 
X#
X# Make a skeleton version of a gnuplot or gnutex plot.tex file
X# This can be used in the same way, and takes up the same amount of
X# space on the page, but will run through latex MUCH faster. Good
X# for quick drafts.
X#
X# Usage:
X#   plotskel plot.tex 
X# Standard output is skeleton file
X
Xif ($#argv != 1) then
X	   echo usage: plotskel plot.tex
X	   exit 1
Xendif
X
Xsed -n -e '1,/begin{picture}/p' -e '/end{picture}/,$p' $1
SHAR_EOF
chmod +x 'plotskel'
fi # end of overwriting check
#	End of shell archive
exit 0
-- 
Department of Computer Science, Duke University, Durham, NC 27706 USA
ARPA:	dfk@cs.duke.edu
CSNET:	dfk@duke        
UUCP:	decvax!duke!dfk