# 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 '13 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 '13 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 '13 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 '13 at 8:31
• @GeoffOxberry - There's also a style file for R in trunk now. Apr 20 '13 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 '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.

• 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 '13 at 14:55
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.30078816413879395, "perplexity": 1837.170437067821}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 20, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2021-43/segments/1634323588257.34/warc/CC-MAIN-20211028034828-20211028064828-00002.warc.gz"}