惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
Jina AI
Jina AI
罗磊的独立博客
V
Visual Studio Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
J
Java Code Geeks
U
Unit 42
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog RSS Feed
爱范儿
爱范儿
酷 壳 – CoolShell
酷 壳 – CoolShell
Last Week in AI
Last Week in AI
T
The Blog of Author Tim Ferriss
腾讯CDC
Hugging Face - Blog
Hugging Face - Blog
T
Tailwind CSS Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
I
InfoQ
月光博客
月光博客
博客园_首页
Vercel News
Vercel News
P
Proofpoint News Feed
GbyAI
GbyAI
Y
Y Combinator Blog

drew's dev blog

Hunting a production-only proxy bug in SvelteKit Artisanal Handcrafted Git Repositories Everything you need to know about Python 3.13 – JIT and GIL went up the hill How Postgres stores oversized values – let's raise a TOAST How Postgres stores data on disk – this one's a page turner Bumper Comtrade QuEST Rust Wrapper Dotfiles How long is a cucumber? 🥒 Jekyll KaTeX Block Calculating the overlap of aerial photos Custom Jekyll plugins with GitHub Pages Calculating meters per pixel from aerial photographs Proper line numbers with Jekyll Compiling zsh without root Coding cheatsheet
Using make and latexmk for easy LaTeX compilation
Drew Silcock · 2014-09-23 · via drew's dev blog

· 4 min read

Still running pdflatex (or equivalent) every time you want to recompile your LaTeX\LaTeX document? There’s a simpler way using make and latexmk. All you need is a simple Makefile and you can tell LaTeX\LaTeX to automatically recompile your document every time you change a file, and run pdflatex the sufficient number of times to get cross-references right.

So I recently discovered latexmk, a utility that simplifies LaTeX\LaTeX compilation by automatically rerunning whatever compilation command you use to compile your documents (i.e. pdflatex, xelatex or whatever) the sufficient number of times in order to make sure cross-references resolve themselves fully.This in itself is super useful, but you can leverage the power of GNU make in addition to this to make compilation easy, continual and targeted only at changed files.

For the purposes of this article, I assume that you’re using pdflatex, but all of this equally applies to xelatex or similar by simply replacing the pdflatex command with whichever you use.

latexmk

The basic syntax of latexmk is as follows:

1

$ latexmk $OPTIONS -pdflatex="$COMPILATION_COMMAND $PDFLATEX_OPTIONS %O %S" yourtexfile.tex

Note that %O is replaced by latexmk with the options given to latexmk, and %S is replaced with the source file name, in this example yourtexfile.tex. Some useful options to give to latexmk include -pdf, which tells latexmk that your final produced document is a pdf, and -pvc, which will be discussed shortly. latexmk also summarises the errors and warnings incurred throughout the compilation, which is very useful considering they’re usually lost in a sea of output during normal pdflatex compilation.

This will automatically run pdflatex enough times to get those references resolved. But we can make this even more useful using the pdflatex option, --interaction=nonstopmode. This means that pdflatex automatically goes through the compilation, not requiring any user input. This importantly means that it goes right through any errors, not requiring the user to type X in to quit the compilation on error. If you prefer less verbose output, you can change --interaction=nonstopmode to --interaction=batchmode, which does the same thing, but outputs only succint, important information.

The next useful option to pass to latexmk in combination with this is -pvc. It’s purpose is to run continuously, and update your pdf viewer every time it updates your document.

make

Finally, we can wrap all this in a Makefile so we don’t have to type the long latexmk command in, and to detect changes in files so you’re not recompiling your file on no change.

Here’s a basic template for using make to simply this whole thing:

1

LATEX=pdflatex

2

LATEXOPT=--shell-escape

3

NONSTOP=--interaction=nonstopmode

4

5

LATEXMK=latexmk

6

LATEXMKOPT=-pdf

7

CONTINUOUS=-pvc

8

9

MAIN=yourtexfile

10

SOURCES=$(MAIN).tex Makefile yourothertexfiles

11

FIGURES := $(shell find figures/* images/* -type f)

12

13

all: $(MAIN).pdf

14

15

.refresh:

16

touch .refresh

17

18

$(MAIN).pdf: $(MAIN).tex .refresh $(SOURCES) $(FIGURES)

19

$(LATEXMK) $(LATEXMKOPT) $(CONTINUOUS) \

20

-pdflatex="$(LATEX) $(LATEXOPT) $(NONSTOP) %O %S" $(MAIN)

21

22

force:

23

touch .refresh

24

rm $(MAIN).pdf

25

$(LATEXMK) $(LATEXMKOPT) $(CONTINUOUS) \

26

-pdflatex="$(LATEX) $(LATEXOPT) %O %S" $(MAIN)

27

28

clean:

29

$(LATEXMK) -C $(MAIN)

30

rm -f $(MAIN).pdfsync

31

rm -rf *~ *.tmp

32

rm -f *.bbl *.blg *.aux *.end *.fls *.log *.out *.fdb_latexmk

33

34

once:

35

$(LATEXMK) $(LATEXMKOPT) -pdflatex="$(LATEX) $(LATEXOPT) %O %S" $(MAIN)

36

37

debug:

38

$(LATEX) $(LATEXOPT) $(MAIN)

39

40

.PHONY: clean force once all

If you don’t like latexmk running continuously, and want to run make manually, or use something like watch -n 1 make to update your document, then just get rid of the -pvc option in LATEXMKOPT. Otherwise, if you only need to compile the document once and don’t need to run latexmk continuously for recompilation, just run make once.

Using this template and copying it across your LaTeX\LaTeX documents hugely saves time on continually retyping in the compilation command, and means you can leave latexmk running in the background and ignore it (unless there’s an error, in which case you can run make debug to view the errors).