Adding an option to the PDFLaTeX call from AUCTeX

Starting from version 11.88 of AUCTeX, you can add an option to the TeX processor with the file-local variable TeX-command-extra-options:

%%% TeX-command-extra-options: "-shell-escape"

As explained in the manual, you have to manually make this variable safe as a local variable because of the security holes it can open.

Note: this question inspired me to add this feature.


Until version 11.87, AUCTeX hadn't a facility to add easily an option to the compiler (at least I always missed to find it). I see at least three strategies you can follow:

  1. edit LaTeX-command-style (the suggested way if you want to activate shell escape for all documents)
  2. add a new element to TeX-engine-alist
  3. add a new element to TeX-command-list

I'll try to outline 1 and 2. 3 is similar to 2 in spirit.

Note: in a previous version of the answer I suggested to edit LaTeX-command, but that variable should be reserved to the actual latex binary name. Setting it to latex -shell-escape doesn't play well with forward/inverse search (for the curious person: TeX-source-correlate-determine-method would fail because there is no latex -shell-escape binary and so forward/inverse search would fall back on source specials, even if SyncTeX is actually available).


LaTeX-command-style

This variable allows to change the options passed to the compiler, its syntax is a bit cumbersome, though. In addition, this isn't a file local variable so you can't set (by default) it on a per-document basis.

The customize way

Issue M-x customize-variable RET LaTeX-command-style RET. In the String field of the variable add -shell-escape after %(latex). E.g., if the value of the field is

%(PDF)%(latex) %S%(PDFout)"

change it into

%(PDF)%(latex) -shell-escape %S%(PDFout)"

The do-it-for-me way

Add the following code to your .emacs.

(setq LaTeX-command-style '(("" "%(PDF)%(latex) -shell-escape %S%(PDFout)")))

TeX-engine-alist

Insert a new element, called for example default-shell-escape, similar to the default element of TeX-engine-alist-builtin. You can do it by using the customization interface or adding an Elisp code to your .emacs.

After creating this new engine, you'll be able to activate it by using the menu Command > TeXing Options > Use Default with shell escape. If you want to set it by default for all documents or on a per-document basis see below.

The customize way

Issue M-x customize-variable RET TeX-engine-alist RET. Press the INS button and fill the fields in the following way:

  • Symbol: default-shell-escape
  • Name: Default with shell escape
  • Plain TeX command: tex -shell-escape
  • LaTeX command: latex -shell-escape
  • ConTeXt command: leave empty

The do-it-for-me way

Add the following code to your .emacs

(eval-after-load "tex"
  '(progn
     (add-to-list
      'TeX-engine-alist
      '(default-shell-escape "Default with shell escape"
     "pdftex -shell-escape"
     "pdflatex -shell-escape"
     ConTeXt-engine))
     ;; (setq-default TeX-engine 'default-shell-escape)
     ))

Set the default-shell-escape engine as default

Now you can set the new fictitious engine as default for all documents

  1. by customizing TeX-engine (M-x customize-varaiable RET TeX-engine RET) and selecting Default with shell escape in the drop-down Value Menu, or
  2. by uncommenting (ie, removing ;; at the beginning of) the
(setq TeX-engine 'default-shell-escape)

line in the previous Elisp code.

Use the default-shell-escape engine on a per-document basis

Alternatively, you can leave the default engine as it is and pick up the default-shell-escape engine only in the documents in which you actually need it. In those documents, issue M-x add-file-local-variable RET TeX-engine default-shell-escape RET.


To summarize the answer by @giordano for use by an Elisp beginner, you should add this to the bottom of your TeX document:

% Local Variables:
% TeX-command-extra-options: "-shell-escape"
% End:

And you should add this to your init-file:

(with-eval-after-load 'tex
  (add-to-list 'safe-local-variable-values
               '(TeX-command-extra-options . "-shell-escape")))

To make the changes take effect without restarting Emacs, you should run M-: (load user-init-file) RET and then type M-x normal-mode in your TeX buffer.

Note that this change to your init-file means that compiling malicious TeX code using AUCTeX can run arbitrary shell commands, so be careful.

Tags:

Emacs

Auctex