[Tex/LaTex] Texshop: Delete aux files in pdflatexmk engine

texshop

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")

instead – But this gives

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'

Best Answer

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.

Related Question