15
$\begingroup$

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?

$\endgroup$
7
  • 8
    $\begingroup$ Use expr //InputForm //SequenceForm $\endgroup$
    – Carl Woll
    Commented Jul 2, 2017 at 23:50
  • $\begingroup$ @CarlWoll Fun,that is a workaround deserved to be accepted..Thanks very very much... $\endgroup$
    – yode
    Commented Jul 2, 2017 at 23:57
  • 1
    $\begingroup$ 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. $\endgroup$
    – MarcoB
    Commented Jul 3, 2017 at 3:42
  • $\begingroup$ @MarcoB Yes,I'd like to use your solution.Thanks.Of course,output // InputForm // f is enogh for me. $\endgroup$
    – yode
    Commented Jul 3, 2017 at 3:53
  • $\begingroup$ Strongly related: (a/55545). $\endgroup$ Commented Jul 4, 2017 at 14:14

3 Answers 3

12
$\begingroup$

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

$\endgroup$
14
$\begingroup$

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

$\endgroup$
3
  • $\begingroup$ Well,I learn a ton of *Form today.Thanks. :) $\endgroup$
    – yode
    Commented Jul 3, 2017 at 12:17
  • $\begingroup$ @yode, does PrettyForm work in your desktop version? if not i I will delete that part. $\endgroup$
    – kglr
    Commented Jul 3, 2017 at 12:20
  • $\begingroup$ @kglr it does, nice find. $\endgroup$
    – Kuba
    Commented Jul 3, 2017 at 12:26
9
$\begingroup$

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?

$\endgroup$
3
  • 6
    $\begingroup$ 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"]] &. $\endgroup$
    – John Fultz
    Commented Jul 9, 2017 at 5:19
  • $\begingroup$ @JohnFultz valid points but wasn't the problem for me so far as I used it only for preview or manual gutting. $\endgroup$
    – Kuba
    Commented Jul 10, 2017 at 10:54
  • 4
    $\begingroup$ 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. $\endgroup$
    – John Fultz
    Commented Jul 31, 2017 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.