[Tex/LaTex] How to enable –shell-escape when using pdflatexmk

auto-pst-pdflatexmkpdftexshell-escape

I want to use the auto-pst-pdf package and therefore I need the --shell-escape option to be enabled. I did this in my TeXShop preferences as follows:

enter image description here

However when I use pdflatexmk (by writing % !TEX TS-program = pdflatexmk on the first line) to compile my document the option is not taken into account and auto-pst-pdf can not be used.
How do I enable the --shell-escape command with pdflatexmk?

I use TeXShop 3.24 on Mac OSX with pdflatexmk version 4.37.

Best Answer

Navigate to your ~/Library/TeXShop/Engines folder and duplicate the file pdflatexmk.engine to, say, pdflatexmk-shell.engine. Then open this file with TeXShop itself and add the -pdflatex="pdflatex --shell-escape %O %S" option: here's how the file should be:

#!/bin/bash
export TSBIN="$HOME/Library/TeXShop/bin/tslatexmk"
export LTMKBIN="$HOME/Library/TeXShop/bin/tslatexmk"
export LTMKEDIT="$HOME/Library/TeXShop/bin"
# make sure latexmkrcedit exists in bin
if [ ! -e "${LTMKEDIT}/latexmkrcedit" ] ; then
    cp "${LTMKBIN}/latexmkrcDONTedit" "${LTMKEDIT}/latexmkrcedit"
fi
"${LTMKBIN}"/latexmk -pdf -r "${LTMKEDIT}/latexmkrcedit" -r "${TSBIN}/pdflatexmkrc" -pdflatex="pdflatex --shell-escape %O %S" "$1"

Now, if you start your document with

% !TEX TS-program = pdflatexmk-shell

the shell escape will be enabled.

Here's a copy of the console when a file with that magic line is processed:

Latexmk: This is Latexmk, John Collins, 2 July 2013, version: 4.37.
**** Report bugs etc to John Collins <collins at phys.psu.edu>. ****
Rule 'pdflatex': File changes, etc:
   Non-existent destination files:
      'bart.pdf'
------------
Run number 1 of rule 'pdflatex'
------------
------------
Running 'pdflatex --shell-escape  -recorder  "bart.tex"'
------------
Latexmk: applying rule 'pdflatex'...
This is pdfTeX, Version 3.1415926-2.5-1.40.14 (TeX Live 2013)
 \write18 enabled.
entering extended mode
(./bart.tex
LaTeX2e <2011/06/27>
Babel <3.9f> and hyphenation patterns for 78 languages loaded.
(/usr/local/texlive/2013/texmf-dist/tex/latex/base/article.cls
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
(/usr/local/texlive/2013/texmf-dist/tex/latex/base/size10.clo)) (./bart.aux)
[1{/usr/local/texlive/2013/texmf-var/fonts/map/pdftex/updmap/pdftex.map}]
(./bart.aux) )</usr/local/texlive/2013/texmf-dist/fonts/type1/public/amsfonts/c
m/cmr10.pfb>
Output written on bart.pdf (1 page, 11541 bytes).
Transcript written on bart.log.
Latexmk: Log file says output to 'bart.pdf'
Latexmk: All targets (bart.pdf) are up-to-date

The lines

This is pdfTeX, Version 3.1415926-2.5-1.40.14 (TeX Live 2013)
 \write18 enabled.

are the proof that shell escape is enabled.