LaTeX utility scripts

Processing a LaTeX file usually takes several steps. At a bare minimum, it usually requires 2 runs through latex (or pdflatex). Two runs are necessary in order to get cross-references and the table of contents right. Since LaTeX processes a page at a time, it can’t generate a table of contents on page 1 until it knows what sections, subsections etc. are in the rest of the file. That is what the .aux file is. Then on the second run, LaTeX reads that info from the .aux file. If you are running bibtex, or making an index, there are additional programs to run. Typing this from the command line (or even hitting the compile button from a GUI like TeXshop) can be tedious. I know that many people use Makefiles to achieve this task. However, as far as I know, Makefiles are specific to a particular project. That is, for every new LaTeX project, you have to create a new Makefile. Instead, I use bash scripts. This allows me to specify a filename on the command line, and I also get some more flexibility from the power of bash. I choose bash for this task, since it is mostly just stringing together commands I would perform on the command line anyways. It is also installed on most any linux machine, and on Mac OSX. (Sorry windows users, (unless you use cygwin)).

I have several different scripts, depending on what I am doing.

Traditional LaTeX

#!/bin/bash
# this script processes a latex file.
DVIPS=dvips
PDF=ps2pdf
PROG=latex
SEED=`echo $1 | cut -f1 -d"."`
$PROG --shell-escape -interaction=batchmode $SEED &&
(if [ -e ${SEED}.idx ]; then
  makeindex $SEED
fi) &&
(if [ $(grep bibdata ${SEED}.aux) ]; then
  bibtex $SEED &&
  $PROG --shell-escape -interaction=batchmode $SEED &&
  $PROG --shell-escape -interaction=batchmode $SEED
fi) &&
$PROG --shell-escape -interaction=batchmode $SEED &&
$DVIPS $SEED.dvi -t letter -Ppdf -o $SEED.ps &&
$PDF $SEED.ps &&
echo "*****************************
  SUCCESSFULLY PROCESSED $SEED
*****************************"
||
echo "*****************************
  PARSING PROBLEM with $SEED. run $PROG manually to see errors
*****************************"

For pdflatex

#!/bin/bash
PROG=pdflatex
SEED=`echo $1 | cut -f1 -d"."`
$PROG --shell-escape -interaction=batchmode $SEED &&
(if [ -e ${SEED}.idx ]; then
  makeindex $SEED
fi) &&
(if [ $(grep bibdata ${SEED}.aux) ]; then
  bibtex $SEED &&
  $PROG --shell-escape -interaction=batchmode $SEED &&
  $PROG --shell-escape -interaction=batchmode $SEED
fi) &&
$PROG --shell-escape -interaction=batchmode $SEED &&
echo "*****************************
  SUCCESSFULLY PROCESSED $SEED
*****************************"
||
echo "*****************************
  PARSING PROBLEM with $SEED. run $PROG manually to see errors
*****************************"

You can run the scripts like so:

pdftexit myfile.tex

The .tex is optional. In fact you could use .pdf, or nothing. The extension gets stripped off. Note that as it is currently written, if you have a file like 2008.04.11.tex, it will break, since it splits by period, and only takes the first part. A way around this would be to use basename instead, but that would not work for files named anything other than .tex.

Also note that I suppress most of latex’s output by using the -interaction=batchmode option. By default LaTeX prints out quite a bit of information, and printing to the screen can really slow things down. If something goes wrong, then you can always run it once manually. The script will detect if something goes wrong and tell you.

I hope you find the scripts useful. For convenience, here is zip file with both bash scripts.

This entry was posted in latex and tagged , , , . Bookmark the permalink.

9 Responses to LaTeX utility scripts

  1. zhanglong says:

    Awesome! It’s very convenient. thanks.

  2. rob says:

    Hello,

    Have you seen the rubber system? Here is a link. It’s a series of Python scripts created to accomplish exactly the same task at which you are aiming. You give it the base name of the file and a flag specifying the output format (pdf, ps, dvi) and it runs latex/bibtex/makeindex the proper number of times and in the right order. It also has a ton of other options, and can even be made to run special pre-processing and post-processing tasks (such as conversion of figures and generation of graphs). Just thought you might find it useful.

    Cheers,
    Rob

  3. Pingback: blogring.org

  4. This is definitely better than Make files. Thanks! What do you think of a -quick option to not do makeindex and bibtex (and the repeat latex runs)? Also, an option to clean up the all the junk files might be nice (*.bbl *.blg *.log *.aux *.lof *.lot *.toc *.loa).

  5. robfelty says:

    @Danny – note that bibtex and makeindex are only run if you are actually using them. That is, if you don’t have an index or any references, they don’t get run. If you mean that you are using them for a particular document, but haven’t updated them, and just want to recompile, then running latex once or twice would be enough. I was playing around with some options, but I didn’t get around to doing any. Maybe I will try it out. Thanks for the suggestion.

    Rob

  6. robfelty says:

    @drowsy – I am certainly NOT in the business of reinventing the wheel. I had never heard of rubber or latexmk. I’ll tell you one thing though, searching for “latex rubber” doesn’t really return what I am looking for. Thanks for alerting me to them. I will check them out.

    I have been using these scripts for several years now. One of my friends gave me a little script that he used, and I expanded it a bit. I thought it would be worth sharing, and as usual, it seems that it has been worth it, for me at least, since I learned about some new tools.

    Thanks again for the tips. I will definitely check them out.

    Rob

  7. drowsy says:

    Rob, what distro are you using?

    Both latexmk and rubber are Debian packages…
    I guess they are in Ubuntu too.

  8. robfelty says:

    @drowsy,

    I use fedora, ubuntu, and mac osx. Fedora has them in their distros too. They don’t seem to be included in TeXLive though, which is too bad.

    Rob

  9. I just got latexmk and it’s amazing. One thing that’s always been annoying is that when there’s a latex error, latex will pause with a prompt. So after you fix the error you have to go and manually dismiss that prompt before latex can reprocess the file. I discovered that pdflatex has a “-halt-on-error” switch will make it just exit when there’s an error. So I put this in my ~/.latexmkrc:
    $pdflatex = ‘pdflatex -halt-on-error’;
    That works fine but now there’s a new (though milder) annoyance: When there’s a latex error, latexmk keeps trying to compile again and again forever. It should of course sit tight till it detects a change in the source file.