MATLAB Answers

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

16 views (last 30 days)
I want to use MathJax to render the LaTeX math in the published output in MATLAB 7.10 (R2010a).

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

  6 Comments
Michael Borovik
Michael Borovik on 6 Sep 2020
Original answer works great for HTML publishing in MATLAB R2020a. Equations look much better. I have not tried other suggestions.

Sign in to comment.

More Answers (3)

Daniel
Daniel on 12 Feb 2014
Edited: 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


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

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

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!