Syntax highlighting: LaTeX

When I prepared this blog, the first plugin I looked for was a syntax highlighter. I finally chose the Crayon Syntax Highlighter.

Looked right, efficient, got good comments, … Only problem is: TeX is not supported, and I might post about $latex \LaTeX&bg=000000&fg=999999$ (actually, I already have).

But luckily enough, you can define your own languages, via some well-chosen regular expressions. And I did!

Demo

Here is a sample of $latex \LaTeX&bg=000000&fg=999999$ code with the highlighting I defined.

Quite promising, isn’t it?

Imperfections

OK, there are some glitches:

Comments from the beginning of line

Comments starting at the beginning of the line are not accounted for comments.  It does not seem to come from my regexes, but I do not understand why the Crayon plugin would do this. I’ll try to dig it out or see with Crayon’s developer.

Backticks

Some character sequences look weird. French grave accent, for instance, but I cannot even insert it in post (backslash followed with a backtick: backslash disappears in Crayon and backtick outside the Crayon block has a weird effect on WordPress’s post). I think this is a glitch either from the plugin or WordPress, but once more, nothing pops into my mind that would explain this. Once more, I should try to see with Crayon’s developer.

Edit (06/13/2012): I figured that one out: to WordPress, the backtick  is used to wrap code.

As I try to use escaping with backslashing, I find how tricky the use of the backtick is in WordPress and I have no clear solution about how to make a single backtick appear. Only one seems sure enough to work: use an odd number of backticks, but then, I am unsure which one will be retained as being lonely… Still, here is the one in this post: `

And in my Crayon highlighted block, all I had to do was to precede it with two backslashes (though I do not really understand why).

Is it worth disturbing them, though? $latex \TeX&bg=000000&fg=999999$ was not supported because nobody ever asked for it. So, if I am to be the only one to use Crayon for $latex \TeX&bg=000000&fg=999999$, I should be satisfied with what I got already.

Code repetition?!

Edit (06/13/2012): Does it come from my regexes? It appears my TeX code is repeated under the Crayon highlighted block and I cannot do anything to it when writing… Will try to look into that later…

Edit (07/31/2012): Akarmenia corrected that bug in the latest Crayon version. Quite good!

How to do the same

Edit (06/13/2012): Nothing to do anymore! Akarmenia has included my work into the Crayon plugin. Just install it and enjoy!

If on the other hand, you want to install that language on your own Crayon Syntax Highlighter plugin, here is the way to proceed.

Go into your /wp-content/plugin/crayon-syntax-highlighter/langs directory and create a tex directory. Into that newly created directory, create a tex.txt file. Here is the content for the file:

This is really basic, but it seems to do the work.

Any feedback is welcome, from regex enhancement suggestions to LaTeX code samples which are not properly highlighted. Do not hesitate to comment, I will see what I can do.

Edit: I tweeted about that:

Cyrille Chopelet

Programming addict, UI design philosopher, casual gamer, sci-fi enthusiast, hi-tech dilettante, ... Some people even call me a geek. See this page for more information about me.

More Posts - Website

Follow Me:
TwitterFacebookLinkedInGoogle Plus

2 thoughts on “Syntax highlighting: LaTeX”

  1. Nothing to do anymore: akarmenia has added my file to the package: TeX highlighting will be supported by default when you install the Crayon Syntax Highlighter.

    Though I have seen a mistake I made and I am ashamed of… I’m going to correct this right now.

"Wit beyond measure is man's greatest treasure." − Rowena Ravenclaw