5

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.

  • Is simply splitting the input line an option? If so, the 'normal' solution to this type of problem is a strategically-placed comment with the 'matching' item in it. – Joseph Wright Mar 30 '14 at 8:09
  • I'm not sure if understand your tip corretly (splitting the input line?) but I already tried to solve the problem with a comment and it didn't worked. A custom comment command which Kile doesn't recognize as a comment might be a solution. – Steven Thiel Mar 30 '14 at 13:10
  • End of the line would be 'wrong' due to the braces, hence asking about splitting the line so you have {$, % $ <newline> \[, \], .... I'm not a Kile user so I can't check if it respects this. – Joseph Wright Mar 30 '14 at 14:10
  • The problem is Kile marks all following text green not only the line with $ and it ignores %$. – Steven Thiel Mar 30 '14 at 20:09
  • @JosephWright Is there a way of telling TeX to throw away the next token? Kile is too 'clever' - it ignores anything after a comment sign. I want to use $ with l3regex, but it turns all remaining content in my .cls magenta! And obviously repetition isn't an option in this case. (I know I can use \Z but that is much less readable for me. – cfr Jan 19 '17 at 23:09
4
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 ...

| improve this answer | |
  • 1
    Just use \use_none:n $. (And in regexes if I remember correctly \Z is equivalent to $ except perhaps for subtleties with linefeeds that I never implemented. – Bruno Le Floch Jul 13 '17 at 1:46

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.