MATLAB: How to use MathJax to render the LaTeX math in the published output in MATLAB 7.10 (R2010a)

MATLAB

I want to use MathJax to render the LaTeX math in the published output in MATLAB 7.10 (R2010a).

Best Answer

Refer to the following instructions for a workaround:

1) Start MATLAB and run the following commands:

   u = userpath;
   workingDir = u(1:find(u==pathsep,1,'first')-1);
   customXsl = fullfile(workingDir,'mxdom2mathjax.xsl');
   disp(customXsl)
   copyfile(which('private/mxdom2simplehtml.xsl'),customXsl)
   fileattrib(customXsl,'+w')
   disp(customXsl)
   edit(customXsl)

2) In the MATLAB Editor, add code to the XSL file to include MathJax in your generated HTML file:

Find this line:

   <head>

And insert these lines after it:

   <script type="text/javascript"
     src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
   </script>

3) In the MATLAB Editor, alter code in the XSL file to include use of MathJax to render the equations.

Find these lines:

 <xsl:template match="img[@class='equation']">
   <img>
     <xsl:attribute name="src"><xsl:value-of select="@src"/></xsl:attribute>
     <xsl:attribute name="alt"><xsl:value-of select="@alt"/></xsl:attribute>
   </img>
 </xsl:template>

And replace them with these:

 

 <xsl:template match="img[@class='equation']">
   <span class="MathJax_Preview">
       <img>
           <xsl:attribute name="src"><xsl:value-of select="@src"/></xsl:attribute>
           <xsl:attribute name="alt"><xsl:value-of select="@alt"/></xsl:attribute>
       </img>
   </span>
   <script type="math/tex">
     <xsl:call-template name="removeDollars">
       <xsl:with-param name="string" select="@alt"/>
     </xsl:call-template>
   </script>
 </xsl:template>
 <xsl:template name="removeDollars">
   <xsl:param name="string"/>
     <xsl:choose>
       <xsl:when test="concat(substring($string,1,2),substring($string,string-length($string)-1,2))='$$$$'">
         <xsl:value-of select="substring($string,3,string-length($string)-4)"/>
       </xsl:when>
       <xsl:when test="concat(substring($string,1,1),substring($string,string-length($string),1))='$$'">
         <xsl:value-of select="substring($string,2,string-length($string)-2)"/>
       </xsl:when>
       <xsl:otherwise>
         T<xsl:value-of select="$string"/>
       </xsl:otherwise>
     </xsl:choose>
 </xsl:template>

4) Save your changes.

5) Now publish your MATLAB program to HTML using this modified stylesheet.

For releases prior to MATLAB 8.0 (R2012b), use the following code:

   opts = [];
   opts.stylesheet = customXsl;
   opts.outputDir = tempname;
   htmlFile = publish('YOURFILE',opts);
   web(htmlFile,'-browser')

Make sure to replace "YOURFILE" with the name of the MATLAB program you wish to publish.

For releases starting with MATLAB 8.0 (R2012b), under the 'Publish' button in the Publish tab, select 'Edit Publishing Options'. In the 'Output Settings' section, enter the name of the new XSL file in the blank labeled, 'XSL file'. Then, click 'Publish'.