# Can we have syntax highlighting?

It seems that language syntax highlighting is not working.

As I think we will have many answers involving small code fragments. Is it possible to enable this?

Few examples: 1, 2.

I used <!-- language: lang-... --> to specify the language in both cases.

• Yes, and it should be auto-enabled for questions being tagged with a programming languages, like on Stack Overflow. – Konrad Rudolph May 30 '17 at 13:57
• I thought this kind of feature would be automatically enabled by the stackexchange engine. It would be a nice addition to have it. Readability counts. – bli Jun 8 '17 at 8:55
• I've asked SE to activate this and they just told me they have. It should be live soon (there might be some caching issues involved). – terdon Jun 8 '17 at 16:56
• @KonradRudolph that can be set up by mods. The tag page a mod sees also has an option to associate a tag with a language. – terdon Jun 8 '17 at 21:09

I mentioned this to an SE employee and they implemented it today! We now have working syntax highlighting!

Tags can have a specific language syntax associated with them. This is something that mods can set up. So, for example, if we were to ever have a or, more likely a tag, the mods could set it up so that it is associated with Perl syntax highlighting.

If a question isn't tagged with a tag that is associated with any specific syntax highlighting, we can still enable it using HTML comments. The syntax is as follows (using perl as an example):

<!-- language: lang-perl -->


Each code block you want to have highlighted must have a comment like the one above, an empty line, and then the code. For example, this:

<!-- language: lang-python -->

var="foo"


Will be rendered like this:

import foo
var="bar"
otherVar = None


Each code block in a question or answer needs its own HTML comment, and you need a blank line between the comment and the first line of code, as shown above. Here's the list of currently supported languages and their identifying string (the lang prefix is not needed if a tag has the language associated with it, but it won't harm it and we're unlikely to have many such tags anyway):

none:      none   ## Useful for disabling syntax highlighting
bash:      lang-bash OR lang-bsh
c:         lang-c
clj:       lang-clj
coffee:    lang-coffee
cpp:       lang-cpp
cs:        lang-cs
csh:       lang-csh
css:       lang-css
dart:      lang-dart
erlang:    lang-erlang
go:        lang-go
hs:        lang-hs
html:      lang-html
java:      lang-java
js:        lang-js
latex:     lang-latex
lisp:      lang-lisp
lua:       lang-lua
ml:        lang-ml
pascal:    lang-pascal
perl:      lang-perl
php:       lang-php
proto:     lang-proto
py:        lang-py
r:         lang-r
rb:        lang-rb
regex:     lang-regex
rust:      lang-rust
scala:     lang-scala
sh:        lang-sh
sql:       lang-sql
vb:        lang-vb
vhdl:      lang-vhdl
xml:       lang-xml

• Did this stop working? I can’t seem to get anything highlighted, see e.g. here: bioinformatics.stackexchange.com/a/69/29 (both tagged, and the code is explicitly marked with a language). – Konrad Rudolph Jul 4 '17 at 17:16
• @KonradRudolph no, it works, it's just not very helpful for the code you posted. The snippet you had explicitly set a language for was colored but only in the words function and the 1E6 of log(1E6). The one you hadn't set explicitly wasn't colored because I hadn't associated the tag R with a language for syntax highlighting. I have now done so, so any future posts won't need the explicit <!-- language: lang-r --> anymore. – terdon Jul 4 '17 at 22:38
• Right, I forgot how … err … minimal Stack Overflow’s syntax highlighting is. Bit bizarre, given that programming languages is their core competence. – Konrad Rudolph Jul 5 '17 at 10:40
• @KonradRudolph AFAIK, they just use something developed by Google. And it does work OK for many cases. R doesn't seem to be one of them though, granted. – terdon Jul 5 '17 at 10:47