I'm trying to create a script that compiles LaTeX on OS X. Unfortunately I'm not familiar with the language and even though the code works, it has several flaws. Is it possible to close the terminal without having to kill it (and every other instance of it)? Can it be improved easily to automatically find and compile all .tex-files in the folder?

BASEDIR="$( dirname "$0" )"
pdflatex $STR.tex
bibtex $STR.aux
pdflatex $STR.tex
pdflatex $STR.tex

rm $STR.ilg
rm $STR.toc
rm $STR.aux
rm $STR.out
open $STR.pdf
killall Terminal
  • 1
    In general you don't want to remove auxiliary files, unless you're sure that no more changes are needed to the document.
    – egreg
    Feb 19, 2013 at 12:52
  • 2
    Putting together a reasonable bash script for compiling Latex might be useful, even though the proper answer is probably just latexmk - you need to look at timestamps to do this well. Further to what @egreg said, removing auxiliary files that are more than a week old is usually OK, but that's best done by a housekeeping program, not a compile script. Feb 19, 2013 at 14:07
  • Why am I supposed to save the auxiliary files? They are recreated every time I run the script. Is there something I've missed?
    – Hugo
    Feb 19, 2013 at 15:49
  • @Hugo the .aux file is used for many things including cross-referencing, citations, language data (if you are using babel) and stores information that is then read back in on subsequent latex and bibtex compilations. So if you delete them immediately after your first compilation your document will not be complete.
    – Alan Munn
    Feb 19, 2013 at 16:05
  • 1
    @Hugo, if you delete them after every compilation they will have to be recreated with every new compilation. This means that, for example, if you fix a typo in your document and compile it again, latex will have to create all the cross-references, citations, etc. from scratch. That could be the difference between a single latex run or multiple latex - bibtexruns every time. On a short document that might not be a big deal, but it can become pretty annoying very fast on a larger document.
    – Ricardo
    Feb 19, 2013 at 18:15

3 Answers 3



pdflatex document

where document.tex is the name of your document. This will need to be repeated a few times to resolve cross references, and to save the hassle of running it repeatedly, try

latexmk -pdf document

or, if you have a bibliography, try

latexmk -pdf -bibtex document

I use this last command all the time, generally inside a Makefile.

  • Yes, I'll check out latexmk. I would like to run it as a script though. As of now I have to edit my file each time I compile...
    – Hugo
    Feb 19, 2013 at 15:53

You should look for a makefile. Type 'LaTeX makefile' in Google.

  • 1
    Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer (that obviously can be found via google) here, and provide the google search term for reference.
    – Spontifixus
    Feb 18, 2013 at 18:18

Assuming you installed TeX Live via the MacTeX installer you have TeXShop. Open the file in TeXShop, place the line

% !TEX TS-program = pdflatexmk

at the top of the file and Typeset->Typeset (Cmd-T) will use latexmk. Of course you can edit and preview the file from within TeXShop.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .