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 mathjax script and it will take care of the math.
Suppose we have the following in a notebook:
This is an equation: $f(x) = x^2$. We let $x \in [0,1]$ for this example.
The following is the expression that generates the above.
Cell[TextData[{
"This is an equation: ",
Cell[BoxData[
FormBox[
RowBox[{
RowBox[{"f", "(", "x", ")"}], " ", "=", " ",
SuperscriptBox["x", "2"]}], TraditionalForm]],
FormatType->"TraditionalForm"],
". We let ",
Cell[BoxData[
FormBox[
RowBox[{"x", " ", "\[Element]", " ",
RowBox[{"[",
RowBox[{"0", ",", "1"}], "]"}]}], TraditionalForm]],
FormatType->"TraditionalForm"],
" for this example."
}], "Text"]
If you look here 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 posts.
In any case, here is an attempt:
ExportString[
Cell[TextData[{"This is an equation: ",
Cell[BoxData[
FormBox[RowBox[{RowBox[{"f", "(", "x", ")"}], " ", "=", " ",
SuperscriptBox["x", "2"]}], TraditionalForm]],
FormatType -> "TraditionalForm"], ". We let ",
Cell[BoxData[
FormBox[RowBox[{"x", " ", "\[Element]", " ",
RowBox[{"[", RowBox[{"0", ",", "1"}], "]"}]}],
TraditionalForm]], FormatType -> "TraditionalForm"],
" for this example."}], "Text"
],
"HTML", "ConversionRules" -> {"TraditionalForm" -> {"$",
Convert`TeX`BoxesToTeX[#] &, "$"}}]
The result is the following html code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN"
"HTMLFiles/xhtml-math11-f.dtd">
<!-- Created by Wolfram Mathematica 8.0 : www.wolfram.com -->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
Untitled
</title>
<link href="HTMLFiles/m0001756441.css" rel="stylesheet" \
type="text/css" />
</head>
<body>
<p class="Text">
This is an equation: <span><span><img \
src="HTMLFiles/m0001756441_1.gif" alt="m0001756441_1.gif" width="51" \
height="15" style="vertical-align:middle" /></span></span>. We let \
<span><span>x ∈ [0,1]</span></span> for this example.
</p>
<div style="font-family:Helvetica; font-size:11px; width:100%; \
border:1px none #999999; border-top-style:solid; padding-top:2px; \
margin-top:20px;">
<a href="http://www.wolfram.com/products/mathematica/" \
style="color:#000; text-decoration:none;">
<img src="HTMLFiles/spikeyIcon.png" alt="Spikey" width="20" \
height="21" style="padding-right:2px; border:0px solid white; \
vertical-align:middle;" />
<span style="color:#555555">Created with</span> Wolfram <span \
style="font-style: italic;">Mathematica</span> 8.0
</a>
</div>
</body>
</html>
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:
\documentclass{article}
\usepackage{amsmath, amssymb, graphics, setspace}
\newcommand{\mathsym}[1]{{}}
\newcommand{\unicode}[1]{{}}
\newcounter{mathematicapage}
\begin{document}
This is an equation: \(f(x) = x^2\). We let \(x \in [0,1]\) for this \
example.
\end{document}
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.
EDIT:
This conversion rule for HTML might be more useful:
"Text" -> {"<p class=\"text\">\n",
If[MatchQ[#, _FormBox],
"$" <> Convert`TeX`BoxesToTeX[#[[1]]] <> "$", #] &, "\n</p>"}
Using that we obtain:
<p class="text">
This is an equation: <span><span>$f(x) = x^2$</span></span>. We let
<span><span>$x \in [0,1]$</span></span> for this example.
</p>
Question now is... how do we get rid of the span tag?
ConversionRules
! $\endgroup$lang-tex
, per this answer on meta.so. It is listed under the optional extensions listed near the bottom. $\endgroup$