Is there any way to set up a cell style with a particular syntax highlighting? For example, I'd like to have a CProgram cell to write in blue the C special words. I have done it by setting InputAutoReplacements for each word, replacing it with a StyleBox. It more or less works, but it is by far not the same as the usual highlighting on input cells... If you paste a bunch of code at once it doesn't highlight. If you, e.g, had set "int" to be spelled in blue, if you erase the t and go on writing, it all becomes blue. Perhaps it is fine enough, but I'd like to know if there's a better way.

Furthermore, what's the option that makes Input cells automatically turn the text you enter into RawBoxes and such? Is that customizable?

  • $\begingroup$ Perhaps there are much simpler ways to do it, but I could not help exposing one of my toys :) $\endgroup$ Commented Feb 4, 2012 at 20:37
  • $\begingroup$ No need to help yourself, it's a great toy $\endgroup$
    – Rojo
    Commented Feb 4, 2012 at 21:08

1 Answer 1


As a part of a larger sets of development tools which I am working on currently, I have developed a general syntax highlighter generator which does just that (not yet with styles though, this is coming). I wanted to put in on GitHub and do a bit more polishing / development, but since you asked the question, here goes.


  • From a simple lexical specification, a whole package is generated, which provides code highlighting capabilities for a given language. Once you generate the package, you (supposedly) start happily using it, and don't depend on the master package (the generator), unless you want to generate another highlighter package. For those who are familiar with js Google prettify, this is similar in spirit, but less developed as of now.
  • The lexical analyzer is generated automatically from your specifications, but you can also override it with a custom one.
  • You can customize it in many ways, including colors for keywords etc.
  • There are several optimizations which can be switched on and off, to control the responsiveness of the highlighter
  • keywords etc are highlighted as you type
  • Bracket and paren-matching / highlighting is supported.
  • Cells can be evaluatable.

For the impatient, the package and a notebook with an example for C language can be downloaded here and here. I also made a gist where one can look at them as well. The notebook can be regarded as a brief manual to the package.

Installation steps

  • Place a CodeHighlighterGenerator.m package into any directory where Mathematica can find it (e.g. FileNameJoin[{$UserBaseDirectory,"Applications"}])

  • Open the CodeHighlighterGenerator.nb notebook and follow the discussion there.

Future plans

I plan to place the project to GitHub properly in a few days. There are several directions in which I plan to extend the package, it is a work in progress. All comments & suggestions are more than welcome!

Here are a few screen-shots: enter image description here enter image description here

  • $\begingroup$ great package generator! I don't see the prompt nor see what I am selecting in those cells. I'm on W7. Is that expected? $\endgroup$
    – Rojo
    Commented Feb 4, 2012 at 21:51
  • 2
    $\begingroup$ @Rojo Ok, this is an important omission, so let me explain. You see the prompt when you press one of the key triggering the event actions. By default, there are just 3 such keys: left-arrow key, right-arrow key, and space bar. This was done to omptimize the responsiveness of the highighter (one can enable other keys, but this will slow it down, particularly when typing, so I disabled them by default). So, to see the prompt, press one of those. $\endgroup$ Commented Feb 4, 2012 at 22:01
  • 2
    $\begingroup$ @Spartacus Yep, real-time highlighting. I use a combination of lazy evaluation, CellEventActions, and a very nice thing called Developer`CellInformation` , which I discovered by pure chance and which allows me to track the current cursor position. Even with this, making this work was a highly non-trivial thing (for me anyway). But the package has other goodies. Check out the mergeOptions function for example - with it, I can specify partial option trees and get them merged onto full option tree of the original function. OTOH, parts of the code are quite ugly, I intend to rewrite soon. $\endgroup$ Commented Feb 4, 2012 at 23:34
  • 1
    $\begingroup$ @StackExchanger Yes, I do :). The code in its current form has certain limitations, so I intend to rewrite it, to make more modular and extensible. Once this is done, I'd like to build some more features, including this. $\endgroup$ Commented May 19, 2012 at 20:51
  • 1
    $\begingroup$ @Szabolcs Thanks! This is my code highlighter at work :). I made a Java version for Andreas. $\endgroup$ Commented Jun 1, 2012 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.