2
$\begingroup$

Several functions in WL have a custom elided typesetting form that does not automatically embed large amounts of data into the notebook but presents you with the option to do so if you wish. SparseArray is a good example of this:

SparseArray[RandomInteger[1, 10^8]]

enter image description here

You can make objects like these with BoxForm`ArrangeSummaryBox as explained here, but sometimes the rigid form of ArrangeSummaryBox is insufficient for what you want to do. Is there a way to "hijack" this mechanism?

$\endgroup$

1 Answer 1

1
$\begingroup$

I spent some time reverse-engineering the ArrangeSummaryBox code and came up with the function AddCustomTypesetting. It's easy to use and automatically incorporates the delayed embedding button if the expression is larger than a certain limit (given by $NotebookInlineStorageLimit). Consider this example from the Neat Examples section:

myObj[assoc_?AssociationQ][key_String] := assoc[key];
myObj[assoc_?AssociationQ][fun_] := fun@assoc;

ResourceFunction["AddCustomTypesetting"][
 myObj[_?AssociationQ], Function[{obj},
  RawBoxes @ Cell[
     BoxData @ ToBoxes @ Row[{
        "myObj", "[", "\[LeftAssociation]",
        Panel @ Grid[{
           {"Length:", obj[Length]},
           {"Keys:", Multicolumn[obj[Keys], 4]},
           {"ByteCount:", obj[ByteCount]}
           }, Alignment -> Left],
        "\[RightAssociation]", "]"
        }],
    ShowStringCharacters -> False
    ]
  ]
 ]

myObj[<|"a" -> RandomReal[1, 10], "b" -> 2|>]
myObj[<|"a" -> RandomReal[1, 10^6], "b" -> 2|>]

enter image description here

I tend to tinker a bit with functions like these. The latest version can always be found here on GH. The current version (that I will push to ResourceFunction["AddCustomTypesetting"] soon) will also allow you to remove the typesetting rule again with:

AddCustomTypesetting[myObj[_?AssociationQ], None]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.