====== Makefile Gallery ======
This page contains a collection of Makefile rules and other snippets.
===== LaTeX to PDF or PS =====
In case you use John Collin's excellent [[http://www.phys.psu.edu/~collins/software/latexmk-jcc/|latexmk script]], the rule is extremely simple:
%.pdf : %.tex
latexmk -pdf $<
Similarly, to generate ''.ps'' files, use
%.ps : %.tex
latexmk -ps $<
The script handles things such as recessary re-runs of LaTeX and calling BiBTeX automatically and transparently.
In case you don't use latexmk, or if you want to make your Makefile usable for people who don't, the following, slightly more complex Makefile should do the job.
LATEX = latex
PDFLATEX = pdflatex
BIBTEX = bibtex
EGREP = egrep
EGFLAGS = "-c"
RERUN = "Label\(s\) may have changed"
RERUNBIB = "No file.*\.bbl|Citation.*undefined"
%.pdf: %.tex
$(PDFLATEX) $<
$(EGREP) $(EGFLAGS) $(RERUNBIB) $*.log && ($(BIBTEX) $*; $(PDFLATEX) $<); true
$(EGREP) $(RERUN) $*.log && $(PDFLATEX) $<; true
$(EGREP) $(RERUN) $*.log && $(PDFLATEX) $<; true
# Display relevant warnings, including the following line
egrep -i -A 1 "LaTeX.*Warning" $*.log ; true
To generate ''.ps'' files without latexmk, you will need a rule similar to the one above, that generates a ''.dvi'' file from a //.tex// file, and a second rule that uses ''dvips'' to generate the ''.ps'' file; however this is not provided here, at least for the moment.
===== MATLAB Figure to EPS =====
The following assumes that the MATLAB file is a script, not a function, and that it includes code to print a figure to an ''.eps'' file of the same name. (See also [[makematlabeps|this article]]).
%.eps : %.m
matlab -nodisplay -nojvm < $< > /dev/null
//Note:// In MATLAB version 7.4, it seems the script needs to contain an ''exit'' statement at the end, or MATLAB will not exit. In previous versions, MATLAB exits upon encountering the end of the file.
===== EPS to PDF =====
%.pdf : %.eps
eps2pdf $<
===== XFig to PDF/LaTeX combined =====
This requires the ''transfig'' package, in particular the ''fig2dev'' command. There are two rules involved. The first rule converts the ''.fig'' file (without text) to a ''.pdf'' file, and the second rule creates the file containing the corresponding LaTeX code.
%.pdf : %.fig
fig2dev -L pdftex $< $@
%_t : %.fig
echo -e "\\\\begin{picture}(0,0)%\n\\\\epsfig{file=$(patsubst %.fig,%,$<)}%\n\\\\end{picture}%"\
> $@
fig2dev -L pdftex_t $< >> $@
The line starting with ''echo'' makes sure that all you need to do in the LaTeX file is type
\input{file_t}
(If you had exported the figure manually from ''xfig'', the above code (''\begin{picture}...'') would have been automatically included in the ''_t'' file. I don't know why ''fig2dev'' doesn't do this automatically.)
//Note: // It can happen that, depending on the XFig page settings, the figure appears rotated by 90 degrees. In that case, you need to use the following line to generate the ''.pdf'' file from the ''.fig'' file:
fig2dev -L pdftex -p1 $< $@
===== XFig to PS/LaTeX combined =====
This is almost equal to the previous case, except that you have to slightly change the parameters given to ''fig2dev'', and you need to replace ''pdf'' by ''ps'' in the filenames.
There are two rules involved. The first rule converts the ''.fig'' file (without text) to a ''.pdf'' file, and the second rule creates the file containing the corresponding LaTeX code.
%.ps : %.fig
fig2dev -L pstex $< $@
%_t : %.fig
echo -e "\\\\begin{picture}(0,0)%\n\\\\epsfig{file=$(patsubst %.fig,%,$<)}%\n\\\\end{picture}%"\
> $@
fig2dev -L pstex_t $< >> $@
The line starting with ''echo'' makes sure that all you need to do in the LaTeX file is type
\input{file_t}
(If you had exported the figure manually from ''xfig'', the above code (''\begin{picture}...'') would have been automatically included in the ''_t'' file. I don't know why ''fig2dev'' doesn't do this automatically.)
//Note: // You can combine these rules, so that they work transparently with both ''latex'' and ''pdflatex'', to create ''.ps'' and ''.pdf'' files, respectively.