Function Repository Resource:

AddCustomTypesetting

Source Notebook

Define an output format for objects that can be copy-pasted as the original expression

Contributed by: Sjoerd Smit

ResourceFunction["AddCustomTypesetting"][symbol, fun]

creates a MakeBoxes rule that will typeset any expression of the form symbol[] as fun[symbol[]], but still copies as the original expression.

ResourceFunction["AddCustomTypesetting"][patt, fun]

creates a MakeBoxes rule for any expression matching pattern patt.

ResourceFunction["AddCustomTypesetting"][spec, fun, limit]

defines a custom ByteCount limit for embedding the full expression in the notebook.

ResourceFunction["AddCustomTypesetting"][spec, None]

removes the custom MakeBoxes rule for a symbol or pattern.

Details

The default size limit for embedding expressions is given by $NotebookInlineStorageLimit.
If an expression is too big, the output will show a button with options for how to store the output.
The format function fun takes form as a second argument, where form is passed down from MakeBoxes[expr, form].

Examples

Basic Examples (4) 

Make NonCommutativeMultiply appear as a CenterDot multiplication:

In[1]:=
ResourceFunction[
 "AddCustomTypesetting", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][NonCommutativeMultiply, CenterDot @@ # &]
In[2]:=
y ** x
Out[3]=

Copy-paste the output and check that it still represents the original expression:

In[4]:=
FullForm[
NonCommutativeMultiply[y, x]]
Out[4]=

The noncommutativity holds:

In[5]:=
NonCommutativeMultiply[y, x] == NonCommutativeMultiply[x, y]
Out[14]=

Remove the custom typesetting rule:

In[15]:=
ResourceFunction[
 "AddCustomTypesetting", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][NonCommutativeMultiply, None]
In[16]:=
y ** x
Out[16]=

Scope (3) 

Define a custom data object that formats as a summary panel:

In[17]:=
myObj[assoc_?AssociationQ][key_String] := assoc[key];
myObj[assoc_?AssociationQ][fun_] := fun@assoc;
ResourceFunction[
 "AddCustomTypesetting", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][myObj[_?AssociationQ], Function[{obj},
  Panel @ Grid[{
     {"Length:", obj[Length]},
     {"Keys:", Multicolumn[obj[Keys], 4]},
     {"ByteCount:", obj[ByteCount]}
     }, Alignment -> Left]
  ]
 ]
In[18]:=
myObj[<|"a" -> 1, "b" -> 2|>]
Out[18]=

The panel still works as an input expression:

In[19]:=
myObj[<|"a" -> 1, "b" -> 2|>][Values]
Out[19]=

If the underlying expression is very large, it will not be automatically embedded into the notebook:

In[20]:=
myObj[<|"a" -> RandomReal[1, 10^7], "b" -> 2|>]
Out[20]=

Set a very low ByteCount limit for embedding the expression:

In[21]:=
ResourceFunction[
 "AddCustomTypesetting", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][NonCommutativeMultiply, Shallow[CenterDot @@ #] &, 2000]
In[22]:=
NonCommutativeMultiply @@ Range[10]
Out[22]=
In[23]:=
NonCommutativeMultiply @@ Range[100]
Out[23]=

Typeset differently depending on the format:

In[24]:=
ResourceFunction[
 "AddCustomTypesetting", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][NonCommutativeMultiply,
 Function[{expr, fmt},
  If[fmt === TraditionalForm,
   CenterDot @@ expr,
   Framed[expr]
   ]
  ]
 ]
In[25]:=
x ** y
Out[25]=
In[26]:=
x ** y // TraditionalForm
Out[26]=

Neat Examples (1) 

Make a custom object that still looks like the original expression:

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

ResourceFunction[
 "AddCustomTypesetting", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][
 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
    ]
  ]
 ]
In[28]:=
myObj[<|"a" -> 1, "b" -> 2|>]
Out[28]=

Publisher

Sjoerd Smit

Version History

  • 1.1.0 – 15 May 2023
  • 1.0.0 – 10 May 2023

Related Resources

License Information