I often wish to see the internal representation of Mathematica‘s graphical objects not in the FullForm but in much more readable InputForm having the ability to select parts of the code by double-clicking on it and easily copy this code to a new input Cell. But the default InputForm does not allow this since InputForm is displayed by default as a String, not as Mathematica‘s code. Is there a way to have InputForm displayed as Mathematica‘s code?
I also often wish to see a shortened version of such InputForm where all long lists of coordinates are displayed as the first coordinate followed by number of skipped coordinate values wrapped with Skeleton, all empty Lists removed and all numbers are also shortened for displaying no more than 6 digits. It would be even better to use 6 digits only for coordinates but for color directives such as Hue display only 2 significant digits. For example,
Plot[{Sin[x], .5 Sin[2 x]}, {x, 0, 2 \[Pi]},
Filling -> {1 -> {2}}] // ShortInputForm
should give:
Graphics[GraphicsComplex[{{1.28228`*^-7, 1.28228*^-7}, <<1133>>},
{{{EdgeForm[], Directive[{Opacity[0.2], Hue[0.67, 0.6, 0.6]}],
GraphicsGroup[{Polygon[{{1133, <<578>>}}]}]},
{EdgeForm[], Directive[{Opacity[0.2], Hue[0.67, 0.6, 0.6]}],
GraphicsGroup[{Polygon[{{432, <<556>>}}]}]}}, {{Hue[0.67, 0.6,
0.6], Line[{1, <<431>>}]}, {Hue[0.91, 0.6, 0.6],
Line[{432, <<701>>}]}}}], {AspectRatio -> GoldenRatio^(-1),
Axes -> True, AxesOrigin -> {0, 0},
Method -> {"AxesInFront" -> True},
PlotRange -> {{0, 2*Pi}, {-1., 1}},
PlotRangeClipping -> True,
PlotRangePadding -> {Scaled[0.02], Scaled[0.02]}}]
(note that -0.9999998592131705 is converted to -1., 1.2822827157509358*^-7 is converted to 1.28228*^-7 and Hue[0.9060679774997897, 0.6, 0.6] is converted to Hue[0.91, 0.6, 0.6]).
In this way, I wish to have the output of InputForm as Mathematica‘s code and also have a ShortInputForm function which will give the shortened version of this code. Can anybody help me?
As to the first part of the question, I have found one way to achieve what I want:
Plot[{Sin[x], .5 Sin[2 x]}, {x, 0, 2 \[Pi]}, Filling -> {1 -> {2}}] //
InputForm // StandardForm
UPDATE
The most recent version of the
shortInputFormfunction can be found here.Original post
Here is another, even better solution (compatible with Mathematica 5):
How it works
This solution is based on simple idea: we need to block conversion of such things as
Graphics,Pointand others to typeset expressions in order to get them displayed in the internal form (as expressions suitable for input). Happily, if we do this, the resultingStandardFormoutput is found to be just formatted (two-dimensional)InputFormof the original expression. This is just what is needed!But how to do this?
First of all, this conversion is made by
FormatValuesdefined forSymbols likeGraphics,Pointetc. One can get full list of suchSymbols by evaluating the following:My first idea was just
Blockall theseSymbols (and it works!):But this method leads to the evaluation of all these
Symbols and also evaluates allFormatValuesfor allSymbols in the$ContextPath. I think it should be avoided.Other way to block these
FormatValuesis just to remove context"System`"from the$ContextPath. But it works only if theseSymbols are not resolved yet to the"System`"context. So we need first to convert our expression toString, then remove"System`"context from the$ContextPathand finally convert the string backward to the original expression. Then all newSymbols will be associated with the current$Context(andGraphics,Pointetc. – too, since they are not in the$ContextPath). For preventing context shadowing conflicts and littering the"Global`"context I switch$Contextto"myTemp`"which can be easily cleared if necessary.This is how
myInputFormworks.Now about
shortInputForm. The idea is not just to display a shortened version ofmyInputFormbut also preserve the ability to select and copy parts of the shortened code into new input cell and use this code as it would be the full code without abbreviations. In version 6 and higher it is possible to achieve the latter withInterpretation. For compatibility with pre-6 versions ofMathematicaI have added a piece of code that removes this ability if$VersionNumberis less than 6.The only problem that I faced when working with
Interpretationis that it has noSequenceHoldattribute and so we cannot simply specifySequenceas the second argument forInterpretation. But this problem can easily be avoided by wrapping sequence inListand thenApplyingSequenceto it:Note that I need to specify the exact context for all built-in
Symbols I use because at the moment of calling them the"System`"context is not in the$ContextPath.This ends the non-standard decisions taken me in the development of these functions. Suggestions and comments are welcome!