Hot answers tagged

40

Double click the output cell instead EDIT: From murrays comment: tutorial/WorkingWithCells: "To specify which cells remain visible when the cell group is closed, select those cells and double-click to close the group."


28

You can create a palette to show/hide all the input cells in the selected notebook. CreatePalette[ Column[{ Button["Hide code", { NotebookFind[SelectedNotebook[], "Output", All, CellStyle]; FrontEndExecute[FrontEndToken[SelectedNotebook[], "SelectionCloseUnselectedCells"]] }], Button["Show code", ...


23

First of all, I agree, as OP mentioned in his comment, ANTLR is one of the proper ways to go. Now for this specific task, it might be easier to just compose a parser in the "dirty" way, except we don't have to go so far to regex. In my opinion Mathematica's StringExpression is much more powerful and very suitable for the job. All we have to do is (as OP ...


22

You can setup CellEpilog to automatically advance a cell after evaluating the current one. That way, you don't need to press the down arrow after evaluating a cell. SetOptions[EvaluationNotebook[], CellEpilog :> SelectionMove[EvaluationNotebook[], Next, Cell]]


21

This syntax was deprecated in the version 6.0 era. According to the legacy documentation, For example, in version 5.2, the following strings are interpreted differently string1 = "first line second line" string2 = "\<first line second line\>"


20

Intro One day I was playing with Developer` package and found DateSetter which uses some kind of floating elements that I found useful. After taking a look at a source code I found out it was FrontEnd`AttachedCell. From the code one could learn enough to create something useful: Usage FrontEnd`AttachCell[ parentObject, (*Box or Cell ...


18

The following seems to work, however I think it's not general enough: At a clean nb, enter: For[i = 0, i < 4, i++, Print[{i, {33, i}}]] For[i = 0, i < 4, i++, Print[Graphics[Circle[], ImageSize -> 20]]] And then retrieve the Print[ ] output as: c = Cases[NotebookRead /@ Cells[GeneratedCell -> True], Cell[___, "Print", ___]]; ToExpression /@ ...


17

You can use this to create a functionality which will fit your need the best. Here's how you can preview your input cell with c highlighted Red. CellPrint[ NotebookRead @ PreviousCell[] /. "c" -> InterpretationBox[ StyleBox["c", FontColor -> RGBColor[1, 0, 0]], c ] ] You can even evaluate such cell. General ...


17

Update I have incorporated Kuba's improvement into the code. Here is how I would do it. In a working notebook (not the target notebook) put the following code. With[{nb = target}, SetOptions[nb, ShowCellBracket -> False]; SetOptions[#, CellOpen -> False] & /@ Cells[nb, CellStyle -> "Input"];] With[{nb = taget}, SetOptions[nb, ...


16

"Style" /. Developer`CellInformation[nb] where nb is the NotebookObject for the notebook you care about. Since you could have multiple cells selected, this will always return a list. It's also possible to have no cells selected, in which case CellInformation will return $Failed and the above code would throw an error.


16

I think I found it but I'd be more than happy to look at other alternatives if any provided: Shift + Ctrl + O to open Options > Notebook options > Evaluation options > Global preferences from the drop down menu at the top Change InitializationCellEvaluation to True and InitializationCellWarning to False The next time the .nb file is launched, the ...


16

This took some digging but at least in Version 7 the FrontEnd command is FT, e.g.: FE`FT["Plot"] You can read the definition with Definition[FE`FT]. If you want only the Box form itself we can modify it accordingly (here for version 7): templateCell[name_String] := Module[{template}, If[! StringQ@ToExpression[name <> "::usage"], $NewMessage[...


16

Typesetting in Text cells Cell[TextData[Cell[BoxData[ FormBox[ RowBox[{ FractionBox["1", "N"], RowBox[{ UnderoverscriptBox["\[Sum]", RowBox[{"i", "=", "1"}], "N"], FractionBox[ RowBox[{ SubsuperscriptBox[ OverscriptBox["x","~"], "j", "i"], "(", RowBox[{"k", "|", "k"}], ")"}], SqrtBox[ ...


16

The case when the evaluation is finished Some of my calculations take hours or even days, so I don't want to restart the entire evaluation just to add Timing[] to it. If your code generates an Output Cell then you can find at least an upper bound of the evaluation time as the difference between last CellChangeTimes of the Output and Input Cells. It can ...


15

You can use something like this. (I have added extra steps to demonstrate practical usage.) Selectable code x = 3 y = x + 2; Clear[x, y, z] SelectionMove[EvaluationNotebook[], Previous, Cell, 3]; z = ToExpression@First@NotebookRead[EvaluationNotebook[]]; y + z Addendum Further to xzczd's comment, you can also use a cell tag :-


15

TL;DR; To set ShowGroupOpener on a global level, previously done by CurrentValue[$FrontEnd, ShowGroupOpener] = True, now use: CurrentValue[$FrontEnd, {StyleHints, "GroupOpener"}] = "OutsideFrame" This is what preferences menu does too. Further explanation The direct cause is Core.nb / All / Working style which, as a stylesheet setting, comes between the ...


14

If your keyboard has a numeric keypad, or a way to emulate one by using a function key, you can use Shift+Enter on the numeric keypad. This keystroke will evaluate a cell if you're in an evaluatable cell, or move to the next evaluatable cell if you're not. It would still be two keystrokes to move then evaluate, but it'd be the same keystroke.


14

Version 9 offers a couple of new ways to access cells that are useful to meet the needs of this question. The first way involves the use of the Cells and EvaluationCell functions. They can be used in the present context like this: previousCell[] := Cells @ EvaluationNotebook[] /. {___, prev_, EvaluationCell[], ___} :> ToExpression @ First @ ...


14

Evaluate SetOptions[$FrontEnd, EvaluationCompletionAction -> "ShowTiming"] and then the evaluation time for the last evaluation will be shown in the lower left corner of the notebook: Like many front end options, this setting is sticky. Set it once, and forget it; it also works across all versions of Mathematica installed.


13

It is not straightforward to access the clipboard contents but one way, that doesn't involve writing and reading from temporary notebooks is to use the undocumented ClipboardNotebook function: CellPrint /@ StringSplit[NotebookGet[ClipboardNotebook[]][[1, 1, 1]], "\n"]; See also this answer which mentions some caveats. This prints strings as text cells. ...


12

Another possibility is to modify the notebooks style sheet by defining a new "Screen Environment" that hides cell brackets and closes input cells: SetOptions[ EvaluationNotebook[], StyleDefinitions -> Notebook[{ Cell[StyleData[StyleDefinitions->"Default.nb"]], Cell[StyleData[All, "Working"], MenuCommandKey->"D"], ...


11

This is very straightforward in v9 using Cells and CellObject. Let's start with a notebook with a disjoint selection of the type you're describing: nb = NotebookPut[ Notebook[{ Cell["a", CellTags -> "x"], Cell["b"], Cell["c", CellTags -> "x"]}]]; NotebookFind[nb, "x", All, CellTags] Now, we can get CellObjects for the selected cells ...


11

There are a number of nonlinear filters that are possible. One that might help is the RidgeFilter img=Import["http://i.stack.imgur.com/IyUPZ.jpg"]; ImageAdjust[RidgeFilter[img, 5]] Let's now clean it up a bit: Pruning[Thinning[SelectComponents[Binarize[ImageAdjust[RidgeFilter[img, 5]], 0.094], "Count", # > 800 &]]] The above binarizes the ...


11

Here is a quick way of doing it: NotebookFind[nb = EvaluationNotebook[], "Print", All, CellStyle]; CreateDocument[NotebookRead[nb]]


10

I have to do this all the time. I only have an annoyingly manual way to solve this. On a mac, Alt-click on the cell marker on the right for an InputCell containing the code, this should select ALL input cells. Then go to the Cell -> Cell Properties menu at the top and unselect the Open item. All the input cells should hide. On the right there should ...


10

In version 9: suppose nb is your notebook object (this can be nb=EvaluationNotebook[], but if you're going to be closing input cells you probably want to have an auxilary notebook a choose the correct notebook from Notebooks[]). Then, to close all the Input cells, for example, do cells = Cells[nb, CellStyle->"Input"] SetOptions[#, CellOpen -> False] &...


10

More stable answer, in case of images and stuff evaluated in place in input cells: NotebookDelete /@ Select[Cells[GeneratedCell -> False], StringMatchQ[ First@ FrontEndExecute[ FrontEnd`ExportPacket[NotebookRead@#, "InputText"]], (" " | "\t" | "\r" | "\n" | "\[IndentingNewLine]") ... ] & ] It may be slower than the original ...


10

There is a special cell attribute called CellID. I would suggest using this instead of cell tags to avoid the problem that you mentioned with cell tags being inherited. Here's an example of how Select can be used to retrieve a cell with a specific cell ID: nb = CreateWindow[]; cells = <| "name" -> RandomInteger[10^6], "status" -> ...


10

Thanks to andre's comment (where this link is provided), I now see the effect of those delimiters (I tested it in Mathematica 11 and also some earlier versions). When I add 2 newlines to the box representation of the cell: Cell[BoxData["\"\<a bc\>\""], "Input", CellChangeTimes->{{3.662918813714031*^9, 3.6629188530623317`*^9}}] and switch back ...


10

Summary of the all available information In the hoarier days space-like characters (spaces, newlines, tabs) inside strings were interpreted on input in an odd way: for example single newlines followed by spaces or tabs were converted to a single space. The \<\> syntax was introduced as a way to avoid this: between \< and \> the space-like ...


Only top voted, non community-wiki answers of a minimum length are eligible