# How to properly display Matlab code?

When I try to post Matlab code, there are some formatting problems.

For example, an apostrophe is used in Matlab to transpose a matrix. But when this is posted, it is interpreted as the beginning of a string, and the rest of the code turns red.

Also, comments in Matlab begin with the percentage sign. Words after this sign turn blue.

Is there a way to avoid these problems?

• When in doubt, just turn all formatting off. – Mast Oct 31 '16 at 16:44
• You could try to make the code highlighting vanish with <-- language : none --> before a code block. .. see also relevant meta post on mso – Vogel612 Oct 31 '16 at 16:47

Thanks for bringing it up.

The tag had no syntax highlighting language associated to it.

I've modified the tag settings to use lang-matlab, which should fix the automatic syntax highlighting in that language.

• What? We have the power to do this? – Simon Forsberg Oct 31 '16 at 17:18
• @SimonForsberg yup – Mathieu Guindon Oct 31 '16 at 17:21

You can specify the used language by inserting the following in your post. Thanks to Mat's Mug you can now use:

<!-- language: lang-matlab -->


This particular example makes SE think all the code blocks in the question are MATLAB code.

If your language is not supported, simply turn it off:

<!-- language: lang-none -->

• The language meta-comment was required prior to setting up the tag's syntax highlighting; now it should work the same as any other language. Also if you don't specify language-all the meta-instruction only applies to the next code block, not "all code blocks in the question" - that's useful for e.g. formatting a XAML or T-SQL snippet in an otherwise C# post. – Mathieu Guindon Oct 31 '16 at 16:55
• @Mat'sMug True, however, languages are not always properly recognized. Marking them explicit like I did does fix that. Marking all code blocks the same language doesn't work in multiple format questions, no. That's most of the time not an issue but it's good you mention it. – Mast Nov 1 '16 at 9:07