# Texshop: Delete aux files in pdflatexmk engine

I'd like to enhance the pdflatexmk engine to delete the files afterwards. I'm coming from Automatically "Trash Aux Files" after successful typesetting in TeXShop, where the instruction is to write a Python file that does execute "pdftex", and then delete every other file ex-post.

However, I'm unaware in how I may call different texshop "engines". That is, instead of doing

command = '/usr/texbin/pdflatex ' + '--output-driver=/usr/texbin/xdvipdfmx ' +     OUTPUT_ARGUMENT + '"' + output_dir + '"' + ' ' + '"' + pdf_path + os.path.sep + tex_file + '"'


Since I'm saving my file in the /Engine folder, I thought I could simply

execfile("pdflatexmk.engine")


Traceback (most recent call last):
File "/Volumes/Stuff/user/Library/TeXShop/Engines/pdflatexmk_del.py", line 43, in     <module>
execfile("pdflatexmk.engine")
IOError: [Errno 2] No such file or directory: 'pdflatexmk.engine'

• If you delete aux files, cross references will never resolve – David Carlisle Oct 13 '14 at 19:29
• I want to delete them after the final pdf was printed. – FooBar Oct 13 '14 at 19:43
• when you have finished you can do rm *.aux it doesn't make sense to get latexmk etc to do that as it can never know when you have finished editing and never want to run latex on that document again. – David Carlisle Oct 13 '14 at 19:46
• I can't see when that is ever useful, that means every time to you edit the file and run latex it is going to take multiple runs to converge as the aux files are not there, this just makes processing much slower for no gain. – David Carlisle Oct 13 '14 at 20:11
• @FooBar Wouldn't calling latexmk -c do what you want? – egreg Dec 13 '14 at 21:25

Each engine is a shell script of its own, so probably the cleanest method to augment an existing engine is to add to the script.

What you want is to effectively call pdflatexmk -c after each successful run of pdflatexmk.

If you look at pdflatexmk.engine you'll see that the last line is:

"${LTMKBIN}"/latexmk -pdf -r "${LTMKEDIT}/latexmkrcedit" -r "${TSBIN}/pdflatexmkrc"${localrc} "$1"  That's the nut of the whole script: calling latexmk. To add commands that are executed only on successful completion of this line, add && <cmds>. So change the last line to: "${LTMKBIN}"/latexmk -pdf -r "${LTMKEDIT}/latexmkrcedit" -r "${TSBIN}/pdflatexmkrc" ${localrc} "$1" && "\${LTMKBIN}"/latexmk -c


Save that new engine as pdflatexmk-c.engine or something else you like, and restart TeXShop.

Then you can invoke this engine as the first line in your .tex file, as in:

% !TEX TS-program = pdflatexmk-c
\documentclass{article}
\usepackage{lipsum}

\begin{document}
\tableofcontents

\section{Foo}
\lipsum
\section{Bar}
\lipsum[1-12]

\end{document}


You'll see that pdflatexmk is run, running pdflatex a few times to get the generated references right, and then pdflatexmk -c is run, deleting the auxiliary files.

I have to say that during my whole experience on different stackexchange sites, I have never had such a negative experience w.r.t. a question; that is, a condescending "I know better than you want you should want" attitude.

Following is the simplest way to call texshop's pdflatexmk engine (if __file__ is placed in the Engine folder):

import os, subprocess
# run pdflatexmk
engine = os.path.dirname(__file__)+'/pdflatexmk.engine'
file = pdf_path + os.path.sep + tex_file
command = '{0} "{1}"'.format(engine, file)
subprocess.call(command, shell=True)


And then, a safer way to delete is go with a blacklist, rather than a whitelist. Wait a bit, in case we want to re-run the compiler:

# wait a bit before delete
import time
time.sleep(360) # in seconds
# delete
file_plain = tex_file.split('.')[:-1][0]
print file_plain
del_list = ['log', 'aux', 'bbl', 'blg', 'fdb_latexmk', 'fls', 'synctex.gz', 'toc']
files = [pdf_path + os.path.sep + file_plain+'.'+l for l in del_list]
for file in files: os.remove(file)