Offline LaTeX Development

While online clients like ShareLaTeX or writeLaTeX are popular for getting started with LaTeX quickly, developing LaTeX locally with Vim and the command line is my preferred LaTeX workflow. In this post, I’ll describe the changes I’ve made that make working with LaTeX on the command line a seamless experience.

Install LaTeX

Obviously, to work with LaTeX locally, you’ll need LaTeX installed. To check if you already have it installed, you can run which pdflatex. If it’s installed, this command will tell you the path to program. Otherwise, it won’t print anything.

On Linux

Installing LaTeX on Linux isn’t too bad. Usually it’s included in your distribution’s package manager. I’ll be focusing on OS X for the majority of this post though, so Google around if you end up having trouble.

On OS X

To install LaTeX on a Mac, we’ll be installing MacTeX, which includes the command line LaTeX utilities as well as a couple graphical clients for LaTeX development. You can try compiling from source, but as Homebrew points out when you try to brew install latex:

$ brew install latex
Error: No available formula for latex
Installing TeX from source is weird and gross, requires a lot of patches,
and only builds 32-bit (and thus can't use Homebrew deps on Snow Leopard.)

We recommend using a MacTeX distribution: http://www.tug.org/mactex/

With that in mind, head on over to http://www.tug.org/mactex/ and download the file MacTeX.pkg. Once this has downloaded and you’ve clicked through the installer, you should be ready to go with LaTeX. Verify this by running which pdflatex again.

Use Vim

The biggest productivity improvement you gain from developing LaTeX locally is that you get to use Vim. Make sure you have a nice colorscheme for both your terminal and for Vim. I can’t stress enough how important it is to make your terminal look nice: you want to enjoy your terminal experience, and this is one of the easiest ways to do so.

Use Make

Compiling LaTeX is pretty straightforward. To generate a PDF, all you have to do is run the command

$ pdflatex <myfile>.tex

and you’ll get a file called <myfile>.pdf in the current directory, plus some intermediate files. We can go one step further and put a bunch of useful build targets into a Makefile and use it to build our PDF:

# NOTE: Change "written" to the name of your TeX file with no extension
TARGET=written

all: $(TARGET).pdf

# Generalized rule: how to build a .pdf from each .tex
LATEXPDFS=$(patsubst %.tex,%.pdf,$(wildcard *.tex))
$(LATEXPDFS): %.pdf: %.tex
  pdflatex -interaction nonstopmode $(patsubst %.pdf,%.tex,$@)

clean:
  rm *.aux *.log || true

veryclean: clean
  rm $(TARGET).pdf

view: $(TARGET).pdf
  if [ "Darwin" = "$(shell uname)" ]; then open $(TARGET).pdf ; else evince $(TARGET).pdf ; fi

submit: $(TARGET).pdf
  cp $(TARGET).pdf ../

print: $(TARGET).pdf
  lpr $(TARGET).pdf

.PHONY: all clean veryclean view print
LaTeX Makefile

If you save this to a file called Makefile in the same directory as your LaTeX file, we can just run make instead of running pdflatex <myfile>.tex!

As you can see, there are a bunch of other handy targets here:

Workflow Tips

Right now, our workflow looks like this:

We can actually optimize this workflow to one less step: we don’t have to get out of Vim to run make!

Vim has a command :make that will look for a Makefile in the current directory and run it’s all target. It also takes a target as an optional argument, so we can do :make view to compile and view the document from within Vim!

Taking this one step further, we can add a command to shorten this. If we add

command WV w | make view
Save, Compile and View in Vim

to our .vimrc, we’ll only have to type :WV to save, compile, and view our PDF output.

Wrap Up

That’s it! I like this experience for a bunch of reasons:

Do you have a LaTeX tip, a fancier Makefile, or a favorite vim plugin for LaTeX? Share it in the comments!

Part 2!

Actually that’s not it, there’s more! I wrote a Part 2 to this post, which you should definitely check out.