Using minted (source code LaTeX package) with emacs/auctex
Asked Answered
C

4

14

As is explained in here, I find minted package is pretty cool for source code listing.

My question is how to use minted package with AucTeX/emacs? For command line I can use pdflatex -shell-escape SOURCE, but

  • Q1 : How can I modify the AucTeX to insert the -shell-escape? I mean, how to change the action for C-c+C-c?
  • Q2 : Do I need special key other than C-c+C-c for -shell-escape option? Or, is it just OK to use it without any problem?
  • Q3 : What is the -shell-escape for?
Continuation answered 21/7, 2010 at 14:45 Comment(0)
A
14

Q1: You need to edit the way LaTeX is called by AucTeX. One way of doing this is to add the following to your .emacs file:

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

Q2: Once you have made the changes, all calls to LaTeX with C-c C-c will use the -shell-escape option.

Q3: See Konrad's answer. Note that this method will enable -shell-escape for all files edited in AucTeX, so can be a potential security risk if using other peoples packages or files.

Aluminum answered 22/7, 2010 at 12:37 Comment(1)
@Mike's answer at the bottom provides a simpler and less intrusive way to do the same in modern releases of AucTeX.Effective
C
11

In recent versions of auctex, it looks like it'll be more reliable to set TeX-command-extra-options, which is designed for just this purpose, and doesn't make you override the various forms of TeX-command. As I understand it (might be wrong), this can't be set globally, but must be set for each file. You can do this with a hook. For example, in .emacs you might add this:

(add-hook 'TeX-mode-hook
  (lambda ()
    (setq TeX-command-extra-options "-shell-escape")
  )
)

And since you don't fully overwrite the latex command call, other features will still work -- like turning on synctex support with (setq TeX-source-correlate-mode t) [which can happen outside the hook].

Choragus answered 26/11, 2014 at 18:7 Comment(3)
You can also add it to your .dir-locals.el to set it for all files in one folderEffective
I tried setting TeX-command-extra-options as a buffer local variable in Emacs 24.5.1 (Ubuntu 16.04), but that didn't seem to work. How recent a version do you need?Reichenberg
@Clément: That was helpful! I created an answer that uses your tip about .dir-locals.el so that shell commands will only be allowed in latex documents that actually need it.Shaped
A
8

I can only answer question 3:

What is the '-shell-escape' for?

minted uses a third-party application, pygmentize, to process the source code. LaTeX usually doesn’t allow calling other applications for security reasons (a rogue package could otherwise call aribtrary code). To explicitly enable calling external applications, you need to enable this so-called escape to the shell – which, on most LaTeX installations, is done via the -shell-escape switch.

Arnie answered 21/7, 2010 at 16:30 Comment(0)
S
1

Use this .dir-locals.el file

((latex-mode . ((TeX-command-extra-options . "-shell-escape")
                (tex-start-options . "-shell-escape")
                (eval
                 . (setq-local tex-verbatim-environments
                               (cons "minted" tex-verbatim-environments))))))

Discussion

Put the above .dir-locals.el file in the same directory as the latex files which use minted.

This causes Ctrlc Ctrlc to use the -shell-escape option. The first line sets it up for AUCTeX and the second sets it for emacs's builtin latex-mode, when AUCTeX is not installed.

The remainder of the file hints to emacs that the "minted" environment should be styled like a verbatim environment. Normally emacs's latex-mode displays the character after an underscore as a subscript, like_this_for_example.

Why not the .emacs file?

Using .emacs is convenient, but risky. Enabling -shell-escape globally is considered dangerous — there's a reason it is not on by default — and it should be used sparingly. Using a .dir-locals.el file instead allows one to choose to enable -shell-escape only for the documents that actually need it.

Shaped answered 3/9, 2023 at 22:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.