6
$\begingroup$

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?

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$ Commented Jul 9, 2014 at 22:49
  • $\begingroup$ 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. $\endgroup$ Commented Jul 10, 2014 at 1:13

2 Answers 2

4
$\begingroup$

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 *)
$\endgroup$
1
0
$\begingroup$

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

$\endgroup$

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.