[Tex/LaTex] Kile syntax highlighting: A single dollar sign in the preamble

editorskilesyntax highlighting

I'm using Kile to edit my LaTeX documents. I define my own custom environment to hightlight LaTeX code named lstLaTeX with the following code:

\documentclass[a4paper]{scrartcl}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage[latin1]{inputenc}
\usepackage[ngerman,english]{babel}

\usepackage{listings}

\definecolor{lightgrey}{rgb}{0.9,0.9,0.9}
\definecolor{darkgreen}{rgb}{0,0.6,0}

\lstnewenvironment{lstLaTeX}
  {
  \lstset{language=[LaTeX]TeX,
    keepspaces=true,
    texcsstyle=*\bf\color{blue},
    basicstyle=\ttfamily,
    numbers=none,
    breaklines=true,
    keywordstyle=\color{darkgreen},
    commentstyle=\color{red},
    morekeywords={},
    otherkeywords={$, \{, \}, \[, \]},
    frame=none,
    tabsize=2,
    columns=fullflexible,
    backgroundcolor=\color{lightgrey},
    escapechar=°
    }
  }
  {}


\begin{document}
The actual document.
Here some LaTeX code:
\begin{lstLaTeX}
Brackets should be {\bf highlighted}.
The dollar sign: $x=5$
\end{lstLaTeX}

\end{document}

The problem is that Kile cannot deal with the single dollar sign in the preamble and marks all following text green (because it thinks there should be a math environment).

enter image description here

I already read how to teach Kile to ignore dollar signs when used inside custom environments here: disable syntax highlighting in kile
But this post doesn't solve my problem.

So it would be nice if I could tell Kile to ignore this single dollar sign. I already tried to add %$ at the end of the line with the single dollar sign but Kile ignores this.

Best Answer

otherkeywords={$, $, \{, \}, \[, \]},

seems to work. The repetition doesn't seem to bother anything when compiling and it makes Kile happy.

EDIT

A similar problem, which cannot be worked around in the same way, occurs if using $ with l3regex, for example.

The following function

\cs_new_protected_nopar:Nn \prefix_gobble_token:n
{
  \relax
}

allows the $ to be matched, limiting the highlighting damage to the close vicinity of the offending token by adding

\cfr_gobble_token:n { $ }

close by.

However, I have no idea how safe or otherwise this might be ...

Related Question