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.

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.

share|improve this question
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. –  Ajasja Oct 18 '12 at 7:13
I think the online documentation is built using Mathematica Workbench –  cormullion Oct 18 '12 at 9:57
Any news on the project? It seems the project has not been updated for 3 years. –  Kattern 23 hours ago

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

share|improve this answer
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. –  carlosayam Oct 18 '12 at 13:35
@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). –  VF1 Nov 27 '12 at 22:36
@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 –  carlosayam Nov 28 '12 at 0:00
@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. –  carlosayam Nov 28 '12 at 23:21
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) :-) –  Yves Klett Aug 22 '13 at 8:22

Your Answer


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.