Skip to main content
Remove outer Interpretation (copy error)
Source Link
JEM_Mosig
  • 3k
  • 16
  • 28
ClearAll[summaryForm];
summaryForm[name_, icon_, infos_, maxInfos_, form_, expr_] := Interpretation[
  RawBoxes@Quiet@Check[
    Module[{shown, hidden},
      (* convert info-table into summary items *)
      {shown, hidden} = Map[BoxForm`MakeSummaryItem[#, form]&, TakeDrop[infos, maxInfos], {2}];
      (* arrange the summary and make sure the label is gray (therefore StyleBox) *)
      StyleBox[BoxForm`ArrangeSummaryBox[
        name, 
        expr,
        Deploy@Show[icon, 
          (* avoid gray background before mouse-over *)
          Background -> White, 
          (* set standard image size *)
          ImageSize -> Dynamic[{
            Automatic, 
            3.5 CurrentValue["FontCapHeight"]/AbsoluteCurrentValue[Magnification]
          }]
        ],
        shown,
        hidden,
        form,
        (* formatted object should be uasable as input *)
        "Interpretable" -> True
      ], "NonInterpretableSummary"]
    ],
    (* in case something goes wrong *)
    ToBoxes@Panel@Short[expr]
  ],
  expr
]    
ClearAll[summaryForm];
summaryForm[name_, icon_, infos_, maxInfos_, form_, expr_] := Interpretation[
  RawBoxes@Quiet@Check[
    Module[{shown, hidden},
      (* convert info-table into summary items *)
      {shown, hidden} = Map[BoxForm`MakeSummaryItem[#, form]&, TakeDrop[infos, maxInfos], {2}];
      (* arrange the summary and make sure the label is gray (therefore StyleBox) *)
      StyleBox[BoxForm`ArrangeSummaryBox[
        name, 
        expr,
        Deploy@Show[icon, 
          (* avoid gray background before mouse-over *)
          Background -> White, 
          (* set standard image size *)
          ImageSize -> Dynamic[{
            Automatic, 
            3.5 CurrentValue["FontCapHeight"]/AbsoluteCurrentValue[Magnification]
          }]
        ],
        shown,
        hidden,
        form,
        (* formatted object should be uasable as input *)
        "Interpretable" -> True
      ], "NonInterpretableSummary"]
    ],
    (* in case something goes wrong *)
    ToBoxes@Panel@Short[expr]
  ],
  expr
]    
ClearAll[summaryForm];
summaryForm[name_, icon_, infos_, maxInfos_, form_, expr_] := RawBoxes@Quiet@Check[
  Module[{shown, hidden},
    (* convert info-table into summary items *)
    {shown, hidden} = Map[BoxForm`MakeSummaryItem[#, form]&, TakeDrop[infos, maxInfos], {2}];
    (* arrange the summary and make sure the label is gray (therefore StyleBox) *)
    StyleBox[BoxForm`ArrangeSummaryBox[
      name, 
      expr,
      Deploy@Show[icon, 
        (* avoid gray background before mouse-over *)
        Background -> White, 
        (* set standard image size *)
        ImageSize -> Dynamic[{
          Automatic, 
          3.5 CurrentValue["FontCapHeight"]/AbsoluteCurrentValue[Magnification]
        }]
      ],
      shown,
      hidden,
      form,
      (* formatted object should be uasable as input *)
      "Interpretable" -> True
    ], "NonInterpretableSummary"]
  ],
  (* in case something goes wrong *)
  ToBoxes@Panel@Short[expr]
]
Comment on interpretation argument
Source Link
JEM_Mosig
  • 3k
  • 16
  • 28

The last argument x is how the summary should be interpreted, so in the more general case you may want to replace it by something like testfunction[x].

Then testfunction is displayed as

Then testfunction is displayed as

The last argument x is how the summary should be interpreted, so in the more general case you may want to replace it by something like testfunction[x].

Then testfunction is displayed as

Source Link
JEM_Mosig
  • 3k
  • 16
  • 28

The following solution is slightly more general than @Istvàn's answer, and also fixes the icon-size to what seems to be the standard.

First, we define

ClearAll[summaryForm];
summaryForm[name_, icon_, infos_, maxInfos_, form_, expr_] := Interpretation[
  RawBoxes@Quiet@Check[
    Module[{shown, hidden},
      (* convert info-table into summary items *)
      {shown, hidden} = Map[BoxForm`MakeSummaryItem[#, form]&, TakeDrop[infos, maxInfos], {2}];
      (* arrange the summary and make sure the label is gray (therefore StyleBox) *)
      StyleBox[BoxForm`ArrangeSummaryBox[
        name, 
        expr,
        Deploy@Show[icon, 
          (* avoid gray background before mouse-over *)
          Background -> White, 
          (* set standard image size *)
          ImageSize -> Dynamic[{
            Automatic, 
            3.5 CurrentValue["FontCapHeight"]/AbsoluteCurrentValue[Magnification]
          }]
        ],
        shown,
        hidden,
        form,
        (* formatted object should be uasable as input *)
        "Interpretable" -> True
      ], "NonInterpretableSummary"]
    ],
    (* in case something goes wrong *)
    ToBoxes@Panel@Short[expr]
  ],
  expr
]    

Now you can specify automatic formatting for your own function, using

Format[testfunction[x_], StandardForm] := summaryForm[
  "testfunction",
   Graphics[{Circle[], Disk[{0,0},1,{0,x}]}],
  {{"Even: ",EvenQ[x]},{"Value: ",x},{"Prime: ",PrimeQ[x]}},2,
  StandardForm,
  x
]

Then testfunction is displayed as

testfunction[2]

testfunction summary panel

And of course you can use it as input, and the opener works as expected:

testfunction summary panel in equation

(* -15 *)

Using summaryForm it does not matter how your object is structured internally, and, once defined, you can use it to create many different kinds of formatted objects with very little additional code.

Since this Q&A is a bit hard to find if you don't know how this feature is called, I mention a few more functions which have these summary panels: InterpolatingFunction, TimeSeries, EventSeries, DataDistribution.