8

Most of the time, when I write in code in Visual Studio, copy and paste it in the Stack Overflow answer box, the syntax color highlighting works. Sometimes, it doesn't.

Other times I spend over 15 minutes trying to fix it and then give up. One example of the one that didn't work is the answer I provided just here. Am I missing anything? If somebody finds the problem, can you share with me? I will be happy to go back to all my other answers with this problem and fix them all.

6
  • 3
    There is no C# tag on the question, so the highlighter doesn't know to use that language. Maybe you want to add it? Or you can always specify the language manually in your post.
    – animuson StaffMod
    May 25, 2016 at 3:52
  • @animuson Omg. I can't believe this. It worked. How can you specify the language manually? You can put this as an answer. Thanks
    – Programmer
    May 25, 2016 at 3:54
  • 1
    @Programmer <--language: lang-cs --> before the code snippet
    – Rob Mod
    May 25, 2016 at 4:12
  • @animuson This is weird because I just answered a new question and the highlighting worked without the C# tag on the question..stackoverflow.com/questions/37427571/…
    – Programmer
    May 25, 2016 at 4:17
  • 2
    @Programmer Perhaps it's guessing it's java due to the android tag? The colouring between C# and java should be almost identical.
    – Rob Mod
    May 25, 2016 at 4:28

2 Answers 2

8

Syntax highlighting is dependent on the tags. Some tags have languages pre-defined, some tags let the highlighter interpret the language (default), and some tags do not enable highlighting features at all (none).

  • When there is only one tag with a specific language defined, all of the code blocks will use that language.
  • When there are multiple tags with a specific language defined, all code blocks will let the highlighter interpret the language on its own.
  • When any tag is set to default and no tags define a specific language, all code blocks will let the highlighter interpret the language on its own.
  • When all tags are set to none, no syntax highlights will occur.

You can view which language is set for a tag by viewing the tag wiki and looking for the "Code Language" line at the very bottom (below the Edit button).

These rules apply to the question and all its answers. You can override these rules by manually setting the language in your post, with either of these two methods:

  1. Include a rule directly above the code block to override that code block only.
    Example: <!-- language: lang-c -->

  2. Include a rule in your post that defines the language for all code blockes in that post.
    Example: <!-- language-all: lang-c -->

1
  • 1
    Have been here for over a year and didn't know this. Thanks again.
    – Programmer
    May 25, 2016 at 4:39
3

Use fenced code blocks (```) and define the language for highlighting as "cs".

For example:

```cs
var x = 1
```

results in:

var x = 1

The highlight will differ from your IDE. If you want to check it indeed works, check the CSS style applied to the highlighted code section in your browser.

Here's a list of other ways to also achieve this.

And Here's a long, though not complete, list of supported languages (thanks goes to @PeterMortensen)

2
  • The canonical (e.g., with all the supported language tags and their synonyms). May 23, 2022 at 10:16
  • @PeterMortensen great! I find it incomplete and things might change, but I was looking for something like that! I've added it to the answer as it is really useful and better be more visible. Thanks!
    – estani
    May 24, 2022 at 8:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .