What is makefile and when do I need to use it along with latexmk?
This is a quite general question, and so it'll be hard to write down a complete answer. I'll talk about when I use Make and when I use latexmk
.
latexmk
is a tool for compiling .tex
files, and not much else. However, it's good at determining which files depend on which other files: you don't need to provide a list, so latexmk main
will only recompile when a dependency of main.tex
has changed. latexmk
will also compile files two or three times if needed (e.g. for bibliography, index, or hyperlink updates).
Make is much more general, but requires an explicit description of a file's dependencies and how to compile it. This information is provided in a file usually called makefile
. Suppose main
depends on main.tex
, macros.tex
, and eggs.tex
, and that it sometimes needs to be compiled twice. Then, the makefile might like this:
# example makefile
INPUTS = main.tex macros.tex eggs.tex
COMPILER = pdflatex --halt-on-error
main.pdf: $(INPUTS)
$(COMPILER) main.tex
$(COMPILER) main.tex
It's more work to manually specify the dependencies and compilation instructions, especially since they can change throughout a project. But Make's advantage is that it can execute any command. If your final file depends on running some statistical test and including the results in your document, Make can run the test. If your file contains code listings, latexmk
doesn't know to watch them for changes, but you can update $(INPUTS)
to include them if you're using Make.
Another use for Make is to automatically do something to your output files after compilation.
In summary, Make is more complicated, but able to handle non-TeX dependencies, so use it only if you have non-TeX dependencies or need to do postprocessing.
Examples of such dependencies:
- source code listings in a non-TeX language
- statistical graphs or statistical analyses that are automatically generated from data
- images that are automatically generated from inputs or data
- information obtained from the “outside world,” e.g. the user or the Internet
Examples of postprocessing:
- Converting the output PDF into an image
- Moving the output PDF into another directory
- Compressing the output PDF
I would like to know what
makefile
is
The command make
most often refers to GNU make or BSD make.
Each variant has its merits;
the differences are for advanced uses not discussed here.
Regardless of which variant is used, make
expects to find a file named
Makefile
which contains recipes on what files (so-called targets) to build
from other files (so-called dependencies) and what commands to use for
building the targets.
make
then figures out what targets need to be built or rebuilt
and issues the required commands.
Essential points:
- You have to write a
Makefile
with the needed recipes. make
executes each rebuild command for a given target at most once. That is unfortunate for LaTeX document processing where e.g. severalpdflatex
runs may be needed.make
detects changes in dependencies by looking at file last-written times. But everypdflatex
run writes an.aux
file, therefore the nextmake
run will behave as if the.aux
file had changed, regardless of whether its actual contents have changed or not.
So if you'd like to formulate a recipe that re-runs pdflatex
until .aux
file contents do not change anymore,
this is not a straightforward thing to do with make
.
With a lot of sophistication and some tricks,
it is possible
to formulate a useful Makefile
, but its recipes tend to look very
complicated.
latexmk
is specialized for LaTeX-based document processing.
It knows how to employ the various *tex
programs and auxiliary tools like
bibtex
, biber
, or makeindex
.
It does not need a Makefile
, it can repeat commands as needed,
and it detects changes by looking at file contents (actually checksums)
rather than last-written times.
when I need to use both
makefile
andlatexmk
instead oflatexmk
only
There is an answer
proposing a Makefile
fragment that causes make
to do the equivalent
of latexmk -pdf
. In fact, such a make
will delegate the document
processing details to latexmk -pdf
with some fancy options added.
So why use make
at all then?
If you just want the functionality of latexmk
, you do not need make
.
However,
- You can add more recipes to the
Makefile
so thatmake
can do more things than just processing LaTeX documents. Cf. Arun's answer. I typically add recipes for compiling programs, running tests, producing illustrations, and the like. - Users of open-source software universally expect that
make
will build the files they want. By providing a suitableMakefile
, this expectation is met. make
is shorter to type. If you recompile frequently, such things matter.
If the above reasons do not appeal to you, you might sum those up as:
make
with latexmk
is for users already accustomed to make
who recognize the utility of latexmk
.