The title really says it all, so I'll provide an example and a screenshot:


For example, if we wished to create a predicate
  to determine if a node could mark itself
  (considering the \Algorithm{IndSet} problem),
  we could put~\autoref{lst:logic:pred-def}
marked = v['marked']
neighbor_marked = any(map(lambda n: n['marked'], N))
return not (marked or neighbor_marked)

It's worth noting that Emacs can't find the opener for the closing ].

  • I suppose writing all the options in the same line isn't an option, right? – giordano Apr 4 '14 at 18:46
  • @giordano It's technically an option, but not in the scope of this question. My captions especially are often a little longer (no more than a sentence or two, but still) and benefit from line breaking for the VCS. Labels also change frequently to avoid clashes, as do other options. I was thinking maybe altering the regular expression to say something like \\begin{lstlisting}([.*])?, but I don't know where I'd put it (or if this would work, to be perfectly honest). – Sean Allred Apr 4 '14 at 18:47
  • 1
    I see. Unfortunately for the time being I have no solution, font locking isn't my specialty. Anyway, try hacking font-latex-set-syntactic-keywords. – giordano Apr 4 '14 at 18:59

Syntax highlighting of verbatim-like environments with optional arguments was fixed upstream and works out-of-the-box since AUCTeX 11.88. There is no more need to apply the hack I previously suggested in the original answer (see below).

If you have file parsing enabled (which is highly recommended anyway, in order to exploit all features of AUCTeX) and you loaded listings package in your document, AUCTeX will automatically recognize lstlisting as a verbatim-like environment and highlight it correctly, including the optional argument.

enter image description here

This works also if there is a pair of brackets inside the optional argument (for example the dialect of the language):

enter image description here

or if the newline after the \begin{environment} is commented by a % character

enter image description here

Original answer (needed only for AUCTeX 11.87 or previous versions)

Inside the font-latex-set-syntactic-keywords function, defined in font-latex.el, replace

(add-to-list 'font-latex-syntactic-keywords
             `(,(concat "^[ \t]*\\\\begin *{\\(?:" verb-envs
           (1 "|" t)))


(add-to-list 'font-latex-syntactic-keywords
         "^[ \t]*\\\\begin *{\\(?:" verb-envs "\\)}[\t ]*\\(?:%.*\n[\t ]*\\)?"
           (1 "|" t)))

There's a nicer solution in this StackExchange post here

Just add

(setq LaTeX-verbatim-environments-local '("Verbatim" "lstlisting"))

To your .emacs file to make AUCTeX to treat lstlisting as verbatim. It worked well for me and no need to hack the auctex-package source.

  • 1
    please fix the URL, it's broken – naphaneal Dec 21 '16 at 16:25
  • Should be fixed now. Sorry. – Adam A. Dec 21 '16 at 16:30
  • If file parsing is enabled, this is not necessary as the style file of listing package does it automatically. LaTeX-verbatim-environments-local isn't meant to be customized by users, though it's not forbidden. It is also worth mentioning that this answer works only because someone installed the code suggested in the other answer to upstream AUCTeX. – giordano Dec 21 '16 at 17:20

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.