Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Generate boxes corresponding to the printed form of an expression that are compressed where possible
ResourceFunction["ToCompressedBoxes"][expr] generates boxes corresponding to the printed form of expr using compression where appropriate. | |
ResourceFunction["ToCompressedBoxes"][expr,form] gives the boxes corresponding to expr in the specified form. |
Generate a compressed box representation of an image:
In[1]:= | ![]() |
Out[1]= | ![]() |
Display boxes as formatted:
In[2]:= | ![]() |
Out[2]= | ![]() |
Compare to ToBoxes:
In[3]:= | ![]() |
Out[3]= | ![]() |
Convert an expression to its box representation:
In[4]:= | ![]() |
Out[4]= | ![]() |
In[5]:= | ![]() |
Out[5]= | ![]() |
Check the size:
In[6]:= | ![]() |
Out[6]= | ![]() |
Compare to ToBoxes:
In[7]:= | ![]() |
Out[8]= | ![]() |
Check the appearance of both:
In[9]:= | ![]() |
Out[9]= | ![]() |
Use the second argument of ToCompressedBoxes to choose the format type:
In[10]:= | ![]() |
In[11]:= | ![]() |
Out[11]= | ![]() |
In[12]:= | ![]() |
Out[12]= | ![]() |
Using a single argument is equivalent to choosing StandardForm:
In[13]:= | ![]() |
Out[13]= | ![]() |
In[14]:= | ![]() |
Out[14]= | ![]() |
Compare to TraditionalForm:
In[15]:= | ![]() |
Out[15]= | ![]() |
Reduce the box size of large Iconize expressions:
In[16]:= | ![]() |
Out[16]= | ![]() |
In[17]:= | ![]() |
Out[17]= | ![]() |
Check the size:
In[18]:= | ![]() |
Out[18]= | ![]() |
Compare to ToBoxes:
In[19]:= | ![]() |
Out[19]= | ![]() |
Use Unevaluated to yield behavior like MakeBoxes instead of ToBoxes:
In[20]:= | ![]() |
Out[20]= | ![]() |
In[21]:= | ![]() |
Out[21]= | ![]() |
Without Unevaluated, the given expr will evaluate:
In[22]:= | ![]() |
Out[22]= | ![]() |
In[23]:= | ![]() |
Out[23]= | ![]() |
The boxes produced by ToCompressedBoxes can be smaller than ToBoxes even when raster boxes are not involved:
In[24]:= | ![]() |
Out[24]= | ![]() |
In[25]:= | ![]() |
Out[21]= | ![]() |
In[26]:= | ![]() |
Out[23]= | ![]() |
Check the display form:
In[27]:= | ![]() |
Out[27]= | ![]() |
Compress data stored in a DynamicModule:
In[28]:= | ![]() |
Out[28]= | ![]() |
In[29]:= | ![]() |
Out[29]= | ![]() |
In[30]:= | ![]() |
Out[30]= | ![]() |
Define custom formatting for a function using MakeBoxes:
In[31]:= | ![]() |
In[32]:= | ![]() |
Out[32]= | ![]() |
ToCompressedBoxes uses the new formatting automatically:
In[33]:= | ![]() |
Out[33]= | ![]() |
In[34]:= | ![]() |
Out[34]= | ![]() |
ToCompressedBoxes will be equivalent to ToBoxes when there is nothing to compress:
In[35]:= | ![]() |
Out[35]= | ![]() |
In[36]:= | ![]() |
Out[36]= | ![]() |
Boxes produced by ToCompressedBoxes will usually round-trip to other formats without significant changes in size:
In[37]:= | ![]() |
Out[37]= | ![]() |
In[38]:= | ![]() |
In[39]:= | ![]() |
Out[39]= | ![]() |
Convert to a String and back:
In[40]:= | ![]() |
The size is unchanged:
In[41]:= | ![]() |
Out[41]= | ![]() |
ToBoxes will yield NumericArray objects in rasters, which are efficient when packed:
In[42]:= | ![]() |
Out[42]= | ![]() |
In[43]:= | ![]() |
Out[43]= | ![]() |
However, these will not round trip as well as the boxes produced by ToCompressedBoxes since GraphicsBox prevents evaluation:
In[44]:= | ![]() |
Out[44]= | ![]() |
In[45]:= | ![]() |
Out[45]= | ![]() |
The size increased by a factor of 32:
In[46]:= | ![]() |
Out[46]= | ![]() |
Both forms are equivalent when passed to the front end for display:
In[47]:= | ![]() |
Out[47]= | ![]() |
In[48]:= | ![]() |
Out[48]= | ![]() |
In[49]:= | ![]() |
Out[49]= | ![]() |
In[50]:= | ![]() |
Out[50]= | ![]() |
In[51]:= | ![]() |
Out[51]= | ![]() |
ToCompressedBoxes trades time for space:
In[52]:= | ![]() |
In[53]:= | ![]() |
Out[53]= | ![]() |
In[54]:= | ![]() |
Out[54]= | ![]() |
Define formatting for a symbol that uses compressed boxes automatically:
In[55]:= | ![]() |
In[56]:= | ![]() |
Out[56]= | ![]() |
In[57]:= | ![]() |
Out[57]= | ![]() |
This work is licensed under a Creative Commons Attribution 4.0 International License