6
$\begingroup$

How can I open a package as a normal notebook, i.e. without the toolbars and formatting of the built-in .m file interface.

$\endgroup$
5
$\begingroup$

Upon opening you can change the stylesheet and remove the docked cell:

SetOptions[EvaluationNotebook[], DockedCells -> {},StyleDefinitions -> "Default.nb"]

Then if you want to change the "Code" styles to something else, possibly "Input", do this:

NotebookFind[EvaluationNotebook[], "Code", All, CellStyle] 
FrontEndExecute[FrontEndToken["StyleOther"]]
$\endgroup$
2
  • $\begingroup$ It doesn't seem to save, is there a way around running this command manually every time I open the notebook? $\endgroup$ – M.R. Feb 8 '15 at 22:38
  • $\begingroup$ Well perhaps that is a separate question - how to run a bit of code every time a notebook opens... what do you think? $\endgroup$ – M.R. Feb 8 '15 at 22:39
4
$\begingroup$

Here is one way. Using the parseString function from this answer of Mr.Wizard, the following will create a notebook with an entire package's code being placed in a single Code - style cell:

parseString[s_String, prep : (True | False) : True] := 
   FrontEndExecute[UndocumentedTestFEParserPacket[s, prep]]

ClearAll[openPackage];
openPackage[file_String?FileExistsQ] := 
  With[{stringCode = Import[file, "String"]},
     CreateDocument[
        Cell[First@parseString[stringCode, False], "Code"]
     ] /; stringCode =!= $Failed
  ]

For example:

openPackage@FileNameJoin[{FileNameDrop[FindFile["Combinatorica`"], -2], "Combinatorica.m"}]

You can split the code into multiple cells, but that would require a bit more work.

$\endgroup$

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.