[Tex/LaTex] Adding an option to the PDFLaTeX call from AUCTeX

auctexemacs

In my .emacs I have

'(TeX-PDF-mode t) 

set. So I'm using PDFLaTeX. I'm not sure why this is not the default these days.
I had also set

'(LaTeX-command "latex -shell-escape")

In hindsight this was a bad idea, but I didn't realise it till
I was trying to get SyncTeX to work, but it wouldn't.
I belatedly realized that because I had set LaTeX-command manually,
I was overwriting the

--synctex=1

option that would normally have been there.

Which brings to my question.

  1. Is LaTeX-command the right command to modify to add the
    -shell-escape option, and if so
  2. What is the right way to add an option to LaTeX-command?

@David Carlisle suggested

(setq LaTeX-command (concat LaTeX-command " -shell-escape"))

which seems to be in the right ballpark, but this gives the error:

 Symbol's value as variable is void: LaTeX-command

NOTE: Evaluating LaTeX-command just gives "latex", so this may not be the right thing to append to.

Best Answer

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.