# Automatically “Trash Aux Files” after successful typesetting in TeXShop

Is there some way to automatically fire the "Trash Aux Files" command after successful typesetting in TeXShop, without manual action on the part of the user?

-
What is successful typesetting? Sometimes LaTeX needs to know what is in those files for correct placement (tikz) and numeration (label/ref). –  A.Ellett Dec 29 '12 at 0:50
–  cmhughes Dec 29 '12 at 0:55

In addition to Кигава Енсеи's answer for typesetting with the xelatex engine, below minor alteration works perfectly in TeXShop using pdflatex for typesetting

Just change `xelatex' to`pdflatex' and it will work

-

Though there may well be a cleaner method that I am not aware of, I have found that the following serves the same function. Note that the following will process the document using XeLaTeX, but can be customized to suit.

In the `~/Library/TeXShop/Engines` directory, create a python script, such as `XeLaTeX.py`, with the following contents:

``````# -*- coding: utf-8 -*-
import os, sys
tex_file = sys.argv[-1]
os.system("xelatex --shell-escape " + tex_file)
if os.path.isfile(os.path.splitext(os.path.basename(tex_file))[0] + ".aux"):
os.remove(os.path.splitext(os.path.basename(tex_file))[0] + ".aux")
if os.path.isfile(os.path.splitext(os.path.basename(tex_file))[0] + ".log"):
os.remove(os.path.splitext(os.path.basename(tex_file))[0] + ".log")
if os.path.isfile(os.path.splitext(os.path.basename(tex_file))[0] + ".out"):
os.remove(os.path.splitext(os.path.basename(tex_file))[0] + ".out")
if os.path.isfile(os.path.splitext(os.path.basename(tex_file))[0] + ".synctex.gz"):
os.remove(os.path.splitext(os.path.basename(tex_file))[0] + ".synctex.gz")
print(os.path.splitext(os.path.basename(tex_file))[0])
``````

Next, instruct TeXShop to use this file. One way of doing so is, in the `Engine` tab of the TeXShop Preferences, to edit the command line instruction for `Latex` under `pdfTeX` to read:

``````/usr/bin/python /Users/USERNAME/Library/TeXShop/Engines/XeLaTeX.py
``````

Now, after processing a file using the `LaTeX` command, the `.aux`, `.log`, and `.synctex.gz` files will all be automatically deleted; the script can be easily edited to delete any other files also.

Lastly, as noted in the comments to the original question, this can cause problems if these files must be referred to again, and the script should not be used in such cases.

-
To add this this wondeful answer, also check out gist.github.com/macmadness86/5269856 –  macmadness86 Mar 31 '13 at 11:23
Deleting aux files will prevent most latex documents being typeset correctly. –  David Carlisle Oct 13 '14 at 19:32

You could add an alias to a script like this in you're on Mac or Linux:

``````import os,sys

args = sys.argv
print(args)
if len(args) < 1:
print(
'''
please rerun, being sure to include your tex file(s) as an argument!
''')
sys.exit(0)

for tex in args[1:]:
os.system('pdflatex {0}.tex'.format(tex))
os.system('rm *.aux *.log')
``````
1. put above in `~/bin` as `trash-aux.py`
2. edit `~/.bashrc` or whatever it is on OS X to include `alias leantex='python ~/bin/trash-aux.py'`

Done!

Notes

• this won't replicate non-basic `pdflatex` calls (e.g. +-draftmode+ will fail)
• it can process multiple files: `leantex hw1 hw2 problem_set`
-