consider the following list of Cell[]'s:

cells = {
            , "Input"
            , "Input"]
nb = Notebook[{cells}];
SystemOpen@Export[$HomeDirectory <> "/notebook.nb", nb]


Using the Notebook function, I can create and export a notebook, but there is no syntax highlighting.


1 Answer 1


You need to add BoxData to mark it StandardForm as opposed to a plain text content. Notice that as soon as the notebook is created or exported the text content will be automatically converted to a box expression.

cells = {
   Cell["foo=\"string1\";(*comment*)bar=\"string2\";", "Input"], 
   Cell["foo=\"string1\";(*comment*)bar=\"string2\";", "Input"]
nb = Notebook[MapAt[BoxData, cells, {All, 1}]];

Export[FileNameJoin[{$TemporaryDirectory, "test.nb"}], nb]


enter image description here

  • 1
    $\begingroup$ Interesting. Is this usage of BoxData documented? $\endgroup$ Feb 7 at 14:20
  • 2
    $\begingroup$ @GustavoDelfino I am not sure, I think I learned it from John Fultz's comments somewhere around here and I use it from time to time: mathematica.stackexchange.com/a/271092/5478 $\endgroup$
    – Kuba
    Feb 7 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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