# Feature request: syntax highlighting for code

We're starting to get questions with reasonable code snippets in MATLAB and Python, and it would be very useful to enable syntax highlighting for Python, MATLAB, Fortran, C, C++, and R (these are the main languages I can think of in common use in computational science). An example question for which such a feature would be useful is Numerical integration for modelling curve for superconductors (Python).

• It seems the problem with the example question was a simple typo in the highlighting markup: There need to be spaces between the dashes and the language specification: <!-- language: lang-py --> Apr 19, 2013 at 15:58
• @ChristianClason: Thank you for the fix! There's still an issue of not having syntax highlighting available for some languages used in computational science. Fortran and MATLAB syntax highlighting aren't yet available, which is a limitation of Google Prettify. R would also be nice, but R code doesn't come up here as much as it does on CrossValidated. Apr 20, 2013 at 4:38

As @hardmath pointed out, you can already explicitly enable syntax highlighting by prepending <!-- language: lang-py --> (note the spaces) and an empty line before the code block. (This is mentioned on https://stackoverflow.com/editing-help#syntax-highlighting.)

What doesn't seem to be working on the SciComp.SE site is automatic syntax highlighting by associating specific tags (python, matlab etc.) with the corresponding language code, as is done on the StackOverflow site. Jeff Atwood writes in a comment on https://meta.stackexchange.com/a/72163:

diamond moderators can now change syntax highlighting on a per-tag per-site basis.

I'm not sure if this refers to site moderators or only to SE network moderators.

• I've updated the Python and C++ tags, but not the Fortran or MATLAB tags, because syntax highlighting doesn't yet exist for those languages. Apr 20, 2013 at 4:39
• Great! There's a MATLAB mode in google-prettify trunk since March 2013, so there's hope that it will make it into SE at some point in the future. (But until then, I agree that the default highlighting is worse than none at all.) There is an old issue about FORTRAN support, which contains a proposed style file. Since the C++ (and C!) highlighting files seem not to be part of google-prettify itself, maybe one could ask the SE mods to use that file in their code? Apr 20, 2013 at 8:31
• @GeoffOxberry - There's also a style file for R in trunk now. Apr 20, 2013 at 8:39
• Should have done research instead of guessing: C and C++ are part of prettify.js and don't require external language files. This does not affect the suggestion to ask the SE mods to drop in the file for FORTRAN :) Apr 20, 2013 at 8:45

This is an excellent idea! I'm surprised that stack overflow hasn't gone in this direction already...

Great feature!

GitHub has the code they use for syntax highlight open-sourced, it may be a good place to start.

Added: Apparently StackOverflow does support a number of manually specified language options for syntax highlighting, over a previous/default approach that used question tags to infer a language for syntax highlighting. Who knew?

Since the tags here are not terribly language directed, this may well work for us with little or no extra effort.

It's certainly a good question. Since some Markdown syntax options for code are quite minimalist, it's not obvious how a language designation would best be integrated with the monospace display context. Perhaps it really wants a new display context, analogous (but not as intricate) as the MathJAX/LaTeX context.

Likely what @RSFalcon7 is referring to above is GitHub Flavored Markdown. At any rate it shows what can be done, and what tradeoffs might be necessary.

• I just tested the explicit approach on the most recent Python question (scicomp.stackexchange.com/q/6845/1804), and it does work as advertised. Apr 19, 2013 at 14:55