# Customizing pdfLaTeX for a particular file with AUCTeX?

I'm trying to use the TikZ 'external' library, available as of PGF 2.10. This requires running pdfLaTeX with the option -shell-escape. I usually run pdfLaTeX from AUCTeX with the option

``````(TeX-PDF-mode t)
``````

So C-c C-c runs pdfLaTeX. Can I add a line to my LaTeX file so that AUCTeX will know to run pdfLaTeX with the -shell-escape option for this file only, seeing as this is apparently a security issue, and only partially enabled by default? I don't know if this is possible, but similar things are possible. See for example the tex.sx question Force different TeX engine for specific files. If possible this would presumably use file variables.

If not, I know it is possible to customize it globally for all files in .emacs. Currently, I'm not sure how to do that either. Preferably via M-x customize, but I find those menus quite confusing

See for example http://thread.gmane.org/gmane.emacs.auctex.general/538 where David Kastrup seems to suggest that the per file approach is possible. Ie.

"Anyway, you could use the file variable section to change the command just in those files where you want it."

UPDATE: Based on input from the auctex mailing list, the answers suggested by people here should work. It is not clear why it doesn't, but the problem may be specific to the Debian package. Marcus Frings pointed me to his bug report in the thread above. The bottom line is there appears to be a problem with the Debian package, which is not present in vanilla auctex, and nobody knows why. The maintainer, who one would expect to look into this, shows no interest in doing so, unfortunately.

-
I have the impression that your addendum would count as an answer - is this correct? If yes, then it's a lot better if you post it as an actual answer to your own question. Then you can remove the addendum from the question. As a general rule, post only questions in questions and answers in answers. (If you think your addendum is an update of the question, then it would be best if you clarify this, I think.) –  Hendrik Vogt Feb 28 '11 at 18:24
@Hendrik: It's a (partial) answer, yes. Posting as an answer now. –  Faheem Mitha Feb 28 '11 at 18:32

Please be aware that limited FS access is allowed without shell-escape in TL10, using `\write18`. Miktex probably has something similar.

I add the following to files, which are always to be shell-escaped.

``````%%% LaTeX-command: "latex -shell-escape"
``````

I also have the following snippet in my .emacs for adding shell escape on the go:

``````;; toggle shell escape using C-c C-t C-x
(defun TeX-toggle-escape nil (interactive)
"Toggle Shell Escape"
(setq LaTeX-command
(if (string= LaTeX-command "latex") "latex -shell-escape" "latex")))
(lambda nil
(local-set-key (kbd "C-c C-t x") 'TeX-toggle-escape)))
``````

Edit: here's a nicer function which tells you what is going on:

``````;; toggel shell escape using C-c C-t C-x
(defun TeX-toggle-escape nil (interactive)
"Toggle Shell Escape"
(setq LaTeX-command
(if (string= LaTeX-command "latex") "latex -shell-escape"
"latex"))
(message (concat "shell escape "
(if (string= LaTeX-command "latex -shell-escape")
"enabled"
"disabled"))
))
(lambda nil
(local-set-key (kbd "C-c C-t x") 'TeX-toggle-escape)))
``````
-
@Rasmus: Welcome to tex.sx! Note that it is unusual around here to sign your questions (as there is already a box with your username below it) or to have any greeting. –  lockstep Feb 27 '11 at 21:36
@lockstep: Personally, I'm +1 on greetings and salutations. They're friendly. I particularly like the German MfG. Is omitting these a a generally followed custom in SE sites? If so, what is the rationale? –  Faheem Mitha Feb 27 '11 at 21:56
@Faheem: Seems to have been agreed on "long ago and far away. –  lockstep Feb 27 '11 at 22:03
@Rasmus: Welcome to tex.sx, and thanks for the answer. I tried both your approaches both together and singly, and neither work for me. I get exactly the same error messages with them as I do without them. Assuming I am doing something wrong, do you have suggestions to debug? Thanks. –  Faheem Mitha Feb 28 '11 at 8:41
@Rasmus: I compared the logs when running via command line vs from emacs using AUCTeX, and AUCTex is not using that option. Could it be because I'm using pdflatex? –  Faheem Mitha Feb 28 '11 at 11:30

The global customization is done by entering

``````M-x customize-variable RET LaTeX-command RET
``````

(as David Kastrup described in http://thread.gmane.org/gmane.emacs.auctex.general/538) and changing the LaTeX-command variable. He didn't say what the variable should be changed to, but the answer is from the default, which is

``````latex
``````

to

``````latex -shell-escape
``````

If this is set and saved, it appears in .emacs as

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

in the `custom-set-variables` list.

I still don't know how to do this on a per file basis.

UPDATE: The preceding approach of setting `latex-command` doesn't work with `xelatex` for me, but the approach in this answer to "Using minted (source code LaTeX package) with emacs/auctex" does, namely adding

``````(eval-after-load "tex"
'(setcdr (assoc "LaTeX" TeX-command-list)
'("%`%l%(mode) -shell-escape%' %t"
TeX-run-TeX nil (latex-mode doctex-mode) :help "Run LaTeX")
)
)
``````

to your `.emacs` file.

Of course, setting this per file using local variables would be preferable to either method, because enabling `-shell-escape` globally is unnecessary.

-

Solution 1: use local variables. Insert the following at the end of a LaTeX file you want to allow to use shell escapes:

``````%%% Local Variables:
%%% LaTeX-command: "latex -shell-escape"
%%% End:
``````

Note that this is only applied once the file is closed and re-opened in emacs. (you need the "Local Variables:" and "End:" line).

Solution 2: use LaTeX-command-style to automagically turn on shell escapes for every file in which you use the pgf package (a little more dangerous?). Put the following in your .emacs:

``````(setq LaTeX-command-style
'(("^pgf\$" "%(PDF)%(latex) -shell-escape %S%(PDFout)")
("" "%(PDF)%(latex) %S%(PDFout)")))
``````

You may also use the emacs customization system to set this up.

-
Thanks for your answer and welcome to tex.se. I just checked your first answer, and I think you may be right that the "Local Variables" and "End" bit are necessary. If I add them, emacs warns me the "shell-escape" is a "risky" setting when opening the file. Without it, I don't see anything. However, it still doesn't work. Suggestions for debugging? –  Faheem Mitha Mar 11 '11 at 21:15
I think the fact that you are getting warned that it's risky is an indication that emacs/AUCTeX is setting that variable. Of course you answer "y" to allow it, right? To see if it has really taken hold, try looking at the output buffer after you run LaTeX. Go to the top of that buffer, and you'll see a line that tells you exactly what command was run: `Running LaTeX on myfile with pdflatex -shell-escape -interaction=nonstopmode "\input" myfile.tex` (I stripped some quotes). If the '-shell-escape' isn't there, it's not getting picked up. –  David Andrews Mar 12 '11 at 5:24
I agree that the fact I am getting warned is an indication that Emacs/AUCTeX is at least trying to set the variable. Yes, I answer "y". However, it is not getting set, as one would infer from the error. I get `Running `LaTeX' on `snppy' with pdflatex -interaction=nonstopmode "\input" snppy.tex`. Any idea what might be going wrong? –  Faheem Mitha Mar 12 '11 at 8:16

Solution 1 of Hendrik Vogt does not work for me any longer (emacs 23.3.1, AUCTeX-version 11.86). Instead I use the synctex-flags:

``````%%% Local Variables:
%%% TeX-source-correlate-mode: t
%%% TeX-synctex-tex-flags: "-synctex=1 -shell-escape"
%%% End:
``````
-
Henrik didn't answer the question. I think maybe you mean David Andrews answer. In any case, can you elaborate on what synctex-flags are, and how they relate to the question? I've never heard of them before. –  Faheem Mitha Apr 1 '12 at 19:58