Function Repository Resource:

AddCustomTypesetting (1.0.0) current version: 1.1.0 »

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 patttern patt.

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

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

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 (3) 

Make NonCommutativeMultiply appear as a CenterDot multiplication:

In[1]:=
ResourceFunction["AddCustomTypesetting"][NonCommutativeMultiply, CenterDot @@ # &]
In[2]:=
y ** x
Out[2]=

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

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

The noncommutativity holds:

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

Scope (3) 

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

In[5]:=
myObj[assoc_?AssociationQ][key_String] := assoc[key];
myObj[assoc_?AssociationQ][fun_] := fun@assoc;
ResourceFunction["AddCustomTypesetting"][myObj[_?AssociationQ], Function[{obj},
  Panel @ Grid[{
     {"Length:", obj[Length]},
     {"Keys:", Multicolumn[obj[Keys], 4]},
     {"ByteCount:", obj[ByteCount]}
     }, Alignment -> Left]
  ]
 ]
In[6]:=
myObj[<|"a" -> 1, "b" -> 2|>]
Out[6]=

The panel still works as an input expression:

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

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

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

Set a very low ByteCount limit for embedding the expression:

In[9]:=
ResourceFunction["AddCustomTypesetting"][NonCommutativeMultiply, Shallow[CenterDot @@ #] &, 2000]
In[10]:=
NonCommutativeMultiply @@ Range[10]
Out[10]=
In[11]:=
NonCommutativeMultiply @@ Range[100]
Out[11]=

Typeset differently depending on the format:

In[12]:=
ResourceFunction["AddCustomTypesetting"][NonCommutativeMultiply,
 Function[{expr, fmt},
  If[fmt === TraditionalForm,
   CenterDot @@ expr,
   Framed[expr]
   ]
  ]
 ]
In[13]:=
x ** y
Out[13]=
In[14]:=
x ** y // TraditionalForm
Out[14]=

Neat Examples (1) 

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

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

ResourceFunction["AddCustomTypesetting"][
 myObj[_?AssociationQ], Function[{obj},
  DisplayForm@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[16]:=
myObj[<|"a" -> 1, "b" -> 2|>]
Out[16]=

Publisher

Sjoerd Smit

Version History

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

Related Resources

License Information