TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
share|improve this question

migrated from stackoverflow.com Feb 19 '13 at 12:11

This question came from our site for professional and enthusiast programmers.

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 '13 at 12:52
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. – Charles Stewart Feb 19 '13 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 '13 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 '13 at 16:05
@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 '13 at 18:15


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.

share|improve this answer
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 '13 at 15:53

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

share|improve this answer
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 '13 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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.