--- /dev/null
+#
+# Example makefile using mjotex and a BibTeX references database.
+#
+
+# The latex compiler. The SOURCE_DATE_EPOCH=0 prevents the creation
+# and modification dates from being embedded as metadata into the
+# output file; that in turn is important because it allows us to tell
+# when the output stops changing (that is, when we are done). The
+# variable is supported in pdftex v1.40.17 and later.
+LATEX = SOURCE_DATE_EPOCH=0 pdflatex -file-line-error -halt-on-error
+
+# The name of this document.
+#
+# For example, to use the name of our parent directory:
+#
+# PN = $(notdir $(realpath .))
+#
+PN = example
+
+# A space-separated list of bib files. These must all belong to paths
+# contained in your $BIBINPUTS environment variable.
+#
+# Leave commented if you don't use a bibliography database.
+#
+#BIBS = local-references.bib
+
+# A space-separated list of the mjotex files that you use. The path to
+# mjotex must be contain in your $TEXINPUTS environment variable.
+#
+MJOTEX = mjotex.sty mjo.bst
+
+# Beamer template
+BEAMERMJO = beamercolorthememjo.sty beamerfontthememjo.sty
+BEAMERMJO += beamerinnerthememjo.sty beamerouterthememjo.sty
+BEAMERMJO += beamerthememjo.sty
+BEAMERMJOPATHS = $(shell kpsewhich $(BEAMERMJO))
+
+# Use kpsewhich (from the kpathsea suite) to find the absolute paths
+# of the bibtex/mjotex files listed in in $(BIBS)/$(MJOTEX). The SRCS
+# variable should contain all (Bib)TeX source files for the document.
+SRCS = $(PN).tex
+ifdef BIBS
+BIBPATHS = $(shell kpsewhich $(BIBS))
+SRCS += $(BIBPATHS)
+endif
+ifdef MJOTEX
+MJOTEXPATHS = $(shell kpsewhich $(MJOTEX))
+SRCS += $(MJOTEXPATHS)
+endif
+
+SRCS += $(BEAMERMJOPATHS)
+
+
+# The first target is the default, so put the PDF document first.
+#
+# This voodoo is all designed to find a "fixed point" of calling
+# $(LATEX). When you build a LaTeX document, it requires an unknown
+# number of compilation passes. How do you know when to stop? Easy,
+# stop when the output file stops changing! But how to encode that
+# in a makefile?
+#
+# At the start of this target, we call $(LATEX) to compile $(PN).tex.
+# Afterwards, we check for the existence of a "previous" file. If
+# there isn't one, then this is the first time that we've built the
+# PDF. In that case, we take the PDF that we've just built and make it
+# the "previous" file before starting all over. If, on the other hand,
+# there already *was* a "previous" file, then this is the second (or
+# third...) time that we've built the PDF. We diff the newly-built PDF
+# against the "previous" file; if they're the same, then we've
+# succeeded and stop. Otherwise, we make the new PDF the "previous"
+# one, and start all over. The end result is that we will loop until
+# the newly-created PDF and the "previous" one are identical.
+#
+$(PN).pdf: $(SRCS) $(PN).bbl
+ $(LATEX) $(PN).tex
+
+ if [ -f $@.previous ] && cmp -s $@ $@.previous; then \
+ rm $@.previous; \
+ else \
+ mv $@ $@.previous; \
+ $(MAKE) $@; \
+ fi;
+
+
+$(PN).aux: $(SRCS)
+ $(LATEX) $(PN).tex
+
+
+ifdef INDICES
+# We need to be able to build the index source files without involving
+# the main $(PN).pdf rule, in order to avoid a chicken-and-egg problem.
+# This is similar to the $(PN).aux rule above, except that an index is
+# optional and there might be more than one of them.
+$(INDEX_SRCS): $(PN).tex
+ $(LATEX) $(PN).tex
+endif
+
+# The pipe below indicates an "order-only dependency" on the aux file.
+# Without it, every compilation of $(PN).tex would produce a new
+# $(PN).aux, and thus $(PN).bbl would be rebuilt. This in turn causes
+# $(PN).pdf to appear out-of-date, which leads to a recompilation of
+# $(PN).tex... and so on. The order-only dependency means we won't
+# rebuild $(PN).bbl if $(PN).aux changes.
+#
+# As a side effect, we now need to depend on $(SRCS) here, since we
+# won't pick it up transitively from $(PN).aux.
+#
+# If the $BIBS variable is undefined, we presume that there are no
+# references and create an empty bbl file. Otherwise, we risk trying
+# to run biblatex on an aux file containing no citations. If you do
+# define $BIBS but don't cite anything, you'll run into a similar
+# problem. Don't do that.
+#
+$(PN).bbl: $(SRCS) | $(PN).aux
+ifdef BIBS
+ bibtex $(PN).aux
+else
+ printf '' > $@
+endif
+
+# If the output PDF exists but the log file does not, then an attempt
+# to "build the log file" (i.e. build the PDF) would do nothing. Thus
+# whenever the log file does not exist, we do a fresh build.
+$(PN).log: $(SRCS)
+ $(MAKE) clean
+ $(MAKE)
+
+# Ensure that there are no overfull or underfull boxes in the output
+# document by parsing the log for said warnings.
+.PHONY: check-boxes
+check-boxes: $(PN).log
+ @! grep -i 'overfull\|underfull' $<
+
+# Run chktex to find silly mistakes. There is some exit code weirdness
+# (Savannah bug 53129), so we just look for empty output.
+.PHONY: check-chktex
+CHKTEX = chktex --localrc .chktexrc --quiet --inputfiles=0
+check-chktex:
+ @chktexout=$$($(CHKTEX) $(PN).tex); \
+ test -z "$${chktexout}" || { echo "$${chktexout}" 1>&2; exit 1; }
+
+# Ensure that there are no undefined references in the document by
+# parsing the log file for said warnings.
+.PHONY: check-undefined
+check-undefined: $(PN).log
+ @! grep -i 'undefined' $<
+
+# Run a suite of checks.
+.PHONY: check
+check: check-boxes check-chktex check-undefined
+
+# Clean up leftover junk. This only looks overcomplicated because
+# the *.{foo,bar} syntax supported by Bash is not POSIX, and Make
+# will execute these commands using /bin/sh (which should be POSIX).
+JUNK_EXTENSIONS = aux bbl bcf blg glo ilg ist listing lof log nav out pdf
+JUNK_EXTENSIONS += snm spl toc xml
+.PHONY: clean
+clean:
+ for ext in $(JUNK_EXTENSIONS); do rm -f *.$$ext; done;