Exporting notebook to HTML, using conversion rules - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-09-23T10:20:45Z https://mathematica.stackexchange.com/feeds/question/6023 https://creativecommons.org/licenses/by-sa/4.0/rdf https://mathematica.stackexchange.com/q/6023 15 Exporting notebook to HTML, using conversion rules jmlopez https://mathematica.stackexchange.com/users/877 2012-05-25T07:02:07Z 2012-10-18T12:21:39Z <p>The idea is to export the whole notebook to HTML with all the math equations in latex form. In this way we can add the <a href="http://www.mathjax.org/" rel="nofollow noreferrer">mathjax</a> script and it will take care of the math.</p> <p>Suppose we have the following in a notebook:</p> <blockquote> <p>This is an equation: $f(x) = x^2$. We let $x \in [0,1]$ for this example.</p> </blockquote> <p>The following is the expression that generates the above.</p> <pre><code>Cell[TextData[{ "This is an equation: ", Cell[BoxData[ FormBox[ RowBox[{ RowBox[{"f", "(", "x", ")"}], " ", "=", " ", SuperscriptBox["x", "2"]}], TraditionalForm]], FormatType-&gt;"TraditionalForm"], ". We let ", Cell[BoxData[ FormBox[ RowBox[{"x", " ", "\[Element]", " ", RowBox[{"[", RowBox[{"0", ",", "1"}], "]"}]}], TraditionalForm]], FormatType-&gt;"TraditionalForm"], " for this example." }], "Text"] </code></pre> <p>If you look <a href="http://reference.wolfram.com/mathematica/ref/format/TeX.html#1511511812" rel="nofollow noreferrer">here</a> you will find an example of how to use the conversion rules. Unfortunately, I haven't been very lucky with it, as you can see in one of my previous <a href="https://stackoverflow.com/q/6870435/788553">posts</a>.</p> <p>In any case, here is an attempt:</p> <pre><code>ExportString[ Cell[TextData[{"This is an equation: ", Cell[BoxData[ FormBox[RowBox[{RowBox[{"f", "(", "x", ")"}], " ", "=", " ", SuperscriptBox["x", "2"]}], TraditionalForm]], FormatType -&gt; "TraditionalForm"], ". We let ", Cell[BoxData[ FormBox[RowBox[{"x", " ", "\[Element]", " ", RowBox[{"[", RowBox[{"0", ",", "1"}], "]"}]}], TraditionalForm]], FormatType -&gt; "TraditionalForm"], " for this example."}], "Text" ], "HTML", "ConversionRules" -&gt; {"TraditionalForm" -&gt; {"$", ConvertTeXBoxesToTeX[#] &amp;, "$"}}] </code></pre> <p>The result is the following html code:</p> <pre class="lang-html prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN" "HTMLFiles/xhtml-math11-f.dtd"&gt; &lt;!-- Created by Wolfram Mathematica 8.0 : www.wolfram.com --&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;title&gt; Untitled &lt;/title&gt; &lt;link href="HTMLFiles/m0001756441.css" rel="stylesheet" \ type="text/css" /&gt; &lt;/head&gt; &lt;body&gt; &lt;p class="Text"&gt; This is an equation: &lt;span&gt;&lt;span&gt;&lt;img \ src="HTMLFiles/m0001756441_1.gif" alt="m0001756441_1.gif" width="51" \ height="15" style="vertical-align:middle" /&gt;&lt;/span&gt;&lt;/span&gt;. We let \ &lt;span&gt;&lt;span&gt;x &amp;isin; [0,1]&lt;/span&gt;&lt;/span&gt; for this example. &lt;/p&gt; &lt;div style="font-family:Helvetica; font-size:11px; width:100%; \ border:1px none #999999; border-top-style:solid; padding-top:2px; \ margin-top:20px;"&gt; &lt;a href="http://www.wolfram.com/products/mathematica/" \ style="color:#000; text-decoration:none;"&gt; &lt;img src="HTMLFiles/spikeyIcon.png" alt="Spikey" width="20" \ height="21" style="padding-right:2px; border:0px solid white; \ vertical-align:middle;" /&gt; &lt;span style="color:#555555"&gt;Created with&lt;/span&gt; Wolfram &lt;span \ style="font-style: italic;"&gt;Mathematica&lt;/span&gt; 8.0 &lt;/a&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>If we try to export to TeX I'm not so lucky either, instead of the dollar signs Mathematica just ignores it and it gives me:</p> <pre class="lang-tex prettyprint-override"><code>\documentclass{article} \usepackage{amsmath, amssymb, graphics, setspace} \newcommand{\mathsym}{{}} \newcommand{\unicode}{{}} \newcounter{mathematicapage} \begin{document} This is an equation: $$f(x) = x^2$$. We let $$x \in [0,1]$$ for this \ example. \end{document} </code></pre> <p>The question is: How do we export a notebook to HTML and TeX so that the math is wrapped around the dollar signs? Any other tips or comments are welcomed.</p> <h1>EDIT:</h1> <p>This conversion rule for HTML might be more useful:</p> <pre><code>"Text" -&gt; {"&lt;p class=\"text\"&gt;\n", If[MatchQ[#, _FormBox], "$" &lt;&gt; ConvertTeXBoxesToTeX[#[]] &lt;&gt; "$", #] &amp;, "\n&lt;/p&gt;"} </code></pre> <p>Using that we obtain:</p> <pre class="lang-html prettyprint-override"><code>&lt;p class="text"&gt; This is an equation: &lt;span&gt;&lt;span&gt;$f(x) = x^2$&lt;/span&gt;&lt;/span&gt;. We let &lt;span&gt;&lt;span&gt;$x \in [0,1]$&lt;/span&gt;&lt;/span&gt; for this example. &lt;/p&gt; </code></pre> <p>Question now is... how do we get rid of the span tag?</p> https://mathematica.stackexchange.com/questions/6023/-/6038#6038 3 Answer by rcollyer for Exporting notebook to HTML, using conversion rules rcollyer https://mathematica.stackexchange.com/users/52 2012-05-25T15:16:30Z 2012-05-25T15:16:30Z <p>The <a href="http://www.w3schools.com/tags/tag_span.asp" rel="nofollow noreferrer">span tag</a> does nothing unless you specifically style it as it is designed to</p> <blockquote> <p>The <code>&lt;span&gt;</code> tag is used to group inline-elements in a document.</p> <p>The <code>&lt;span&gt;</code> tag provides no visual change by itself.</p> <p>The <code>&lt;span&gt;</code> tag provides a way to add a hook to a part of a text or a part of a document.</p> </blockquote> <p>Using css, you can hook on to that and style those sections. For example,</p> <pre class="lang-css prettyprint-override"><code>p.text&gt;span&gt;span { color:blue; background-color:red; } </code></pre> <p>would produce this</p> <p><img src="https://i.stack.imgur.com/k8IRI.png" alt="enter image description here"></p> <p>Not my idea of reasonable styling, but <em>MathJax</em> seems to respect the styles of the html the $\TeX$ is placed in.</p> <p>However, if you really want to remove the spans, then I would do the following</p> <pre><code>StringReplace[ExportString[...], "&lt;span&gt;&lt;span&gt;$" ~~ t : Shortest[__] ~~ "$&lt;/span&gt;&lt;/span&gt;" :&gt; "$" &lt;&gt; t &lt;&gt; "$" ] </code></pre> <p>Note the use of <a href="http://reference.wolfram.com/mathematica/ref/Shortest.html" rel="nofollow noreferrer"><code>Shortest</code></a>. </p> https://mathematica.stackexchange.com/questions/6023/-/6040#6040 8 Answer by jmlopez for Exporting notebook to HTML, using conversion rules jmlopez https://mathematica.stackexchange.com/users/877 2012-05-25T16:45:41Z 2012-10-18T12:21:39Z <p>It seems that every time you open a new cell with no style, that is "", it automatically puts the span tag to it. To to avoid this we can use this rule:</p> <pre><code>"ConversionRules" -&gt; { "" -&gt; {"", ""} } </code></pre> <p>So now, evaluating </p> <pre><code>ExportString[ Cell[TextData[{"This is an equation: ", Cell[BoxData[ FormBox[RowBox[{RowBox[{"f", "(", "x", ")"}], " ", "=", " ", SuperscriptBox["x", "2"]}], TraditionalForm]], FormatType -&gt; "TraditionalForm" ], ". We let ", Cell[BoxData[ FormBox[RowBox[{"x", " ", "\[Element]", " ", RowBox[{"[", RowBox[{"0", ",", "1"}], "]"}]}], TraditionalForm]], FormatType -&gt; "TraditionalForm" ], " for this example."} ], "Text"], "HTML", "FullDocument" -&gt; False, "ConversionRules" -&gt; { "Text" -&gt; {"&lt;p class=\"text\"&gt;\n", If[MatchQ[#, _FormBox], "$" &lt;&gt; ConvertTeXBoxesToTeX[#[]] &lt;&gt; "$", #] &amp;, "\n&lt;/p&gt;"}, "" -&gt; {"", ""} } ] </code></pre> <p>Now we obtain the desired result:</p> <pre class="lang-html prettyprint-override"><code>&lt;p class="text"&gt; This is an equation: $f(x) = x^2$. We let $x \in [0,1]$ for this example. &lt;/p&gt; </code></pre>