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

推荐订阅源

F
Full Disclosure
WordPress大学
WordPress大学
小众软件
小众软件
Cloudbric
Cloudbric
AWS News Blog
AWS News Blog
腾讯CDC
量子位
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Vulnerabilities – Threatpost
Scott Helme
Scott Helme
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Hacker News
The Hacker News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
IT之家
IT之家
Jina AI
Jina AI
Attack and Defense Labs
Attack and Defense Labs
S
SegmentFault 最新的问题
Simon Willison's Weblog
Simon Willison's Weblog
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
T
Tailwind CSS Blog
Last Week in AI
Last Week in AI
博客园 - 【当耐特】
Google Online Security Blog
Google Online Security Blog
美团技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
罗磊的独立博客
L
LINUX DO - 最新话题
博客园 - Franky
博客园 - 叶小钗
Apple Machine Learning Research
Apple Machine Learning Research
The Last Watchdog
The Last Watchdog
J
Java Code Geeks
AI
AI
C
Cisco Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
C
Cyber Attacks, Cyber Crime and Cyber Security
Cisco Talos Blog
Cisco Talos Blog
博客园 - 三生石上(FineUI控件)
雷峰网
雷峰网
Help Net Security
Help Net Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
云风的 BLOG
云风的 BLOG
I
Intezer
S
Securelist

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).