10
$\begingroup$

TLDR or simply stated: how do I export equation/section numbers to HTML? The built-in function doesn't seem to do it.

Longer form of the question: So I've been tinkering around with the built in stylesheets and the Export command and I've noticed that in principle equation numbering should work.

When you look at PublicationDefault.nb or Notebook.nb stylesheets you'll notice the use of the IncludeCount function, which looks like it should print a number into 'EquationNumberedLabel' class of HTML code.

Here's what I'm talking about:

Cell[StyleData["EquationNumbered", StyleDefinitions -> StyleData["Equation"]],
 ConversionRules:>{
  "HTML" -> {{"", ""}, {"<table class='EquationNumbered'><tr><td>", Automatic, 
  IncludeCount[
      "</td><td class='EquationNumberedLabel'>(`1`)</td></tr></table>", 
       "EquationNumbered"]}}}, ...]

I've tried exporting it with commands:

Export["test.html", SelectedNotebook[], "HTML"]

and

Export["test.html", SelectedNotebook[], "HTML", "MathOutput" -> "TeX"]

both don't seem to give me the equation numbers. Is this functionality really not intended to work?

Edit: You can also use the built in menu command from File->Save. It still doesn't render numbers. It doesn't even render se

$\endgroup$

1 Answer 1

2
$\begingroup$

Appears like this is not possible directly out of Mathematica.

If you have a document that consists purely of equations like this:

eqs

Then you can export to XML, then import the XML and modify the DOM, then export to HTML again.

First save to XML:

save

Then we need a method to insert this javascript into the doc:

function loadTags(){
    for(i=0; i<document.getElementsByTagName("p").length;i++) { 
        document.getElementsByTagName("p")[i].innerHTML = 
        document.getElementsByTagName("p")[i].innerHTML + 
            '<span style="position: absolute; right: 40px;">('+ i 
            +')</span>'};
 }

First read in the XML file as a string:

o = OpenRead["eqs.xml"]
h = ReadString[o]
Close[o]

This monstrosity is the escaped version of the above js with a script tag:

s = "<script> \nfunction loadTags(){\n\nfor(i=0; \
    i<document.getElementsByTagName(\"p\").length;i++) { \n\
    document.getElementsByTagName(\"p\")[i].innerHTML = \
    document.getElementsByTagName(\"p\")[i].innerHTML + '<span \
    style=\"position: absolute; right: 40px;\">(' + i + ')</span>'};\n\n}\
    \n\n\n</script>"

Mathematica is really good at finding things and inserting other things:

rules = {"<body" -> "<body onload=\"loadTags()\"", 
         "</body>" -> StringJoin["</body>", s]}

the rules will find the body start tag and call out custom js onload. they also find the ending body tag and insert our script.

then we write out or doctored XML file to HTML:

str = OpenWrite["numbered2.html"]
WriteString[str, StringReplace[h, rules]]
Close[str]

resulting in:

result

This model would need to adapt to different DOMs- like when other paragraph elements which shouldn't be numbered are present.

$\endgroup$
1
  • $\begingroup$ I really appreciate the input, at least we've got something that works! Thank you! $\endgroup$
    – Ranza
    Feb 9, 2016 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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