# # LaTeX.Rules --- Makefile fragment for the automation of LaTeX document # processing # # Copyright (C) 2005-2011 Kipp C. Cannon # # This program is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the # Free Software Foundation; either version 2 of the License, or (at your # option) any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General # Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # # Example Makefile for a LaTeX file called "article.tex": # # === begin Makefile === # # LATEX := pdflatex # DOCUMENT := article # # include LaTeX.Rules # # === end Makefile === # # Then run # $ make [target] # # The following targets are understood: # dvi generate dvi output (latex default target) # pdf generate pdf output (pdflatex default target) # ps generate postscript # mostlyclean erase all intermediate files # clean erase all but the source files # # To do: # -recursively process \include{} and \input{} commands for .tex # dependancies # -do the same for \bibliography{} commands # -add metapost processing # # # Document information (these are variables you can set in the Makefile # in which this file is being included) # # LATEX # The name of the latex compiler to use. If the program name is, # specifically, "pdflatex" then the .pdf will be the default target # rather than the .dvi. Note that at present there is no way to ask # for one or the other without switching the contents of this # variable. # # DOCUMENT # (required) The root name for all files. # # GRAPHIC_FILES # Any graphic files the document depends on, for example any .eps or # .pdf files included with \includegraphics{}. The output is rebuilt # if any of these files change but otherwise they play no role in the # build process. # # TEXT_FILES # Any text files the document depends on, for example any files that get # included via \verbatiminput{} commands. The output is rebuilt if any # of these files change, but otherwise they play no role in the build # process. # # MPOST_FILES # Any MetaPost .mp files the document depends on. NOTE: do *not* # list .mp files generated by Embedded MetaPost. # # XFIG_FILES # Thes Makefile rules allow your LaTeX document to include XFig .fig # files directly. Each .fig file is converted to a graphic format # suitable for inclusion by the LaTeX processor, for example # postscript or pdf. This is accomplished by processing the .fig # file with fig2dev and producing two output files: a graphics # version of the .fig file and a LaTeX source file containing the # commands needed to import that graphics file. In your own LaTeX # document, you will need to include this .tex source file. For # example, if you have an XFig file called diagram.fig, you should # set "XFIG_FILES = diagram.fig" in your Makefile and place the # command "\include{diagram.fig}" in your LaTeX document where you # would like the image placed. When your document is processed, the # XFig file will be converted to a, for example, postscript file # called diagram.fig.ps and a LaTeX source file called # diagram.fig.tex. The \include{} command in your LaTeX source reads # in the .fig.tex file (the .tex extension is assumed by the command) # which in turn contains the commands to import the graphics file. # # As an added bonus, any text objects in the XFig document that have # their "special" flag set will be removed from the document before # it is converted to a graphics format. Their contents are added to # the LaTeX source file along with the commands needed to overlay # them in the correct places on the figure. In this way, you can use # LaTeX to typset the text of your XFig figure. This has the # advantage of having the text rendered in the same font as the rest # of your document and also allows you to make use of all of LaTeX's # typsetting facilities. Note that in your XFig document you should # set the pen colour of the "special" text to "Default" in order to # prevent \color commands from being issued, otherwise you will need # to include the LaTeX package "color". # # If you get error messages about "unknown graphics format" related # to the .fig.ps or .fig.pdf intermediate graphics file then you must # issue the \DeclareGraphicsRule{.fig.ps}{eps}{.fig.ps}{} command or # the \DeclareGraphicsRule{.fig.pdf}{pdf}{.fig.pdf}{} command in your # document's preamble. See the grfguide.ps graphics package # documentation for more information. # # BIB_FILES # $(DOCUMENT).tex will be automatically searched for all .bib files # specified via \bibliography{} commands. Use this variable to # override the automatic search and manually specify the .bib files. # Reasons for wanting to override the automatics: (i) scanning a # large document can be time-consuming and can be skipped by entering # the info yourself (ii) the algorithm may be broken for your # document. # # BBL_FILES # bibtex produces .bbl files as output from the .aux and .bib files, # and LaTeX will include its contents in subsequence passes. By # default, the name of the .bbl file is $(DOCUMENT).bbl, but you can # manually override that default by setting the value of this # variable to a non-empty string before including this rules file. # # BSTINPUTS # BIBINPUTS # The contents of these variables override the system default search # paths for bibtex. If you are using a custom bibliographic style, # you may need to set BSTINPUTS to the directory in which the .bst # file resides. If your .bib databases cannot be found, then you # will need to set BIBINPUTS to a colon-separated list of the # directories in which they reside. # # DVIPS_FLAGS # Flags to pass to dvips. It might be necessary to include the -K # flag if you are having trouble getting mpage to handle documents # that contain EPS figures generated by certain applications. # ############################################################################### # # Preamble # ############################################################################### DVI_FILE := $(DOCUMENT).dvi PDF_FILE := $(DOCUMENT).pdf PS_FILE := $(DOCUMENT).ps LOF_FILE := $(shell "ls" $(DOCUMENT).lof 2>/dev/null) LOT_FILE := $(shell "ls" $(DOCUMENT).lot 2>/dev/null) TOC_FILE := $(shell "ls" $(DOCUMENT).toc 2>/dev/null) IDX_FILE := $(shell "ls" $(DOCUMENT).idx 2>/dev/null) END_FILE := $(shell "ls" $(DOCUMENT).end 2>/dev/null) AUX_FILES := $(DOCUMENT).aux TEX_FILES += $(DOCUMENT).tex OTHER_FILES := $(DOCUMENT).blg $(DOCUMENT).log $(DOCUMENT).out $(patsubst %.tex,%.aux,$(filter %.tex,$(TEX_FILES))) $(patsubst %.tex,%.out,$(filter %.tex,$(TEX_FILES))) $(patsubst %.tex,%.log,$(filter %.tex,$(TEX_FILES))) BEAMER_FILES := $(shell "ls" $(DOCUMENT).nav $(DOCUMENT).snm 2>/dev/null) # Grab the contents of \bibliograph{} commands. ifeq ($(BIB_FILES),) BIB_FILES := $(shell for source in $(TEX_FILES) ; do "sed" -e '{' -e 'y/,/ /' -e 's?.*\\bibliography[{]\(.*\)[}].*?\1?' -e 't' -e 'd' -e '}' <$$source ; done) BIB_FILES := $(BIB_FILES:%=%.bib) endif ifneq ($(BIB_FILES),) ifeq ($(BBL_FILES),) BBL_FILES := $(DOCUMENT).bbl endif endif ifneq ($(IDX_FILE),) IND_FILE := $(DOCUMENT).ind endif # Construct the names of graphic files generated by mpost. MPOST_GFX = $(string $(MPOST_FILES:%.mp=%.1) $(MPOST_FILES:%.mp=%.ps)) # Construct the names of auxiliary files related to XFig documents. XFIG_AUX = $(strip $(XFIG_FILES:%.fig=%.fig.aux)) XFIG_GFX = $(strip $(XFIG_FILES:%.fig=%.fig.pdf) $(XFIG_FILES:%.fig=%.fig.ps)) XFIG_TEX = $(strip $(XFIG_FILES:%.fig=%.fig.tex)) # LaTeX will be run over and over and over again until the following files # stop changing. MONITOR_FILES := $(strip $(AUX_FILES) $(TOC_FILE) $(LOF_FILE) $(LOT_FILE) $(BBL_FILES) $(IND_FILE) $(END_FILE) $(BEAMER_FILES)) # The following files must be present or processing will fail. REQUIRED_FILES := $(strip $(TEX_FILES) $(BIB_FILES) $(GRAPHIC_FILES) $(TEXT_FILES) $(XFIG_FILES)) # The files that should be deleted by the mostlyclean target LTX_MOSTLYCLEAN_FILES := $(strip $(MONITOR_FILES) $(MONITOR_FILES:%=%.old) $(OTHER_FILES) $(XFIG_AUX) $(XFIG_GFX) $(XFIG_TEX)) # The (additional) files that should be deleted by the clean target LTX_CLEAN_FILES := $(strip $(DVI_FILE) $(PDF_FILE) $(PS_FILE) $(BEAMER_FILES)) # These facilitate incorporating these LaTeX rules into automake .am files. EXTRA_DIST += $(REQUIRED_FILES) CONFIG_CLEAN_FILES += $(LTX_MOSTLYCLEAN_FILES) $(LTX_CLEAN_FILES) ############################################################################### # # Targets # ############################################################################### .PHONY : dvi dvi.local pdf pdf.local ps ps.local mostlyclean mostlyclean.local clean clean.local check_for_sources .SECONDARY : $(MONITOR_FILES) $(XFIG_AUX) $(XFIG_GFX) $(XFIG_TEX) # set default LaTeX processor if one not specified by user ifeq (,$(LATEX)) LATEX := latex endif # set the order of the targets so that the correct one is the default ifeq ($(notdir $(LATEX)),pdflatex) pdf : $(PDF_FILE) pdf.local dvi : $(DVI_FILE) dvi.local else dvi : $(DVI_FILE) dvi.local pdf : $(PDF_FILE) pdf.local endif pdf.local : dvi.local : # what the user wants to build when they ask for the "ps" target ps.local : ps : $(PS_FILE) ps.local # how to clean intermediate files mostlyclean.local : mostlyclean : mostlyclean.local -rm -f $(LTX_MOSTLYCLEAN_FILES) # how to clean back to the original sources clean.local : clean : mostlyclean clean.local -rm -f $(LTX_CLEAN_FILES) ############################################################################### # # Macros # ############################################################################### # # Generate the .dvi (or .pdf) file by running LaTeX (or PDFLaTeX) until the # auxiliary files listed in MONITOR_FILES stop changing. Rather than just # looping, make is re-run which allows any files that depend on the # particular auxiliary files that changed to be updated as well. # define run-latex @saveold () { for file ; do [ -f $${file} ] && "cp" -fp $${file} $${file}.old ; done ; "true" ; } ; \ restoreold () { for file ; do [ -f $${file}.old ] && "mv" -f $${file}.old $${file} ; done ; "true" ; } ; \ deleteold () { for file ; do "rm" -f $${file}.old ; done ; "true" ; } ; \ makeobsolete () { "touch" -r $$("ls" *.old | "tail" -n 1) $${1} ; "true" ; } ; \ nochange () { for file ; do [ ! -f $${1} ] || "cmp" $${1} $${1}.old >/dev/null || return ; done ; "true" ; } ; \ saveold $(MONITOR_FILES) ; \ if $(LATEX) $* ; then \ if nochange $(MONITOR_FILES) ; then \ echo "$(MAKE): LaTeX auxiliary files did not change (processing is complete)" ; \ restoreold $(MONITOR_FILES) ; \ else \ echo "$(MAKE): LaTeX auxiliary files changed (further processing is required)" ; \ echo "please wait..." ; sleep 2 ; \ makeobsolete $@ ; \ deleteold $(MONITOR_FILES) ; \ $(MAKE) --no-print-directory $@ ; \ fi ; \ else \ echo ; \ false ; \ fi endef ############################################################################### # # Dependancies and Generation Rules # ############################################################################### # # Check for the existance of all required source files. # check_for_sources : @FOUNDALL=1 ; for source in $(REQUIRED_FILES) ; do [ -f "$$source" ] || { echo "Error: cannot find source file: $$source" ; FOUNDALL=0 ; } ; done ; [ $$FOUNDALL == 1 ] # # Generate a postscript file from a .dvi file. # %.ps : %.dvi dvips $(DVIPS_FLAGS) -o $@ $* # # Generate the .dvi or .pdf file from the LaTeX sources. # $(DVI_FILE) : %.dvi : $(TEX_FILES) $(MONITOR_FILES) $(GRAPHIC_FILES) $(TEXT_FILES) $(XFIG_TEX) $(run-latex) $(PDF_FILE) : %.pdf : $(TEX_FILES) $(MONITOR_FILES) $(GRAPHIC_FILES) $(TEXT_FILES) $(XFIG_TEX) $(run-latex) # # Generate a .bbl file from the .aux file. # %.bbl : %.aux $(BIB_FILES) BSTINPUTS="$(BSTINPUTS)" BIBINPUTS="$(BIBINPUTS)" bibtex $* # # Generate a .ind file from the .idx file. # %.ind : %.idx makeindex $< # # Generate a .aux or .idx file if it doesn't already exist. The existance # of these files is a prerequisite for the main document processing loop # above so that's what we're doing here. Note, however, that all .fig.tex # files must be present in order for this first pass to succeed. # %.aux %.idx : $(XFIG_TEX) $(LATEX) $* # # Distill XFig .fig files into .fig.tex and either .fig.pdf or .fig.ps # compoents. # ifeq ($(notdir $(LATEX)),pdflatex) %.fig.tex : %.fig %.fig.pdf fig2dev -L pstex_t -p $*.fig.pdf <$< >$@ else %.fig.tex : %.fig %.fig.ps fig2dev -L pstex_t -p $*.fig.ps <$< >$@ endif %.fig.pdf : %.fig cd $(dir $<) && fig2dev -L pstex <$(notdir $<) | ps2pdf - - >$(notdir $@) %.fig.ps : %.fig cd $(dir $<) && fig2dev -L pstex <$(notdir $<) >$(notdir $@)