[TxMt] Bugs in LaTeX highlighting

Charilaos Skiadas skiadas at hanover.edu
Sun Oct 15 01:10:10 UTC 2006


On Oct 14, 2006, at 5:59 PM, Trevor Harmon wrote:
> With the default Mac Classic theme, this environment is supposed to  
> have a light blue background. However, while writing a recent  
> paper, the highlighting often did not work the way it's supposed  
> to. To demonstrate the problem, I've attached three LaTeX files.  
> The rightway.tex file shows how the highlighting is supposed to be;  
> the wrongway1.tex and wrongway2.tex files show incorrect  
> highlighting. Is there a simple fix for these problems?
>

I fixed the problem with wrongway2.tex. Basically the rule for  
lstlisting environments was matching starting at the \begin 
{lstlisting} part. However, the generic rule was matching earlier,  
because it was also matching the spaces in front of the \begin, if  
any. So the lstlisting rule didn't have any chance.

The second problem you are having doesn't have a very elegant  
solution. The problem is that one needs to specify what language the  
code is to be highlighted as. Right now, it assumes the code is  
Python code, basically because the developer who added the rule  
needed that. This is what causes your other problems (for instance  
"is" is a keyword in Python), and there is no elegant fix for this  
atm. We could perhaps come up with some convention, like having to  
precede the \begin{lstlisting} line with a command line like:

  # Java code

and so on. Then we could put a couple of rules in place, each  
matching one of the common languages, though as you can imagine this  
can quickly get out of hand and bloat the grammar to twice its size  
at least.

To "fix" this locally, you have two options. The one is to change the  
LaTeX language grammar, so that the rule matching lstlisting changes  
to include your preferred language instead. This will cost you losing  
any updates to the language grammar that may come.

A more robust solution is to create a new grammar, perhaps called  
JavaLaTeX or some such, which first has a rule for an lstlisting  
environment, essentially copying the one from the LaTeX bundle and  
changing the include rules in it. After this one rule, JavaLaTeX  
simply includes the LaTeX grammar.

> Thanks,
>
> Trevor

Haris





More information about the textmate mailing list