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:
"HTMLFiles/xhtml-math11-f.dtd">
Untitled
type="text/css" />
This is an equation: src="HTMLFiles/m0001756441_1.gif" alt="m0001756441_1.gif" width="51" \
height="15" style="vertical-align:middle" />. We let \
x ∈ [0,1] for this example.
style="color:#000; text-decoration:none;">
height="21" style="padding-right:2px; border:0px solid white; \
vertical-align:middle;" />
Created with Wolfram
style="font-style: italic;">Mathematica 8.0
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.
This conversion rule for HTML might be more useful:
"Text" -> {"\n",
If[MatchQ[#, _FormBox],
"$" <> Convert`TeX`BoxesToTeX[#[[1]]] <> "$", #] &, "\n
"}
Using that we obtain:
This is an equation: $f(x) = x^2$. We let
$x \in [0,1]$ for this example.
Question now is... how do we get rid of the span tag?
Answer
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:
"ConversionRules" -> {
"" -> {"", ""}
}
So now, evaluating
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",
"FullDocument" -> False,
"ConversionRules" -> {
"Text" -> {"\n", If[MatchQ[#, _FormBox], "$" <> Convert`TeX`BoxesToTeX[#[[1]]] <> "$", #] &, "\n
"},
"" -> {"", ""}
}
]
Now we obtain the desired result:
This is an equation: $f(x) = x^2$. We let $x \in [0,1]$ for this example.
Comments
Post a Comment