98

From this question I understand that syntax highlighting on Stack Overflow is done using Google's prettify project, which does support MATLAB syntax.

Despite this, the MATLAB information page still specifies Code Language (used for syntax highlighting): default at the bottom.

Even if I use <!-- language: lang-matlab --> before a code block, the syntax highlighting doesn't work correctly.

For the most part, this isn't that big a deal, but the MATLAB matrix transpose operator is ', which makes the code highlighter think that everything between a pair of ' characters is a string. This can really make code hard to read.

a = 1:5;
b = a';
C = a*b;
for i = 1:5
    for j = 1:5
        R(i,j) = rand();
    end
end
d = det(C'*R); 

Can we get MATLAB syntax highlighting?

4

2 Answers 2

14

MATLAB syntax highlighting is now live!

Add syntax highlighting for the MATLAB language

This is the "lite" version of @Amro's prettify script which does not include complete keyword highlighting (the full script was a bit too large), but the major annoyances like transpose and commenting appear to be fixed.

a = 1:5;   % Comments properly highlighted
b = a';
%{         % Block comments also work
C = a*b;
for i = 1:5
    for j = 1:5
        R(i,j) = rand();
    end
end
%}
d = det(C'*R);   % Transpose doesn't act as quote
s = 'This is a test.';   % but strings are still properly highlighted
2
  • 4
    * complex conjugate transpose (before I get jumped on). ;)
    – beaker
    Commented May 20, 2016 at 16:59
  • 1
    Finally I can answer questions without appending // to comments or having to worry about transposing as "stray comments"... and block comments, yes!
    – rayryeng
    Commented May 24, 2016 at 14:36
-6

I am not sure why <!-- language: lang-matlab --> does not work; but the tag-wiki for also says:

Code Language (used for syntax highlighting): default

but the code snippets can be highlighted using lang-golang (as opposed to the obvious lang-go.


Testing with MATLAB code, I notice that we can use lang-m (I'm not sure whether it actually highlights code with MATLAB syntax or not):

a = 1:5;
b = a';
C = a*b;
for i = 1:5
    for j = 1:5
        R(i,j) = rand();
    end
end
% comment to test
d = det(C'*R);
5
  • This is the default highlighting; there's no recognition of comments. lang-go does supposedly exist, lang-golang would use the default again.
    – Ben
    Commented Nov 4, 2015 at 11:02
  • lang-m doesn't highlight MATLAB code correctly. Note the *R); which is highlighted in red (as a string) because of the ' (transpose operator) which is also used to denote strings in MATLAB like 'mystring'. The comment is also not correctly highlighted which can lead people to use %// instead.
    – IKavanagh
    Commented Nov 4, 2015 at 11:57
  • @IKavanagh That is an issue specific to the code-prettify library itself. You can test it locally by using github.com/google/code-prettify
    – hjpotter92
    Commented Nov 4, 2015 at 12:06
  • @IKavanagh Check the fiddle for eg: jsfiddle.net/ndx3j106
    – hjpotter92
    Commented Nov 4, 2015 at 12:09
  • 7
    But SE hasn't added the MATLAB extension, so you're testing the wrong version. That's the point of the question.
    – Ben
    Commented Nov 4, 2015 at 13:11

You must log in to answer this question.

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