# How can I fix AucTeX's syntax highlighting for the lstlisting environment when using optional arguments?

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

\documentclass{article}
\usepackage{listings}
\begin{document}
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}
\begin{lstlisting}[
float,
caption={caption}
label={label}
]
marked = v['marked']
neighbor_marked = any(map(lambda n: n['marked'], N))
return not (marked or neighbor_marked)
\end{lstlisting}
\end{document}


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
• 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.

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

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

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 "\$$}.*\$$\n\$$")
(1 "|" t)))


with

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


There's a nicer solution in this StackExchange post here

(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.
• 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