6

I am using AUCTeX to edit Latex files and use the minted package to highlight source-code within documents. Unfortunately the character $ is still recognized as a delimiter for a math-mode even though it is located within a minted-block and should have no special meaning whatsoever.

enter image description here

This totally breaks syntax-highlighting and makes emacs think I am editing in math-mode although I am not.

Is it possible to configure AUCTex in such a way as to give characters within a minted environment no special meaning? Or do I have to apply some sort of quoting to $?

4 Answers 4

5

You can add minted environments to the latex-verbatim-environment variable:

M-x customize-variable latex-verbatim-environments, insert a new string "minted", and save your changes. You may need to reload your .tex file for the changes to take effect.

After doing this, minted environments will not be fontified at all, so it won't matter if you include $ symbols.

6
  • I'm pretty sure this happens automatically if file parsing is enabled. It may be worth mentioning it
    – giordano
    Commented Feb 3, 2017 at 19:23
  • @giordano I have enabled both TeX-parse-self and TeX-auto-save, and still needed to manually add minted to latex-verbatim-environments.
    – Tyler
    Commented Feb 3, 2017 at 19:48
  • Which version of AUCTeX are you using?
    – giordano
    Commented Feb 3, 2017 at 19:54
  • @giordano 11.90.0
    – Tyler
    Commented Feb 3, 2017 at 20:27
  • Ok, I'll have a look in the next days.
    – giordano
    Commented Feb 3, 2017 at 20:36
4

I'd like to give an answer which is complementary to the one given by @Tyler.

I'd recommend that you set this in your init file:

(setq TeX-parse-self t)

restart your Emacs and open your .tex file again. The difference is that AUCTeX provides a support file minted.el for minted package which does a lot more besides fixing the fontification in environments and macros provided by minted package. You can hit C-c C-e minted RET and will be asked for optional argument and the language for your code; both with auto-completion. Your .tex file could look like this:

\documentclass{article}

\usepackage[newfloat]{minted}

\begin{document}

\begin{minted}{bash}
$ echo "Hello"
$ echo World
\end{minted}

\begin{Minted}
  $ echo "Hello"
  $ echo World
\end{Minted}

\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% TeX-command-extra-options: "-shell-escape"
%%% End:

Note that Minted is just an arbitrary environment to show the difference that code will be indented with 2 spaces where spaces are significant. Here a screenshot for the fontification:

enter image description here

The line

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

allows you to use C-c C-a in Emacs and your file gets compiled correctly with minted.

0

I still had this problem with a recent distribution of AUCTex, so I looked for an answer. The one given here did not solved my problem. Yet, I added the following command to my .emacs file and everything went fine:

(setq LaTeX-verbatim-environments-local '("minted"))

I hope this might help some people.

0

As a further alternative, you can give give your blocks their natural highlighting and major-mode using mmm-mode.

For example, if you evaluate the following elisp code, load up a tex file and do M-x mmm-mode, then \begin{minted}{bash} blocks behave as if they were in bash-ts-mode:

(require 'mmm-auto)
(require 'mmm-region)

(defvar tex-bash-mode-map (make-sparse-keymap)
  "Keymap for `tex-bash-mode'.")

(define-minor-mode tex-bash-mode
  "Minor mode for minted blocks with bash submode.
This is not intended to be activated manually."
  :init-value nil
  :lighter " MB"
  :keymap tex-bash-mode-map)

(defun tex-bash-initialize ()
  "Initialize `tex-bash-mode'.
Define the `latex-minted-bash' class and add it to `LaTeX-mode'."
  (mmm-add-classes
   '((latex-minted-bash
      :submode bash-ts-mode
      :face mmm-default-submode-face
      :front "\\\\begin{minted}{bash}"
      :back "\\\\end{minted}"
      :save-matches 1
      :insert ((?b bashcode nil @ "\\begin{minted}{bash}" @ "\n" _ "\n" @ "\\end{minted}" @)))))
  ;; (mmm-add-mode-ext-class 'latex-mode "\\.tex\\'" 'latex-minted-bash)
  (mmm-add-mode-ext-class 'latex-mode nil 'latex-minted-bash)
  ;; AUCTeX 13.3+:
  (mmm-add-mode-ext-class 'LaTeX-mode nil 'latex-minted-bash))

(defun tex-bash-enable ()
  "Enable `tex-bash-mode' in the current buffer."
  (setq-local indent-line-function #'tex-bash--indent-line-narrowed)
  (tex-bash-mode 1))

(defun tex-bash-disable ()
  "Disable `tex-bash-mode' in the current buffer."
  (tex-bash-mode 0))

(defun tex-bash--indent-line-narrowed ()
  "An indent function which works on some modes where `mmm-indent-line' doesn't.
Works like `mmm-indent-line', but narrows the buffer before
indenting to appease modes which rely on constructs
like (point-min) to indent."
  (interactive)
  (let ((indent-function
         (save-excursion
           (back-to-indentation)
           (mmm-update-submode-region)
           (get
            (if (and mmm-current-overlay
                     (> (overlay-end mmm-current-overlay)
                        (point)))
                mmm-current-submode
              mmm-primary-mode)
            'mmm-indent-line-function))))
    (if mmm-current-overlay
        (save-restriction
          (narrow-to-region (overlay-start mmm-current-overlay)
                            (overlay-end mmm-current-overlay))
          (funcall indent-function))
      (funcall indent-function))))

(tex-bash-initialize)

(add-hook 'mmm-bash-mode-enter #'tex-bash-enable)
(add-hook 'mmm-bash-mode-exit #'tex-bash-disable)

Here's what it looks like:enter image description here

(The alignment is wrong, but hitting tab would correct it.)

You can do this for each programming language that you use. The above code is adapted from my package czm-tex-mint, which does something similar for minted sage blocks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.