5

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

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.

  • I'm pretty sure this happens automatically if file parsing is enabled. It may be worth mentioning it – giordano Feb 3 '17 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 Feb 3 '17 at 19:48
  • Which version of AUCTeX are you using? – giordano Feb 3 '17 at 19:54
  • @giordano 11.90.0 – Tyler Feb 3 '17 at 20:27
  • Ok, I'll have a look in the next days. – giordano Feb 3 '17 at 20:36
3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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