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

6 views (last 30 days)

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 28 Aug 2015

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'.

  2 Comments
Mikhail
Mikhail on 9 Jul 2014

The required changes are not very diligently formatted. The first change should read:

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

The second change should read:

<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>
      <xsl:value-of select="$string"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

I hope i got it right.

Royi Avital
Royi Avital on 11 Aug 2017
I wish you added support fot LaTeX using $$<LaTeX Code>$$ and $<LaTeX Code>$ inside the new Live Editor.

Sign in to comment.

More Answers (0)

Tags

No tags entered yet.

Products


Release

R2010a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!