20
$\begingroup$

In Mathematica 9, a graph is returned as an object with head Graph:

In[1]:= CompleteGraph[8] // Head
Out[1]= Graph

Right-clicking on a Graph object brings a menu with the option "Convert to Graphics". Selecting this option returns a new object which has head Graphics. I need to do this programmatically, but I haven't found any command that, applied to CompleteGraph[8] (to put an example), returns an object with head Graphics, with a plot of the graph.

Is there a command in Mathematica to convert a Graph to a Graphics?

$\endgroup$
0

2 Answers 2

29
$\begingroup$

Well, just after I had posted the question, I found a very simple way to do it:

In[1]:= Show[CompleteGraph[8]] // Head
Out[1]= Graphics
$\endgroup$
0
14
$\begingroup$

Contextual menu bindings are defined in the file here:

FileNameJoin[{$InstallationDirectory,
  "SystemFiles", "FrontEnd", "TextResources", "ContextMenus.tr"}]

Examining the contents of that file, you can discover that the "Convert to Graphics" contextual menu item maps to the Mathematica command GraphComputation`GraphConvertToGraphics. Thus, for example,

GraphComputation`GraphConvertToGraphics[
  Graph[{1 -> 2, 2 -> 3, 3 -> 1}]] // Head

(* Out: Graphics *)

I actually prefer your Show technique, which is more natural and more likely to remain stable between versions, but it is nice to know how to find what commands these contextual menu items map to.

$\endgroup$
1
  • $\begingroup$ +1 Thanks. I imagine that Show probably calls GraphConvertToGraphics internally. $\endgroup$
    – a06e
    Commented May 11, 2013 at 16:54

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.