Sunday, July 22, 2007

SEEing LaTeX 8: Dealing with Auxiliary Files

As noted in part 5 of this series, there were three main features to add to the LaTeX mode for SubEthaEdit (SEE). I've already taken care of the first, typesetting, by taking advantage of latexmk. In this installment, I'll address the second, cleaning up the mess of auxiliary files.

LaTeX produces several sorts of auxiliary file to handle cross-references, tables of contents, bibliographies, and more. The auxiliary files are essential to how LaTeX works, but of little use when, e.g., sending a paper to a co-author or submitting it to a journal. With a bit of bad luck, it is also possible to have a corrupted auxiliary file that prevents you from creating a PDF from correct LaTeX sources; deleting some or all of the auxiliary files solves this problem.

Auxiliary file deletion is thus an infrequent, but necessary task. It would be possible to just ignore the issue and do the needed clean-up in either the Terminal or the Finder, but doing it by hand can be error prone. Also, it is simple to add to SEE. Overall, it seems worth doing.

In an earlier post, I used latexmk to trigger typesetting from SEE. With the right command line options, latexmk can also be used to clean up the auxiliary files! Adding a clean-up feature to SEE then just requires some relatively minor modifications to the scripts used for typesetting.

First, let's look at the shell script. It becomes:

export PATH

cd "`dirname "$1"`"
latexmk -c "`basename "$1"`"
The only change is that latexmk is called with a -c option, so that it will clean up the auxiliary files, leaving the output PDF. I saved the script as

Second, we'll look at the AppleScript. It becomes:
tell application "SubEthaEdit"
    if exists path of front document then
        set latexFilePath to path of front document
        set modeResources to resource path of mode of front document
        --Unsaved document, so LaTeX not run on it and can just return
    end if
end tell

set cleanupScript to quote & modeResources & "/Scripts/shell/" & quote & space & quote & latexFilePath & quote

do shell script cleanupScript

on seescriptsettings()
    return {displayName:"Clean Up Auxiliary Files"}
end seescriptsettings
Again, the changes are minimal. For typesetting, it mattered whether the front document was saved; here, it is only necessary to check whether the document has ever been saved. If it has, we run the shell script to clean up the auxiliary files, but if it hasn't, then LaTeX must not have been run on it so we just quietly do nothing.

The seescriptsettings handler is simplified, as well. All that remains is a displayName, which appears in the mode menu. There seems to be little reason to have either a keyboard shortcut or a toolbar item, since the task is infrequent.

The clean-up done by latexmk is fairly conservative. It only eliminates the auxiliary files from the specifically named document (in our case, the front document in SEE), and not from other files that are included. Further, it doesn't clean up absolutely everything, since different LaTeX packages can create files of their own which latexmk doesn't recognize. My feeling is that being conservative here is appropriate, as we don't want to accidentally delete something important while getting rid of the usual suspects.

No comments: