I just saw this at Herbert answer to this question: if a code block contains \\ directly followed by a text then the syntax highlighting marks it as macro. This is quite irritating and should be fixed. Most likely the regular expression for macros misses a look-behind instruction.

Another example where the regular expression should be improved, added by Hendrik: In this answer it is probably hardly avoidable that \MikTeX is interpreted as a control sequence, but at least the 2.9 shouldn't be part of the control sequence. See also this question, where it looks as if \ifnum0 was the control sequence. Or is this intentional?

Proposed Solution

I updated the JavaScript syntax file for LaTeX (New Version) which now highlights word with starts with \ and are followed by a-z, A-Z and @ OR followed by any other single character (e.g. \\, \_) as macros.

Here an example result (as slightly magnified image):

Example of new syntax highlighting

I would ask people to vote on this version, so that it can be installed on tex.sx and maybe also on meta. See my answer below for further information and the discussion on the previous versions of the above file.

  • 1
    Good that you brought this up. I added another similar possible bug; hope that's OK. Jan 30 '11 at 7:48
  • 1
    The new version works well. Thank's for the work! Could you please accept your answer (an maybe a mod could tag the question as status-completed).
    – Caramdir
    Feb 11 '11 at 21:50
  • @Caramdir, @Hendrik: The fix for the preview mode still needs to be added too.
    – Martin Scharrer Mod
    Feb 12 '11 at 12:26
  • @MartinScharrer - If this is resolved, do you mind adding a status-completed to the question?
    – Oded
    Jul 12 '13 at 9:50
  • @Oded: Thanks for pointing this out. Now done.
    – Martin Scharrer Mod
    Jul 12 '13 at 10:45

We use Google Prettify with a hint for the Tex language:

<pre class="lang-tex prettyprint"><code>

We have implemented TeX prettify highlighting using a user contributed plugin found here. A previous discussion on how this was done can be found here. We have reported this issue so hopefully a contribution will be made that will resolve it soon.

  • @Geoff: Could you please update the Prettify plugin to the last version found in that bug report. That offers a vast improvement over the current highlighting.
    – Caramdir
    Feb 10 '11 at 21:16
  • 1
    @Caramdir I have committed the latest version located on the bug report. You should see this on the next build. Feb 10 '11 at 21:51
  • @Geoff: Great! .
    – Caramdir
    Feb 10 '11 at 22:21

I now modified the mentioned plugin by myself and uploaded it to the issue tracker.

I changed the regular expressions for macros from

[PR.PR_KEYWORD, /^\\[^ \r\n{}\[\]=]+/],


[PR.PR_KEYWORD, /^\\(\\|[^ \r\n{}\[\]=]+)/],

so telling the prettifier that \\ is a macro by itself.

Please update it on tex.se. Thanks!


I updated the plugin as requested with only letters, '@' and also ':' for macros plus single character sequences.

An additional alternative also adds special highlighting for declarations and the $ and & characters.

Update 2

Bugfixed version of plugin with extra highlighting Fixes wrongly highlighted macros which start with \def etc.

Demo version

I now wrote a small greasemonkey script (a firefox plugin which allow you to run your own JS files on websites you visit) to enable the new syntax highlighting. If someone has interest to try it out, here is the script. It also works on meta and in addition on inline code tags.

  • Maybe it is easier to just allow letters and @ since (usually) everything else terminates the the macro name. And then there should be a separate case for control characters.
    – Caramdir
    Jan 30 '11 at 20:39
  • I had the same reaction when I saw the current regex, but the updated version seems to work well enough.
    – Martin Scharrer Mod
    Jan 30 '11 at 23:32
  • 2
    No, there are still lots of case left where it does the wrong thing. For example \sin(t)+x would highlight everything.
    – Caramdir
    Jan 31 '11 at 1:47
  • 1
    @Caramdir: Good example. You can see it happen in this answer; even the closing dollar gets highlighted there. Jan 31 '11 at 8:50
  • Ok, I see. I can change it to letters, @ and IMHO also : to support LaTeX3 code.
    – Martin Scharrer Mod
    Jan 31 '11 at 8:56
  • 1
    @Caramdir: I made the suggested changes and uploaded to the issue tracker. Now only letters, @ and : (for LaTeX3 code, can easily be removed if not wanted) build a macro. As a second rule every single character after the backslash is also a macro. I also attached a second proposal with some extra highlights.
    – Martin Scharrer Mod
    Jan 31 '11 at 22:41
  • 1
    Great! The extra highlights have the problem of matching anything that starts with \def. I'm not sure how much of a problem that is though.
    – Caramdir
    Feb 1 '11 at 0:00
  • @Caramdir: Thanks, I fixed that now.
    – Martin Scharrer Mod
    Feb 1 '11 at 0:26
  • I'm aghast! I thought it used catcodes to work out what to highlight. Feb 1 '11 at 11:24
  • @Martin: I've just tried your script, and it looks good. Only the special treatment of the \def s looks strange to me. (And I wouldn't want to have the highlighting in comments, so I'll disable the script again.) Feb 2 '11 at 12:43
  • @Martin: I just see what my problem with the \def s is: I think it doesn't fit into the colour design. Feb 2 '11 at 13:16
  • @Hendrik: I don't like the pink color as well. The color could be changed in the CSS file, but it might be just better to not handle \def s differently than other macros. Removing that line is simple.
    – Martin Scharrer Mod
    Feb 2 '11 at 13:44
  • @Hendrik: I now removed the special handling of definitions again from the greasemonkey script.
    – Martin Scharrer Mod
    Feb 2 '11 at 15:36
  • @Martin: Unfortunately I can't give another +1. Maybe you should update your question and ask people to vote on your changes. Or what would be the right way to make this official? Feb 2 '11 at 16:13
  • @Hendrik: I did update the question as you said.
    – Martin Scharrer Mod
    Feb 3 '11 at 16:26

As I think there are no objections, could someone in charge please add Martin's code to the site. TikZ code still looks horrible.

  • I know: this should be a comment and not an answer, but this way the question gets bumped to the top.
    – Caramdir
    Feb 10 '11 at 0:38
  • Thanks @Caramdir.
    – Martin Scharrer Mod
    Feb 10 '11 at 10:10
  • I guess it's better if you post it as a comment to Jeff's answer, with the comment directed at Geoff. Feb 10 '11 at 21:07
  • @Hendrik: That worked surprisingly well.
    – Caramdir
    Feb 10 '11 at 22:24
  • I just pointed you to someone in charge :-) Feb 11 '11 at 10:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .