37

For syntax highlighting, StackExchange uses a general-purpose syntax highlighter (probably something like prettify?), which guesses what to highlight based on the most common languages: C-like languages, Python, etc (and often gets highlighting of other languages wrong, e.g. when you have a Haskell function with a "prime" in it, everything after the ' is highlighted as if quoted.)

For this TeX.SE, it seems worthwhile to use a syntax highlighter that is more tuned to (La)TeX. How could we go about this?


A toy example of something that ought to be highlighted correctly:

\begin{enumerate}
  \item This is # not a comment
  \item This is % a comment
\end{enumerate}

A good syntax highlighter would recognize which lines are comments, and also probably do something with 'begin', 'end' and 'item' (as the LaTeX Wikibook does).

(It seems a weaker version of this has been asked before as Syntax Highlighting Hints, and there's even a somewhat simple hack that is not implemented. Irrespective of all that history, consider this another request for TeX-specific syntax highlighting on this website, not necessarily using any of the more general solutions.)

  • 7
    +1 this is a really good idea! Hopefully we can get attention from the Stack Exchange developers for this. – Scott Morrison Jul 26 '10 at 20:10
  • 1
    Yes, it’s prettify. And making it support LaTeX should be relatively straightforward. – Konrad Rudolph Aug 17 '10 at 12:44
14

I had a look at the prettify, and despite my rexp-fu is seriously lacking, I pulled together something:

// PR_(STRING|KEYWORD|COMMENT|TYPE|LITERAL|PUNCTUATION|PLAIN|TAG|DECLARATION|SOURCE|ATTRIB_(NAME|VALUE)
PR.registerLangHandler(
  PR.createSimpleLexer(
    [
      ['opn', /^(?=[^\\])(?:\{|\\\[)/, null, '{['],
      ['clo', /^(?=[^\\])(?:\}|\\\])/, null, '}]']
    ],
    [
      [PR.PR_TAG, /^\\(?:text|math)?(?:rm|it|sl|tt|sc)\b/],
      [PR.PR_KEYWORD, /^\\(?:[^\\])\w*\b/],
      [PR.PR_COMMENT, /^(?=[^\\])%[^\r\n]*/],
      [PR.PR_PUNCTUATION, /^(?:\\\\|\\;|\\,|\\!|~|\ |\^)/],
      [PR.PR_PUNCTUATION, /^(?=[^\\])(?:\$){1,2}/],
      [PR.PR_PUNCTUATION, /^(?:\\\[|\\\])/],
      [PR.PR_LITERAL,
/^(?:by|at|to|spread)? ?(?:-)?(?:\d+)?\.?\d+ ?(?:true)? ?(?:pt|pc|in|bp|cm|mm|dd|cc|em|ex|mu|\\fil)\b/],
      [PR.PR_LITERAL, /^(?=[^\\])#\d?/],
      [PR.PR_LITERAL, /^(?=[^\\])[&_]/]
    ]),
  ['tex']);

And the accompanying colours from the site-design:

/* CSS */
.prettyprint { display: block; color: #393318; padding: 2px 4px;
  border: 1px solid #efefef; background-color: #f6f0df; }
.kwd { color: #8a4a0b; }
.com { color: #868686; }
.lit { color: #953838; }
.opn, .clo { color: #145680; }
.pun { color: #145680; }
.tag { color: #4c9067; font-weight: bold }

You know, something to get this train running!
I made liberal use of the "PUNCTUATION", "LITERAL", etc. because I'm not sure what they are corresponding in TeX.

Here is a pretty picture:

  • Thanks! Sorry for my ignorance right now, but is this something we need to get the Stack Exchange people to add to their configuration? (Or something that may be easier to get into Prettify itself, because SE people are often unwilling to make changes.) Or is it something that we can use on our own? – ShreevatsaR Dec 14 '10 at 8:30
  • @ShreevatsaR: I guess the first one. – morbusg Dec 14 '10 at 9:22
  • Oh, if anyone could tell me any LaTeX-specific "must have"'s, it'd be sweet. – morbusg Dec 14 '10 at 9:37
  • @morbusg: Personally, I like it very much if things in math mode get a special colour: In your example picture this would mean that, e.g., "|", "c" and "0" would get this colour. (I like it green, but it might be that this doesn't fit with the design.) – Hendrik Vogt Dec 14 '10 at 12:46
  • @Hendrik Vogt: that would mean, AFAIK, to capture the whole math mode content, meaning everything inside it would be green (because of regexp unioning, if I've understood correctly). – morbusg Dec 14 '10 at 21:09
  • @morbusg: I've got no idea what's possible. For me it would be quite OK if everything was green, but I guess others wouldn't like that. In principle I thought it should be possible to get only the "normal" characters in math mode green. – Hendrik Vogt Dec 15 '10 at 9:53
  • @Hendrik: Personally, I would prefer that that not be done with $ or $$, only \[/\] and \(/\) (and possibly the environments). The $-based syntax has a tendancy to get highlighters almost-permanently confused sometimes :-(. – SamB Dec 16 '10 at 20:24
  • @SamB: $$ is rare enough and can possibly be ignored. But I'd really like $ to work! I wrote a highlighter myself a long time ago, and it hardly ever got confused (2 or 3 times in >10 years of use). (With the highlighting, I like $ much better than \( and \).) And indeed I forgot math environments; I think they should get the same treatment. – Hendrik Vogt Dec 17 '10 at 10:10
  • @Hendrik Vogt: "$$ is rare enough and can possibly be ignored." Gee, thanks; I'm a Plain user myself ;-) I do agree with the idea of math mode coloring, though. I'll just need to dig a little deeper in prettify.js. – morbusg Dec 17 '10 at 10:57
  • @morbusg: Sorry, I was absent minded and didn't think of plain TeX (although I do use it from time to time for testing). So please ignore this part of my comment; also $$ should be treated accordingly. – Hendrik Vogt Dec 17 '10 at 12:57
10

We have implemented this prettify plugin and will enable it on the next deploy (tonight). If there are changes required please update this post so we will be sure to see it.

  • It seems that the preview doesn't work properly yet. I was rather frightened when looking at the preview for this question, but the actual question turned out nice. – Hendrik Vogt Dec 23 '10 at 13:02
  • 1
    There will be times where the preview doesn't match the final output. We are still working on making ways to improve this. – Geoff Dalgas Dec 23 '10 at 15:57
  • Ah, I only just found your answer to my comment - thanks a lot. I'm still seeing strange code previews; hope you'll succeed in fixing this. – Hendrik Vogt Jan 15 '11 at 20:11
  • Just to let you know; there's a new post about this. – Hendrik Vogt Jan 19 '11 at 9:46
  • I added some info about the newly posted issue mentioned by Hendrik. – Martin Scharrer Feb 5 '11 at 18:51
2

Not that I don't think that in-site support for this is a bad idea, but pastie.org has support for TeX syntax highlighting in the meantime. Of course, the original question should include the code so that we don't loose everything if pastie.org goes down, and then just add a link to the same code on pastie.org that says [highlighted] or something like that at the bottom.

1

GeSHi is a generic syntax highlighter for PHP. It can be customized for using it with LaTeX code. There exist LaTeX language files.

Here's the example above with the default LaTeX syntax highlighting:

alt text

Of course it could be adjusted to the requirements and the design of our site.

  • 3
    Not really applicable though since the software doesn’t run on PHP. In fact, the build-in syntax highlighting uses a (theoretically very versatile) JavaScript syntax highlighter. Someone just needs to write a LaTeX plug-in for it. – Konrad Rudolph Aug 17 '10 at 12:42
  • @Konrad: And get tex.SE to invoke Prettify such that it would actually use the plug-in when appropriate, yes. – SamB Dec 16 '10 at 20:28
1

SE is using Google Code Prettify (as mentioned on meta.SO), which doesn't yet support TeX. So the easiest way to get highlighting for this site is to add support in code prettify.

  • Well, see @morbusg’s answer. – Konrad Rudolph Dec 16 '10 at 12:40
  • @Konrad: Actually its your comment that I missed and I was wondering with morbusg was doing. Anyway I recommend to get this directly into prettify. – Caramdir Dec 17 '10 at 18:04

You must log in to answer this question.

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