MATLAB Answers

5

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

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

Tags

No tags entered yet.

Products


Release

R2010a

4 Answers

Answer by MathWorks Support Team on 28 Aug 2015
 Accepted 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'.

  5 Comments

Hi!

I think step 3 can be improved. By using the following substitution, the correct display type (inline or display) will be used. With the current solution, all equations are displayed inline.

<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>
  <xsl:call-template name="equationType">
    <xsl:with-param name="string" select="@alt"/>
  </xsl:call-template>
</xsl:template>
<xsl:template name="equationType">
  <xsl:param name="string"/>
  <xsl:choose>
    <xsl:when test="concat(substring($string,1,2),substring($string,string-length($string)-1,2))='$$$$'">
      <script type="math/tex; mode=display">
        <xsl:value-of select="substring($string,3,string-length($string)-4)"/>
      </script>
    </xsl:when>
    <xsl:when test="concat(substring($string,1,1),substring($string,string-length($string),1))='$$'">
      <script type="math/tex">
        <xsl:value-of select="substring($string,2,string-length($string)-2)"/>
      </script>
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select="$string"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>
Thank you Mathworks support for this answer - works great for me in 2016b. One suggestion: in step 2 I would recommend adding a link to MathJax documentation in case the CDN link changes (<http://docs.mathjax.org/en/latest/start.html#using-a-content-delivery-network-cdn)>. Currently the link mentioned still works but they have officially retired their self-hosted CDN as of April 2017.
I wish you added support fot LaTeX using $$<LaTeX Code>$$ and $<LaTeX Code>$ inside the new Live Editor.

Sign in to comment.


Answer by Daniel
on 12 Feb 2014
Edited by Walter Roberson
on 6 Nov 2015

after fixing the syntax error at the sript-element:

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

the XML parser throws another error:

Error on line 3 column 50 of null:
Error reported by XML parser: White spaces are required between publicId and systemId.
Error using com.icl.saxon.Controller/transform
Java exception occurred:
javax.xml.transform.TransformerException: org.xml.sax.SAXParseException:
White spaces are required between publicId and systemId.
	at com.icl.saxon.om.Builder.build(Builder.java:273)
	at com.icl.saxon.Controller.transform(Controller.java:977)
Caused by: org.xml.sax.SAXParseException: White spaces are required
between publicId and systemId.
	at
    org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown
    Source)
	at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown
    Source)
	at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown
    Source)
	at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown
    Source)
	at org.apache.xerces.impl.XMLScanner.reportFatalError(Unknown Source)
	at org.apache.xerces.impl.XMLScanner.scanExternalID(Unknown Source)
	at
    org.apache.xerces.impl.XMLDocumentScannerImpl.scanDoctypeDecl(Unknown
    Source)
	at
    org.apache.xerces.impl.XMLDocumentScannerImpl$PrologDispatcher.dispatch(Unknown
    Source)
	at
    org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown
    Source)
	at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
	at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
	at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
	at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
	at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown
    Source)
	at com.icl.saxon.om.Builder.build(Builder.java:265)
	... 1 more
---------
org.xml.sax.SAXParseException: White spaces are required between publicId
and systemId.
	at
    org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown
    Source)
	at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown
    Source)
	at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown
    Source)
	at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown
    Source)
	at org.apache.xerces.impl.XMLScanner.reportFatalError(Unknown Source)
	at org.apache.xerces.impl.XMLScanner.scanExternalID(Unknown Source)
	at
    org.apache.xerces.impl.XMLDocumentScannerImpl.scanDoctypeDecl(Unknown
    Source)
	at
    org.apache.xerces.impl.XMLDocumentScannerImpl$PrologDispatcher.dispatch(Unknown
    Source)
	at
    org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown
    Source)
	at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
	at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
	at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
	at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
	at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown
    Source)
	at com.icl.saxon.om.Builder.build(Builder.java:265)
	at com.icl.saxon.Controller.transform(Controller.java:977)
Error in xslt (line 90)
xProcessor.transform(xSource,xResult);
Error in publish>publishToPdf (line 535)
xslt(saxSource,getPdfStylesheet(noToc),xsltDestination);
Error in publish (line 202)
        publishToPdf(dom,options,outputAbsoluteFilename)
    % code
  end

I use Matlab R2011b. How can I use the MathJax?

Best regards

Daniel Güthler

  0 Comments

Sign in to comment.


Answer by Gregory
on 26 May 2015

I'm having the same issues when trying to implement this workaround. Has anyone been able to get this to work? Matlab publish to pdf would be great, if it weren't for the fuzzy LaTex equations.

  0 Comments

Sign in to comment.


Answer by Laura
on 28 Aug 2015

Thank you for your feedback, I have edited the solution to fix the syntax errors that I found. For technical issues you have encountered with the above solution, please contact MathWorks Technical Support. A support engineer will be able to investigate your issue and update the article accordingly.

  0 Comments

Sign in to comment.