New answers tagged

2

It is not a good idea to try to run a notebook in command line mode. Whether or not it is possible, it is just not a good idea because notebooks can only be handled by the Front End, which is not a command line tool. If you go this route anyway, expect difficulties. The usual way is to extract the code you want to run into a plain text .m file and run ...


2

Here is a solution written in the spirit of the answer by george2079 but NOT relying upon the buggy NBImport.exe executable. Instead it uses FrontEnd for converting Notebooks into plain text using the findings described here: findInNBFile::cntconv = "Failed to extract plain text from `1`"; findInNBFile[NBFilePath_String, stringPattern_, resPerFile_Integer: ...


2

Begin may be what you need here, as it allows you to define variables inside a given Context: a = 3; Begin["MyContext`"]; {a, b, c} = {1, 2, 3}; Print@{a, b, c}; End[]; Print@{a, b, c} Print@{MyContext`a, MyContext`b, MyContext`c} {1,2,3} {1,b,c} {MyContext`a,2,3} There are a couple of important caveats: If you have already given a ...


5

As Kuba notices in the comment, undocumented FrontEnd`ExportPacket allows conversion of a whole Notebook into plain text: nb = NotebookGet@EvaluationNotebook[]; First[FrontEndExecute[FrontEnd`ExportPacket[nb, "PlainText"]]] "nb=NotebookGet@EvaluationNotebook[]; First[FrontEndExecute[FrontEnd`ExportPacket[nb,\"PlainText\"]]]" But it isn't the whole ...


2

A couple of additional solutions. The first, with FindList, is probably the simplest and quickest. Using FindList searchDir = "<NB dir>"; fnames = FileNames["*.nb", searchDir, 2]; Length@fnames sres = {#, FindList[#, {"curve"}, WordSearch -> False]} & /@ fnames; sres = Select[sres, Length[#[[2]]] > 0 &]; Grid[sres, Dividers -> All, ...


4

For producing a NB file without line breaks you can Get it as a Notebook expression, then Export it with PageWidth -> Infinity as "Package" (other possible options does not work correctly: Export ignores PageWidth -> Infinity when exporting as "NB" and corrupts the code when exporting as "Text"): Export["document.nb", Get["ExampleData/document.nb"], ...


4

Here is an approach which does not rely on the NBImport.exe (which actually performs importing of the NB files as "Plaintext" under the hood) and performs all the operations in the Kernel only. Currently NBImport.exe contains a bug due to which it returns $Failed when have to import a NB file with non-ASCII file path. I'm also not sure how it interprets ...


7

Here is a way to search from within mathematica: notebooks = Quiet@FileNames["*.nb", NotebookDirectory[], 2]; Monitor[Select[ Table[{nb, StringJoin@Select[ StringSplit[Import[nb, "Plaintext"], "\n"] , ((If[#, Print["match on:", nb]]; #) &@ StringMatchQ[#, "*NIntegrate*"]) &, 5]}, {nb,notebooks}], #[[2]] ...


0

This seems to work as intended: style = CurrentValue[EvaluationNotebook[], StyleDefinitions]; Export["001_sty.nb", style, "Text"]; and style = Import["001_sty.nb", "Text"]; SetOptions[ EvaluationNotebook[], StyleDefinitions -> ToExpression @ style ]; Anyway, I think that a crash or an internal self test error, when a simple error message ...


4

To replicate the Ctrl+A Enter execution behaviour automatically you can run Solve.nb from a package file, say runSolve.m, containing the code below. While Wolfram Language Scripts are straightforward, this method has the advantage that output can be saved in the notebook as if it was being run manually. (Developer`CellInformation usage thanks to Arnoud ...


2

What you want, I think, is the cell option ShowCellLabel -> False. You can edit the stylesheet to add the option to the styles "Input" and "Output" in the "Printout" environment. Or you can add them to a notebook, assuming it has the default style definitions, as follows: SetOptions[EvaluationNotebook[], StyleDefinitions -> ...


2

Evaluate this and then save as another nb, open the new nb file, and then save as PDF SetOptions[InputNotebook[], CellLabelAutoDelete -> True];


3

Use CounterBox["Page", CounterFunction :> FrontEnd`CapitalRomanNumeral] in setting the PageFooter option as follows SetOptions[EvaluationNotebook[], PageFooters -> {{ None, Cell[TextData[CounterBox["Page", CounterFunction :> FrontEnd`CapitalRomanNumeral]], "Footer", CellMargins -> {{0, Inherited}, {Inherited, ...


11

This appears to be a difference in parsing between the frontend and the kernel. Compare (in a notebook) HoldForm[\[LeftCeiling]x\[RightCeiling] + 1] (* Ceiling[x] + 1 *) with Get[StringToStream["HoldForm[\[LeftCeiling]x\[RightCeiling] + 1]"]] (* Ceiling[x] (+1) *) where the latter has multiplication instead of addition. One possible workaround is ...


2

It seems to me that setenv is being used here to set the values of a series of helper variables that are then used by the other functions in the code. This (is awful but) works within a single notebook because all those variables are visible to all functions. I suspect, however, that once you put the code in a package, you run into context problems. Those ...


3

I augmented Mr.Wizard's answer with a "Back" button: With[{nb:=SelectedNotebook[]}, DynamicModule[{token,backcell}, Column[{ Button["Find First", (* Expand selection to whole word *) SelectionMove[nb,All,Word]; (* Create notebook sub-object based on current selection *) ...


0

Just use Rule (->) instead of RuleDelayed (:>) Cell[TextData[{ "Look at this ", ButtonBox["page", BaseStyle->"Hyperlink", ButtonData->{ URL[StringJoin["file://", FileNameJoin[{NotebookDirectory[], "subdir/target.html"}] ] ], None}],"..." }], "Text"] //CellPrint ...


0

In order to see the code in the demonstration files, you simply click on the right bar of the section with the code, and select 'convert to input form'... Then you can copy the entire code snippet and paste it into your new file, where it will then get syntax formatting & coloring.


2

Another way which makes a docked cell (much like a package notebook): SetOptions[EvaluationNotebook[],DockedCells -> Cell[ BoxData@ ToBoxes@ DynamicModule[{cell$}, Grid[{ {Dynamic@ActionMenu["Functions", (#[[1]]:>(SelectionMove[EvaluationNotebook[],Before,Notebook,AutoScroll->False]; ...


5

Similar approach to Mr.Wizard's but searching only code and input cells. Put that procedure inside joker.m file, as described in 72914 or as a procedure for Button in a Palette. Module[{name, nb}, nb = InputNotebook[]; Label["readName"]; name = NotebookRead[nb]; If[name === {}, FrontEndExecute@FrontEndToken[nb, "ExpandSelection"]; Goto["readName"] ...


12

If the first appearance of the Symbol in the Notebook is the definition, or close enough to the definition to be convenient, you could use the following Palette button to find that first appearance for whatever token the cursor is within. With[{nb := SelectedNotebook[]}, DynamicModule[{token}, Button["Find First", SelectionMove[nb, All, Word]; ...


2

Here is a slight modification of Karsten's answer. Previously I felt it was too similar, but I suppose it cannot hurt to post it. The main difference is that I avoid MakeExpression. I also like the alternative in your question, which is to use StringToStream. read[cObj_] := DeleteCases[#, HoldComplete[Null]] &@( ToExpression[#, InputForm, ...


3

Instead of SelectionMove and NotebookSelection one can use NotebookRead[PreviousCell[]] and then cleanup the text returned by the ExportPacket. Thread @ MakeExpression[ "{" <> StringReplace[ First[FrontEndExecute[FrontEnd`ExportPacket[NotebookRead[PreviousCell[]], "InputText"]]], {"\r\n " -> "", "\r\n" -> ","}] <> "}", ...


2

There is no reason to keep your Input inside of Manipulate, you don't want a dialog show up every time it updates, right? Probably you need something like this: ClearAll[f, x, \[Alpha]] f[x_, \[Alpha]_] = Input["Please input a function of x to plot with one parameter \ \[Alpha] to manipulate"]; Manipulate[Plot[f[x, \[Alpha]], {x, 0, 1}], {\[Alpha], 0, ...



Top 50 recent answers are included