Posted on September 22, 2008 at 9:28 am

Scripting Documents with LaTeX

Before you can create documents with LaTeX, you must install a TeX distribution. A TeX distribution is the underlying software that typesets your documents for you and outputs the PDF document. The easiest way to install a distribution is to download and install the MacTeX TeXLive 2008 distribution. This distribution installs just about everything you will need to begin drafting documents. And, yes, the distribution is free.

Because LaTeX/XeTeX files are simply plain text, you can draft your documents in any text editor. However, for beginners, I recommend TeXShop. With TeXShop, you can draft and compile your document in one application. I use TextMate to draft and compile my documents, but it requires some setup. You can use TeXShop immediately after downloading it and installing it in your /Applications folder.

After installing TeXLive 2008 and TeXShop, you can begin to draft documents. Let’s start with an easy one. Open TeXShop and type the following:

\documentclass{article}

\begin{document}
Hello, world. This is the first paragraph.

This is the second paragraph.
\end{document}

LaTeX/XeTeX documents always begin with a \documentclass command. In almost every case, you will use the article document class.

Next, you send the \begin{document} command to LaTeX to let it know where your document begins. You then write your text. When you are done, you send the \end{document} command to LaTeX. In TeXShop, press the “Typeset” button and you should see the output. Notice that LaTeX automatically set the margins and added the page number at the bottom of the page. Of course, we can customize all the properties of your document if needed.

So, how can we fit this into our workflow? Suppose that you want to draft a letter to a contact from your FileMaker database. First, you would have to get the data from your database. Your script might look something like this:

tell application "FileMaker Pro"
tell database "Contacts.fp7"
set addressee to "fullName" of current record
end tell
end tell

Now, you can construct a string of text to use as your document:

set latexString to "\\documentclass{article}" & return & "\\begin{document}" & return & "Hello, " & addressee & " This is the first paragraph." & return & return & "This is the second paragraph." & return & "\\end{document}"

This string looks just like our LaTeX document that we typeset above, except that we need to escape the backward slashes so we don’t confuse AppleScript.

Next, we need to write the string out to a file. This serves two purposes. First, we cannot compile our documents easily without a document file. Second, we have a document to put under version control. To write a string out to a file, type the following:

try
set filePath to (((path to desktop) as string) & "letter.tex")
set f to open for access file filePath with write permission
write latexString to f
close access f
on error e
close access f
end try

This code sets a path to a file on our desktop. If the file doesn’t already exists (and it shouldn’t), then AppleScript creates a blank file for us. We then write our latexString to the file and close the file. Notice the error handling, too. We use this to ensure that our file gets closed. AppleScript does not like it when you open a file and forget to close it.

In the Finder, you should see a new document on your desktop named “letter.tex”. To typeset the document, we will use TeXShop’s AppleScript dictionary. Type this into your script:

tell application "TeXShop"
set doc to open file filePath
typeset doc
end tell

This chunk of code tells TeXShop to open the document and typeset it. Easy. When you run the script, you should see a file named “letter.pdf” on your desktop. Open it up and you will see your document typeset in PDF. But what are all those extra documents on the desktop? Relax. TeX outputs a log file and an auxillary file every time it compiles your document. The log file contains any errors or warning messages. You can safely delete these files.

As with many scripts, this seems like a lot of work. But the beauty of scripting is that you only need to setup your scripts once and then run them over and over again.

In the next installment, we’ll look at how to use your Mac fonts with TeX using XeTeX.

I note that you don’t need to use TeXShop to typeset your documents. You can use the “do shell script” command to typeset your documents using programs from the command line. This requires some advanced setup that is beyond the scope of this tutorial.

Leave a Reply