latex shell script

Sun, 2008-08-17 15:53

Okay, so I should have learned this about 20 years ago, but I didn't figure it out until today, and thought others might be helped by it.

I use latex for all "word processing" since I write a lot of math. Most of the time I work on my Mac OS X laptop in "terminal", so I have a command line. To "tex" a latex file, I would type

latex file.tex

and then

dvipdf file

to turn it into a pdf, which I then view with Acrobat Reader. This works really well, and even for large documents, is very fast. The down side is I have to type two commands. In addition, I often need to "tex" the file more the once to get references correct. This means more commands to type. Finally, I decided that I should write a shell script to do this for me. I'd never written a shell script before, but I correctly figured, "How hard could it be?". Here's the result:

#! /bin/sh
FIRST_ARGUMENT="$1"
SECOND_ARGUMENT="$2"
for ((i=1; i<=$FIRST_ARGUMENT; i++))
do
latex $SECOND_ARGUMENT
done
echo ""
echo "Now making pdf..."
dvipdf ${SECOND_ARGUMENT%%.tex}

It's pretty self-explanatory except perhaps the last line. This removes the ".tex" extension from the tex file before executing the dvipdf command.

I put this script (I called it "texit") in /sw/bin (you would want to do a "sudo mv" to that location if you do not automatically have admin privileges) and made it executable. It works like this, for example:

texit 2 file.tex

The action is to "tex" file.tex twice and then create a pdf from the dvi file. It works great! Hooray! One command instead of three!