Syntax Highlighting for Matlab (HTML, LaTeX, RTF)

HighLight: Syntax Highlighting for Matlab (in HTML, LaTeX, RTF, XML)

This Matlab function converts a Matlab M-file into HTML, RTF or LaTeX with syntax highlighting (similar to the Mathworks M-file editor). An XML output is also available.

This function might be useful when inserting Matlab code in a report or any other document: syntax highlighting makes the code easier to read !


This function is self documented, see the Matlab help for syntax:

  >> help highlight

At Matlab prompt, to convert the 'highlight.m' file into several formats, type:

  >> % to generate an HTML version ('highlight.html')
  >> highlight('highlight.m','html') 
  >> % to generate a LaTeX version ('annexe3.tex')
  >> highlight('highlight.m','tex','annexe3.tex') 
  >> % to generate a RTF version ('highlight.rtf') that can be loaded into Microsoft Word
  >> highlight('highlight.m','rtf') 
  >> % to generate an HTML version ('highlight.html') with tab characters replaced by 3 spaces.
  >> highlight('highlight.m',struct('type','html','tabs',3)) 

You can also decide to include or not the line numbers, or you can provide a file identifier rather than a filename for the output file: in that case, the converted M-file is written in the file corresponding to the file identifier, but without the HTML/LaTeX/RTF/XML header and footer.

  >> fid = fopen('report.tex', 'a');
  >> highlight('test.m', 'tex', fid);
  >> fclose(fid);

At last, HTML output can be customized thanks to the use of CSS (see the header of the highlight.m function). If you wish a completely different output layout, you can convert your M-file in XML and then use XSL to transform it into what you want. XSL files for conversion from XML to HTML and LaTeX are provided.


Without syntax highlighting    With syntax highlighting
Without syntax highlighting    With syntax highlighting

Another one

This simple Matlab code:

for i=1:10,
    [a, b] = my_function('test string', c(i:end), '%d'); % a comment


0001 for i=1:10,
0002     [a, b] = my_function('test string', c(i:end), '%d'); % a comment
0003 end

The HTML code is:

0001 <span class="keyword">for</span> i=1:10,
0002     [a, b] = my_function(<span class="string">'test string'</span>, c(i:end), <span class="string">'%d'</span>); <span class="comment">% a comment</span>
0003 <span class="keyword">end</span>

The LaTeX code is:

0001 \textcolor{keyword}{for} i=1:10,
0002     [a, b] = my\_function(\textcolor{string}{'test string'}, c(i:end), \textcolor{string}{'\%d'}); \textcolor{comment}{% a comment}
0003 \textcolor{keyword}{end}

The RTF code is:

{\f0\fs16{{0001 }{\cf2 for}{ i=1:10,}
\par {0002 }{    [a, b] = my_function(}{\cf4 'test string'}{, c(i:end), }{\cf4 '%d'}{); }{\cf3 % a comment}
\par {0003 }{\cf2 end}
\par }}

And the XML code is:

<line nb="0001 "><keyword>for</keyword> i=1:10,</line>
<line nb="0002 ">    [a, b] = my_function(<string>'test string'</string>, c(i:end), <string>'%d'</string>); <comment>% a comment</comment></line>
<line nb="0003 "><keyword>end</keyword></line>