5

I am using LaTeX-Mode and need to include some listings which contain dollar-signs:

\begin{lstlisting}[caption=Meteor Server: Bereitstellen einer entfernten Prozedur,label=lst:meteor-server-publish-rpc]
Meteor.Methods({
  addStar: function(messageId) {
    Messages.update(messageId, {
      $inc : { "stars" : 1 }
    });
  }
});
\end{lstlisting}

Sadly, this breaks syntax highlighting as emacs treats the contents of the listing block as "normal" LaTeX code.

emacs with incorrect syntax highlighting

Is there any way to tell emacs not to ignore certain blocks? I know I could use an external file and use that, but I would prefer to keep my source together.

This is running GNU Emacs 24.5.1 and AUCTeX 11.89.

  • Do you have file parsing enabled? If so, syntax highlighting would be correct. If not, add the lines suggested at the beginning of the AUCTeX manual: (setq TeX-auto-save t) (setq TeX-parse-self t) (setq-default TeX-master nil) (and read at least the "Quick Start" section of the manual, while you're at it) – giordano Dec 20 '15 at 11:56
  • That was a good read, but for some reason the problem persists even after customizing that variables. – Marcus Riemer Dec 20 '15 at 14:27
  • What's the value of the TeX-active-styles variable (C-h v TeX-active-styles) in your .tex file buffer? The line suggested by JeanPierre is already in the style file associated to listings package, you shouldn't need to add it manually. Which version of AUCTeX and Emacs are you using? – giordano Dec 20 '15 at 16:28
  • Sorry for the possibly stupid question but hem, that was my problem. Do you actually \usepackage{listings} with the correct spelling? – JeanPierre Dec 20 '15 at 17:03
  • @giordano I think I sorted it out (it does work like it should now for me) and updated my answer. Hope this helps! – JeanPierre Dec 20 '15 at 21:05
6

Syntax highlighting would be correct if file parsing is enabled. First check whether the options suggested at the beginning of the AUCTeX manual are enabled:

(setq TeX-auto-save t)
(setq TeX-parse-self t)
(setq-default TeX-master nil)

In general AUCTeX should not highlight verbatim-like environments. How does it know these envs? Section 3.1.4 of AUCTex documentation says:

3.1.4 Verbatim macros and environments
Usually it is not desirable to have content to be typeset verbatim highlighted according to LaTeX syntax. Therefore this content will be fontified uniformly with the face `font-latex-verbatim-face'.

font-latex differentiates three different types of verbatim constructs for fontification. Macros with special characters like | as delimiters, macros with braces, and environments. Which macros and environments are recognized is controlled by the variables LaTeX-verbatim-macros-with-delims, LaTeX-verbatim-macros-with-braces, and LaTeX-verbatim-environments respectively.

So you can tell AUCTeX about a new verb-like env by adding to your init file something like:

(eval-after-load 'latex '(add-to-list 'LaTeX-verbatim-environments "myverb"))

However, verb-like envs of common latex packages (including lstlistings from the listings package) can be automatically detected and 'styles' applied. By parsing your file, AUCTeX sees the \usepackage{listings} and so adds lstlistings to its list of verb-like envs.

AUCTeX parses the file only if TeX-parse-self is set. If TeX-auto-save is set, it saves the found information in the auto local directory. Once the file exists, it gets the information from it and does not re-parse the file.

To make it parse again (eg because your file changed), run C-c C-n (TeX-normal-mode) (I found that I sometimes need to run it twice). Actually, you can dispend setting any variable and just type C-c C-n when you want your file to be parsed and styles applied.

  • This is correct in general, for a new verbatim-like environment, but in this concrete case AUCTeX already adds lstlisting to the list of verbatim environments, so maybe there are some problems in the configuration of Marcus Riemer. – giordano Dec 20 '15 at 16:25
  • 1
    @giordano It seems I have the same problem, lstisting is not added to the list (debian package auctex 11.87.3+deb8u1)., even with your suggestion applied. Adding (eval-after-load 'latex '(add-to-list 'LaTeX-verbatim-environments "lstlisting")) to my init file does work. – JeanPierre Dec 20 '15 at 16:42
  • Please, answer these questions. – giordano Dec 20 '15 at 16:43

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.