15

I always use InputForm to check the result object,such as Dataset or Graphics or other objects.But if you are in the result of InputForm,you cannot use the Front-End function of balance the bracket. Note this gif

enter image description here

When I double click the input line, I will select all content just in this bracket.But when I'm in result of InputForm,I will select all line. Of course I can copy the output of InputForm as another new input,but which will make the notebook more mess.

Any method can make the output of InputForm support the function of balance the bracket?

yode
  • 26,686
  • 4
  • 62
  • 167
  • 8
    Use expr //InputForm //SequenceForm – Carl Woll Jul 02 '17 at 23:50
  • @CarlWoll Fun,that is a workaround deserved to be accepted..Thanks very very much... – yode Jul 02 '17 at 23:57
  • 1
    I tend to use output // InputForm // List. Although it adds an extra pair of enclosing braces, I find it a small price to pay to use those features you desire. – MarcoB Jul 03 '17 at 03:42
  • @MarcoB Yes,I'd like to use your solution.Thanks.Of course,output // InputForm // f is enogh for me. – yode Jul 03 '17 at 03:53
  • Strongly related: (a/55545). – Alexey Popkov Jul 04 '17 at 14:14
  • @AlexeyPopkov Thanks,that's a very good link. :) – yode Jul 04 '17 at 14:18
  • Just a point on what's actually going on to cause this: InputForm creates a Cell with a String as its cell content, instead of BoxData and the front-end only provides many features on boxes. It's worth noting as you might see other odd behaviors like this and wonder why they occur and that distinction might explain them. That also means you can go into the Cell and stick a BoxData before the content, too, and get your boxes back. – b3m2a1 Jul 05 '17 at 04:24

3 Answers3

14

If you have Version 11 you can use the function PrettyForm instead of processing InputForm output to get the desired result:

Needs["GeneralUtilities`"]

Interpolation[{1, 2, 3, 4}] // PrettyForm

enter image description here

ListLinePlot[Range[5]^2] // PrettyForm

Mathematica graphics

For earlier versions you can also wrap InputForm with DisplayForm:

ListLinePlot[Range[5]^2] // InputForm // DisplayForm

Mathematica graphics

kglr
  • 394,356
  • 18
  • 477
  • 896
12

Per my comment, I like using expr //InputForm //SequenceForm, but another similar possibility is to use a custom head with a custom MakeBoxes rule. For instance, let's call the custom form myInputForm. Then, we can define:

myInputForm /: MakeBoxes[myInputForm[expr_], StandardForm] := MakeBoxes[InputForm[expr]]

We also need to add myInputForm to $OutputForms:

Unprotect[$OutputForms];

AppendTo[$OutputForms, myInputForm];

Protect[$OutputForms];

Now, myInputForm gets stripped before being stored in Out. For instance:

Interpolation[{1,2,3,4}]
% //myInputForm
% //Head

InterpolatingFunction[{{1, 4}}, <>]

InterpolatingFunction[{{1, 4}}, { 5, 3, 0, {4}, {4}, 0, 0, 0, 0, Automatic, {}, {}, False}, {{1, 2, 3, 4}}, {{ 1}, {2}, {3}, {4}}, {Automatic}]

InterpolatingFunction

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
9

Carl's tip seems to be the best quick solution.

Very often I find syntax/style highlighting very useful too so I use:

CellPrint[ExpressionCell[InputForm@#, "Input"]] &

to get everything what Input cells offer:

Plot[x, {x, 0, 1}, PlotPoints -> 10, MaxRecursion -> 1
] // CellPrint[ExpressionCell[InputForm@#, "Input"]] &

enter image description here

related:

How to see a code preview (in Experimental`Explore[] or related GUI)

How to use an ExpressionCell to display e.g. an Input cell inside a generated output?

Kuba
  • 136,707
  • 13
  • 279
  • 740
  • 6
    I'm not a huge fan of this because of the combination of CellPrint and the "Input" style. This creates Input cells which have GeneratedCell->True and CellAutoOverwrite->True (this is documented behavior of CellPrint). If you start using these cells for real, then you may have unpleasant side effects...like your Input cells disappearing when you evaluate inputs above them. If you really want Input cells, I'd recommend a NotebookWrite based solution. The equivalent formulation would be NotebookWrite[EvaluationNotebook[], Cell[BoxData[ToString[#, InputForm]], "Input"]] &. – John Fultz Jul 09 '17 at 05:19
  • @JohnFultz valid points but wasn't the problem for me so far as I used it only for preview or manual gutting. – Kuba Jul 10 '17 at 10:54
  • 4
    Many (many, many) years ago, we made a big mistake and had cells written by NotebookWrite have GeneratedCell->True. It caused a whole class of mysterious-to-our-users bugs where cells would just seem to "randomly" disappear after evaluation and data was getting lost. It took quite a long time after we fixed NotebookWrite to stop seeing user reports like this. So, hopefully, you'll understand my sensitivity about the subject. – John Fultz Jul 31 '17 at 17:52