Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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 &isin; [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?

share|improve this question
    
+1 just for letting me know about ConversionRules! –  Szabolcs May 25 '12 at 9:57
    
the span tag is superfluous and will not affect the display of the html, so I wouldn't worry about it. –  rcollyer May 25 '12 at 14:35
    
I changed the highlighter used for the $\TeX$ block to lang-tex, per this answer on meta.so. It is listed under the optional extensions listed near the bottom. –  rcollyer May 25 '12 at 14:52
    
@rcollyer, That is the case with the span tag, but what I want is to have full control over the conversion rules. Is there anywhere where we can see the source code involving the conversion rules? What are the default settings for HTML and TeX? I think this would make it a lot easier to decide how to make them instead of just having very few examples in the doc center. –  jmlopez May 25 '12 at 15:16
1  
Okay, that is the third change in scope of the question. So, do me a favor, and post your update as an answer, and your new question as a separate post. –  rcollyer May 25 '12 at 16:32

2 Answers 2

up vote 7 down vote accepted

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" -> {"<p class=\"text\">\n", If[MatchQ[#, _FormBox], "$" <> Convert`TeX`BoxesToTeX[#[[1]]] <> "$", #] &, "\n</p>"},
        "" -> {"", ""}
    }
]

Now we obtain the desired result:

<p class="text">
This is an equation: $f(x) = x^2$. We let $x \in  [0,1]$ for this example.
</p>
share|improve this answer

The span tag does nothing unless you specifically style it as it is designed to

The <span> tag is used to group inline-elements in a document.

The <span> tag provides no visual change by itself.

The <span> tag provides a way to add a hook to a part of a text or a part of a document.

Using css, you can hook on to that and style those sections. For example,

p.text>span>span
{
  color:blue;
  background-color:red;
}

would produce this

enter image description here

Not my idea of reasonable styling, but MathJax seems to respect the styles of the html the $\TeX$ is placed in.

However, if you really want to remove the spans, then I would do the following

StringReplace[ExportString[...],
  "<span><span>$" ~~ t : Shortest[__] ~~ "$</span></span>" :> 
      "$" <> t <> "$"
]

Note the use of Shortest.

share|improve this answer
1  
This is a way to remove those tags, +1 for that. But I'm really looking for a better understanding of those darn conversion rules. I'm pulling my hair out just to know what the default settings are. How does it map the notebook to html. –  jmlopez May 25 '12 at 15:23
    
@jmlopez I honestly don't know, I have not explored that part of mathematica. –  rcollyer May 25 '12 at 15:24
    
hi, do you know how to designate the title of a html page, when Exporting one notebook to a html file. I've tried the option` "HeadElements" -> "<title>My title</title>"` however it's override by the default <title> Untitled </title> –  HyperGroups Dec 24 '13 at 5:24
    
@HyperGroups no I don't. I have never looked at it. –  rcollyer Dec 24 '13 at 13:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.