Some types of objects have special input/output formatting in Mathematica. This includes Graphics
, raster images, and, as of Mathematica 8, graphs (Graph[]
). Unfortunately large graphs may take a very long time to visualize, much longer than most other operations I'm doing on them during interactive work.
How can I prevent auto-layout of Graph[]
objects in StandardForm and TraditionalForm, and have them displayed as e.g. -Graph-
, preferably preserving the interpretability of the output (perhaps using Interpretation
?). I think this will involve changing Format
and/or MakeBoxes
in some way, but I was unsuccessful in getting this to work.
I would like to do this in a reversible way, and preferably define a function that will return the original interactive graph display when applied to a Graph
object (not the same as GraphPlot
, which is not interactive).
On a related note, is there a way to retrieve Format/MakeBoxes definitions associated with certain symbols? FormatValues
is one relevant function, but it is empty for Graph
.
Sample session:
In[1]:= Graph[{1->2, 2->3, 3->1}]
Out[1]= -Graph-
In[2]:= interactiveGraphPlot[%] (* note that % works *)
Out[2]= (the usual interactive graph plot should be shown here)
Though I do not have Mathematica 8 to try this in, one possibility is to use this construct:
Unprotect[Graph]
MakeBoxes[g_Graph, StandardForm] /; TrueQ[$short] ^:=
ToBoxes@Interpretation[Skeleton["Graph"], g]
$short = True;
Afterward, a Graph
object should display in Skeleton form, and setting $short = False
should restore default behavior.
Hopefully this works to automate the switching:
interactiveGraphPlot[g_Graph] := Block[{$short}, Print[g]]
Mark's concern about modifying Graph
caused me to consider the option of using $PrePrint
. I think this should also prevent the slow layout step from taking place. It may be more desirable, assuming you are not already using $PrePrint
for something else.
$PrePrint =
If[TrueQ[$short], # /. _Graph -> Skeleton["Graph"], #] &;
$short = True
Also conveniently, at least with Graphics
(again I cannot test with Graph
in v7) you can get the graphic with simply Print
. Here, shown with Graphics:
g = Plot[Sin[x], {x, 0, 2 Pi}]
(* Out = <<"Graphics">> *)
Then
Print[g]
I left the $short
test in place for easy switching via a global symbol, but one could leave it out and use:
$PrePrint = # /. _Graph -> Skeleton["Graph"] &;
And then use $PrePrint = .
to reset the default functionality.