I use notebook "Save as ... (html)" to convert my .nb to web. Formulas and plots get converted to images, which is fine. But code (input cells) get converted too into images. I would like people to be able to copy and paste my code, what can I do?

Not sure how to use (in a generic scenario) what is mentioned in here.

edit Got something very close working:

  Export[NotebookDirectory[] <> "/test.html", 
    EvaluationNotebook[], "HTML",
    "ConvertReverseClosed" -> True,
    "ConversionRules" -> {"Input" -> {"<code>", DisplayForm[#] &, 

if someone can improve on this it will be awesome.

second edit

This is the code I now have in a Palette. The context of the question is here. The code above is now in the proper place to export to HTML, including the CDFs, in (* 1 *).

 Button[Style["Blog it", 12, FontFamily -> "Times"],
    nb = InputNotebook[],
    fpath = NotebookDirectory[InputNotebook[]],
    htmlName = 
     StringDrop[NotebookFileName[InputNotebook[]], -3] <> ".html",
   generateCDF[nb_, name_] := Module[{sel},
     sel = SelectionMove[nb, Next, Cell];
     Message[generateCDF::info, name];
      FileNameJoin[{fpath, name}],
     SelectionMove[nb, "Input", Next, CellStyle]
   generateCDF::info = "Generating CDF `1`";
   generateCDFs[nb_] := Module[{sel, curID},
     SelectionMove[nb, Before, Notebook];
     sel = NotebookFind[nb, "Input", Next, CellStyle];
     While[ MatchQ[sel, _NotebookSelection],
      curID = CurrentValue[sel, CellID];
      sel = Switch[CurrentValue[sel, CellTags],
        _String, generateCDF[nb, CurrentValue[sel, CellTags]],
        _, NotebookFind[nb, "Input", Next, CellStyle]
   exportHTML[nb_] := Module[{
      rules = {
        "Input" -> {
          "\n<pre class=\"code\">\n",
          DisplayForm[#] &,
          } (* 1 *)
     Message[exportHTML::info, FileNameTake[htmlName]];
      "CSS" -> None,
      "MathOutput" -> "GIF",
      "ConvertReverseClosed" -> True,
      "ConversionRules" -> rules

   exportHTML::info = "Generating HTML `1`";
   exportHTML::done = "Export to HTML done";
  Appearance -> "Palette"]

The problem I have is that it generates funny characters in the HTML. I have tried several "encoding" options without luck. This is an example of what I get:

web page screenshot

I am not worried about the tags or formatting as I know how to deal with those in my Rails app or in CSS. But the "wrongly encoded" characters are my issue. I committed the sample.nb and sample palette code to github if any one wants to test it.

  • $\begingroup$ Good question. I'd also like to know how one can implement the functionality of the mathematica online documentation, where the input cells are images and then one can click on the image and a box with copyable code appears. $\endgroup$
    – Ajasja
    Oct 18, 2012 at 7:13
  • 1
    $\begingroup$ I think the online documentation is built using Mathematica Workbench $\endgroup$
    – cormullion
    Oct 18, 2012 at 9:57
  • 1
    $\begingroup$ Any news on the project? It seems the project has not been updated for 3 years. $\endgroup$
    – Kattern
    Jul 2, 2015 at 12:47

1 Answer 1


This stupid piece of code doesn't work very well:

Export["test.xhtml",  EvaluationNotebook[], "MathOutput" → "DisplayForm"]

and crashes Mathematica 8 reliably too, but the files it creates contain selectable text - here selecting something in the browser...

enter image description here

  • $\begingroup$ Thanks cormullion. I was thinking on something along those lines. I got the following not crashing... Export[NotebookDirectory[] <> "/test.html", EvaluationNotebook[], "HTML", "ConvertReverseClosed" -> True, "ConversionRules" -> {"Input" -> {"<p class=\"text\">\n", # &, "\n</p>"}}] but produces lots of RowBox[...] output, so I bet I need to convert from RowBox to plain text somehow. $\endgroup$
    – carlosayam
    Oct 18, 2012 at 13:35
  • $\begingroup$ @caya Have you tried exactly what cormullion recommends? It works on my machine (for some reason, I had to replace his arrow with my ->, though). $\endgroup$
    – VF1
    Nov 27, 2012 at 22:36
  • $\begingroup$ @VF1, hum, not exactly as I have more options to supply and was trying "HTML" instead "XHTML". I will give it another go. If it works will be more than happy to accept answer. Tnx $\endgroup$
    – carlosayam
    Nov 28, 2012 at 0:00
  • $\begingroup$ @VF1, that approach crashes consistently. The best I can do is to convert to HTML as mentioned in my first edit, but still the special characters are not properly mapped. $\endgroup$
    – carlosayam
    Nov 28, 2012 at 23:21
  • $\begingroup$ Your first line sounds like a mantra for a lot of my code (as well as the "but it works after a fashion anyhow" part) :-) $\endgroup$
    – Yves Klett
    Aug 22, 2013 at 8:22

Your Answer

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

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