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).
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.
-
$\begingroup$ 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. $\endgroup$ – Geoff Oxberry Apr 20 '13 at 4:39
-
$\begingroup$ 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 ofgoogle-prettify
itself, maybe one could ask the SE mods to use that file in their code? $\endgroup$ – Christian Clason Apr 20 '13 at 8:31 -
$\begingroup$ @GeoffOxberry - There's also a style file for R in trunk now. $\endgroup$ – Christian Clason Apr 20 '13 at 8:39
-
$\begingroup$ 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 :) $\endgroup$ – Christian Clason Apr 20 '13 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.
-
1$\begingroup$ I just tested the explicit approach on the most recent Python question (scicomp.stackexchange.com/q/6845/1804), and it does work as advertised. $\endgroup$ – Christian Clason Apr 19 '13 at 14:55
<!-- language: lang-py -->
$\endgroup$ – Christian Clason Apr 19 '13 at 15:58