Makefiles with LaTeX.

With large document, it can become difficult to maintain all of the required generated files. This is particularly true if combined LaTeX/pdfTeX compilation is supported (see above section), as in this case both .eps and .mps files may be required. Automatic generation of the necessary figure files is very useful, particulary if you use CVS (concurrent versioning system - it's good: try it) to track document changes.

An Example Makefile

Although normally used for software compilation, "make" is well suited for dependency checking and automatice file generation. The following is the makefile I have used for my thesis:

.PRECIOUS:%.mps
%.eps: %.sk
        sk2ps $< $@
%.mp: %.eps
        pstoedit -f mpost -fontmap /your_path/fontmap.mpost $< $@
%.1: %.mp
        cd $(@D); mpost $(<F)
%.mps: %.1
        mv $< $@
%.eps: %.fig
        fig2dev -L ps $< $@
%.pdf: %.tex
        pdflatex $<
%.dvi: %.tex
        latex $<
%.ps: %.dvi
        dvips $< -o $@
%.dep: 
        ./texdep $* > $*.depend
include *.depend

The first rule is for generating the eps files from the Sketch source. I tend to use Sketch for my illustrations as I prefer its interface to that of xfig. For now, this makefile has to be bootstrapped by creating a dummy file in the directory with extension .depend. It seems hard to make the include directive handle the case of no files. Suggestions on improving this are welcome.

Dependency Calculation

The program "texdep" in the above program is a perl script to detemine the names of the graphics files required by the document. It performs a similar role to the -g switch to gcc, writing out the files in the correct form for inclusion into a makefile

This script is really quite poor, as it was one of my first experiences with perl. In particular, it is a bit pedantic on how the graphics inclusions appear, but it does work for the way I do it ;-). It does, however, search though multi-level documents, observing any \includeonly statement.

#!/usr/bin/perl
#----------------------------------------------------------------------
# A perl script to determine makefile dependencies for LaTeX documents.
# The target name determines the type of extension to add to graphics
# files.  If it is .pdf, the default extension is .pdf, otherwise it
# is .eps.
#
# http://www.2pi.info/
#----------------------------------------------------------------------
sub FixExt {$name=$_[0];
    return $name if ($name=~/\.[me]ps/);
    return $name.".pdf" if $_[1]=~/\.pdf$/;
    return $name.".eps";
}\par
die "Usage:\n  $0 target\n" if $#ARGV<0;
print $ARGV[0].": ";
$ARGV[0]=~/^([^.]*)/;
open(SRC, $1.".tex");
while (<SRC>) {
    if (/^[^%]*\\includeonly\{([^\}]*)\}/) {
        @files=split(/,/,$1);
        $HaveOnly=1;
    };
    last if /^[^%]*\\begin/;
};
while (<SRC>) {
    if ((!$HaveOnly) $amp;$amp; /^[^%]*\\include\{([^\}]*)\}/) {
        @files=(@files,$1);
    };
    if (/^[^%]*\\includegraphics\{([^\}]*)\}/) {
        print FixExt($1,$ARGV[0])." ";
    };
}
foreach my $file (@files) {
    $file=$file.".tex" if !($file=~/(.sty$|.tex$ )/);
    print "\\\n  ".$file ." ";
    open(SRC, $file);
    while (<SRC>) {
        if (/^[^%]*\\includegraphics\{([^\}]*)\}/) {
            print FixExt($1,$ARGV[0])." ";
        };
    }
}