4
$\begingroup$

consider the following list of Cell[]'s:

cells = {
         Cell[
              "foo=\"string1\";(*comment*)bar=\"string2\";"
            , "Input"
         ]
        ,Cell[
              "foo=\"string1\";(*comment*)bar=\"string2\";"
            , "Input"]
        };
nb = Notebook[{cells}];
SystemOpen@Export[$HomeDirectory <> "/notebook.nb", nb]

d

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

$\endgroup$

1 Answer 1

5
$\begingroup$

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]

NotebookOpen@%

enter image description here

$\endgroup$
2
  • 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.