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
shortInputForm
function 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
,Point
and 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 resultingStandardForm
output is found to be just formatted (two-dimensional)InputForm
of the original expression. This is just what is needed!But how to do this? First of all, this conversion is made by
FormatValues
defined forSymbol
s likeGraphics
,Point
etc. One can get full list of suchSymbol
s by evaluating the following:My first idea was just
Block
all theseSymbol
s (and it works!):But this method leads to the evaluation of all these
Symbol
s and also evaluates allFormatValues
for allSymbol
s in the$ContextPath
. I think it should be avoided.Other way to block these
FormatValues
is just to remove context"System`"
from the$ContextPath
. But it works only if theseSymbol
s are not resolved yet to the"System`"
context. So we need first to convert our expression toString
, then remove"System`"
context from the$ContextPath
and finally convert the string backward to the original expression. Then all newSymbol
s will be associated with the current$Context
(andGraphics
,Point
etc. - too, since they are not in the$ContextPath
). For preventing context shadowing conflicts and littering the"Global`"
context I switch$Context
to"myTemp`"
which can be easily cleared if necessary.This is how
myInputForm
works.Now about
shortInputForm
. The idea is not just to display a shortened version ofmyInputForm
but 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 ofMathematica
I have added a piece of code that removes this ability if$VersionNumber
is less than 6.The only problem that I faced when working with
Interpretation
is that it has noSequenceHold
attribute and so we cannot simply specifySequence
as the second argument forInterpretation
. But this problem can easily be avoided by wrapping sequence inList
and thenApply
ingSequence
to it:Note that I need to specify the exact context for all built-in
Symbol
s 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!
At this moment I have come to the following solution:
Now: