Using a Makefile to generate Latex documents

So, you're using Latex to compile a paper, article, or book. If you have any type of table of contents, index, or bibliography, you've probably noticed that you have to run latex two (or sometimes 3) times to generate the final document. In addition, you probably have to run a conversion program to get your document into its final format (like pdf).

If you're like me, I was getting tired of typing those commands every time I wanted to view a final copy of my paper. I decided to combine a couple great tools to "make" the job easier!

Makefiles aren't just for compiling code. You can use them to generate commands that produce any final document. There really isn't a limit as to what you can produce other than the commands available on your system.

For my current Latex document, I am creating an index.

\usepackage{makeidx}

I'm not going to go into the details of index creation with Latex here. There are good references all over for that [1]. Basically, you just add \index{Some Entry} at each point in your document that you want an item indexed. Then at the end, you include a "\printindex" command at the point you want your index to be output in your document.

After those commands are in place, your "latex mydocument" command produces a mydocument.idx file. You'll also see a notice that there is no mydocument.ind file. That is because the final index file that is included is not the idx (which is just all of your entries with their page number), but an ind file which is the idx file's entries, aggregated according to entry, and then combined with the appropriate style information to be printed correctly.

You have to run the following to create your .ind file:

makeindex mydocument

After you make the index, you can run latex again and your \printindex command has an ind file to include. You're now set with a dvi file that you can convert into another form for publishing.

Now, lets automate all that. The format for a Makefile rule is:

TARGET : PREREQS
   COMMANDS

All you have to do is add a rule for each output that you created with the process manually.

Here is a simple Makefile that I am using to create my document. The rules work in reverse order from the way I listed them in this post. I've added a rule for each step that I listed for creating my document and index. Don't forget to name it "Makefile"

mybook.pdf : mybook.dvi
   dvipdf mybook
mybook.dvi : mybook.ind
   latex mybook
mybook.ind : mybook.idx
   makeindex mybook
mybook.idx : *.tex
   latex mybook

Once your Makefile is saved, all you have to do is run "make". Of course, you might have figures or other resources that need to be included in your prerequisites for your sources, but the general idea ought to be the same for most projects. If you have questions or comments, feel free to leave them here.


[1] http://web.image.ufl.edu/help/latex/latex_indexes.shtml

4 Responses to “Using a Makefile to generate Latex documents”


  1. Byron Clark

    Or you could just use rubber (http://www.pps.jussieu.fr/~beffara/soft/rubber/) to automate all of the build steps for you.

  2. Dennis

    Thanks Byron!

    Sweet link. And it’s written in Python too. I’ll give it a try.

  3. Scott Robertson

    You can also have a look at the latex-makefile (http://code.google.com/p/latex-makefile/) by Chris Monson. I used it for most of my college papers and it worked great.

  4. Dennis

    Another good resource. Thanks Scott.



css.php