6

In the version 10, a lot of functions returned a box with brief information which is very useful.

For example:

Interpolation[{1, 2, 3, 5, 8, 5}]

enter image description here

So what's that little box, and how can I make a function that return a similar box which contains some of the information?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
xslittlegrass
  • 27,549
  • 9
  • 97
  • 186
  • the little box is just a Panel with some info. I'll see if i can find a link to how to make them in your own functions. darren glosemeyer (formerly) at WRI wrote this up a few years ago. – Mike Honeychurch Jul 09 '14 at 22:49
  • and FWIW they are terribly annoying on the RPi, as it oftentimes takes longer to render the box than it does to run the underlying function. – bobthechemist Jul 10 '14 at 01:13

2 Answers2

4

You can use MakeBoxes to format output pretty much however you like. This is what Mathematica does for InterpolatingFunction. You may be able to glean something by inspecting the output cell or from FormatValues[InterpolatingFunction]. Unlike InterpolatingFunction, I included an Interpretation, which means that the output in the notebook may be copied and used. The InterpolatingFunction has a little + button that toggles between information panes. I've included a PaneSelector and an Opener for that feature, in case you wanted that. Just like InterpolatingFunction, this leaves little Dynamic bits littering your notebook.

Toy example:

MakeBoxes[myFN[n_], fmt_] := ToBoxes[
   Interpretation[
    Row[{
      HoldForm[myFN][
       DynamicModule[{open = False},
        Panel[Grid[{{
            Opener[Dynamic[open]],
            Graphics[{Green, Rectangle[]}, 
             ImageSize -> {Automatic, 
               Dynamic[
                3.5*(CurrentValue["FontCapHeight"]/
                   AbsoluteCurrentValue[Magnification])]}],
            PaneSelector[{
              False -> Row[{"Domain: ", Reals}],
              True -> Column[{Row[{"Domain: ", Reals}], 
                              Row[{"Formula: ", myFN[n][\[FormalX]]}]
                       }]},
             Dynamic[open],
             ImageSize -> Automatic (* pane resizes to current expression *)
             ]
            }}, Alignment -> {Left, Top}]] (* grid alignment *)
        ]]}],
    myFN[n]],  (* interpretation *)
   fmt];

makeFN[n_?NumericQ] := myFN[n];
myFN[n_?NumericQ][x_] := x^n

Usage:

f = makeFN[3]

Mathematica graphics

f[4]
(* 64 *)

With the Opener open:

makeFN[5]

Mathematica graphics

Copying the output from above and evaluating it at 2:

Mathematica graphics

(* 32 *)
Michael E2
  • 235,386
  • 17
  • 334
  • 747
0

The info on Mathgroup by Darren Glosemeyer here wasn't exactly what I remembered but if you change this line:

makeWrapper[data_List, var_Symbol, opts___] := myWrapper[data, var]

to

makeWrapper[data_List, var_Symbol, opts___] := myWrapper[Panel[data], var]

you can see how to include panels in your own functions:

enter image description here

Mike Honeychurch
  • 37,541
  • 3
  • 85
  • 158