Personal Website.

The Simplest & Most Useful Makefile (LaTeX)

I’ve started using LaTeX when I started my degree at uni a bit more than 5 years ago. When I first heard about it I thought it was a software like Microsoft Office, just not as horrible with the formatting. That has all changed and LaTeX has become my language of choice for writing papers, reports, coursework or anything closely related. I found it quite handy that there are so many different editors available until I have recently discovered the best of them all: Emacs + make.

I was looking for something new because I became more and more fed up with so and so often having to reach to the mouse and click on buttons to compile my bib and tex files. The solution: using a Makefile for the compilation process. I had always told myself I should really learn how to write Makefiles, but I never really got round to it. The one for LaTeX documents is a very neat one, though, since it doesn’t require much knowledge and is very short:
 # LaTeX Makefile FILE=report

 all: $(FILE).pdf .PHONY: clean clean: \rm *.aux *.blg *.out *.bbl *.log  $(FILE).pdf: $(FILE).tex pdflatex$(FILE) pdflatex $(FILE) bibtex$(FILE) pdflatex $(FILE) pdflatex$(FILE) 
So let’s examine this in a bit more detail. At the beginning I just define a variable called FILE, which will hold the filename of the tex file. By using a variable here I made sure I will only ever have to change this one single line when I use different tex files. A Makefile command looks generally like this:
 target: dependencies 
The target all defines what happens when you just type make in the terminal. Here, all has the finished pdf file as a dependency. It will then check the dependencies for the pdf file (target $(FILE).pdf) and if none of the source files have changed and the pdf file exists already it will do nothing. So make will only take action if it has to, which is a handy feature, as it minimises the work to be done by ruling out things that don’t have to be done. If the pdf file doesn’t exist or any of the dependencies for it (tex or bib files in this case) have changed, it will run all the dependencies. You could also run make report.pdf and with that call the target $(FILE).pdf explicitly. I have added one more target called clean, which deletes all files that are generated during the compilation process, except for the pdf file. You can call this command by typing make clean in the terminal.

There are many more options of what could be included in the Makefile, but here I only want to outline the bare essentials of it. My new LaTeX “editor” setup in the office is shown in the image below. One screen holds the terminal and Emacs and the other one has been set upright because in my opinion that makes for a better reading experience on the screen. I find this setup quite a lot more useful than having both screens in the normal setting.

Update: Rob has pointed out that you should apply the phony rule for targets that aren’t files. I’ve added that to the makefile

Comments on: "The Simplest & Most Useful Makefile (LaTeX)" (7)

1. Hey Alex,

.PHONY: clean

This makes the clean target special, and lets make know that it’s not really the name of a file.

2. Ah, thanks Rob, I’ll add that!

3. Josh said:

Nice setup! If you were so inclined, you get remove the terminal window and do everything inside emacs The first step, would be to run a shell inside emacs with M-x shell, and then you can just switch to that buffer to run your make file.

But I think there is an even better way. Have you used AUCTeX? It’s a more powerful TeX environment for Emacs. One of it’s many features allows you to set a “master file”, which is the top level TeX file, so you can compile from any derived file. It is also BibTeX aware and things like this.

The easiest way to get it, is to use the Emacs package manager (M-x package-list-packages). Unfortunately for you, it requires Emacs 24 and I see you are running Emacs 23. I’m not sure which distro you are using, but here are various install procedures.

• I actually used to do it running my terminal in Emacs at the beginning, but I switched to having the terminal separately because I felt I could work better with them being separate.
I’m running Mint 14, since it’s Ubuntu based it most often doesn’t have the latest software versions. But I’ll have a look at installing Emacs 24 and running AUCTeX, it sounds quite interesting and I’ve never heard of it so far. Thanks for the suggestion!

• Josh said:

I’d think you’d like AUCTeX. One of the more liked features is the ability to have inline previews. So, if you have an equation like this: $\forall n > 2: x^n+y^n=z^n$, the actual rendered image of the equation will show up in the Emacs buffer. BTW, WordPress’ support of $\LaTeX$ is great!

• Being able to look at equations straight away would indeed be a great feature.
And I didn’t know that WordPress does it too. Thanks a lot for your suggestions! Much appreciated.

4. [...] had posted a small tutorial for making your life with that tiny bit easier and more automated earlier. Today I want to introduce you to another very useful tool in that respect: texcount. It’s a [...]