Group Abstract Group Abstract

Message Boards Message Boards

Trying to reproduce the behavior of Graph objects with visual representation

Posted 1 year ago

Hello everyone.

I am working on a package dealing with a specific type of object that has a visual representation, so I'm trying to reproduce the behavior of the Graph objects in Mathematica. By that, I mean that when an output cell contains a graph it's visually represented, and you can embed this visual representation into your code for further computing (instead of having the full list of edges).

So far, this is what I have done.

myObject /: MakeBoxes[myObject[data_], StandardForm] := TemplateBox[
  {
   myObject[data],
   ToBoxes@Deploy@visualRepresentation[myObject[data]]
   },
  "myObject",
  DisplayFunction :> (#2 &),
  InterpretationFunction :> (#1 &)]

It's working properly except that I cannot copy-paste the visual representation and it be evaluated as the object, and I cannot evaluate an output cell. However, I can use % to refer to the output and then it works. Anyone would know how to achieve that?

POSTED BY: Paul Cousin
4 Replies
POSTED BY: Jason Biggs
Posted 1 year ago

It works exactly as I wished and is indeed super simple. Thank you so much!

POSTED BY: Paul Cousin

I was facing the same issue while trying to make the Hypergraph object that behaves exactly like Graph and Tree. I just looked into their FormatValues and copied stuff from there. You can see this hack that overloads GraphicsBox here: https://github.com/WolframInstitute/Hypergraph/blob/main/Kernel/Formatting.m

POSTED BY: Nikolay Murzin
Posted 1 year ago

Excellent, I'll try to use your code as a template. Thank you!

POSTED BY: Paul Cousin
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard