Function Repository Resource:

CellBoundingRectangle

Source Notebook

Get the bounding rectangle coordinates for a cell as it appears in a notebook

Contributed by: Alexey Popkov

ResourceFunction["CellBoundingRectangle"][cellObj]

gives the bounding rectangle coordinates for the contents of the cell cellObj.

ResourceFunction["CellBoundingRectangle"][cellObj,Full]

gives the coordinates of a rectangle that fills out the whole cell cellObj.

ResourceFunction["CellBoundingRectangle"][cellObj,spec,ImageSize]

returns the bounding box information suitable for immediate use as a value for ImageSize.

Details

ResourceFunction["CellBoundingRectangle"] is intended for use within the Notebook interface.
The basic usage syntax is ResourceFunction["CellBoundingRectangle"][cellObj,spec], where spec can be either CellContents or Full.
ResourceFunction["CellBoundingRectangle"][cellObj] is a shorthand for ResourceFunction["CellBoundingRectangle"][cellObj,CellContents].
ResourceFunction["CellBoundingRectangle"][cellObj,spec] returns coordinates in printer's points in the form {{xmin,ymin},{xmax,ymax}}, where y coordinates run from top to bottom relative to the Notebook window.
ResourceFunction["CellBoundingRectangle"][cellObj,spec,ImageSize] returns bounding box size in printer's points in the form {width,height}, taking into account current Magnification setting for cellObj. This value is suitable for immediate use as a value for ImageSize option of functions such as Pane, Graphics, etc.

Examples

Basic Examples (6) 

Get the bounding rectangle coordinates of the contents of the current cell:

In[1]:=
ResourceFunction["CellBoundingRectangle"][EvaluationCell[]]
Out[1]=

Get the bounding rectangle coordinates of a rectangle that fills out the whole internal area of the current cell:

In[2]:=
ResourceFunction["CellBoundingRectangle"][EvaluationCell[], Full]
Out[2]=

Generate an output cell with some text:

In[3]:=
Style["abcd", FontFamily -> "Times New Roman", FontSlant -> Italic]
Out[3]=

Get the bounding box size of the text in printer's points:

In[4]:=
{width, height} = ResourceFunction["CellBoundingRectangle"][
  PreviousCell[CellStyle -> "Output"], CellContents, ImageSize]
Out[4]=

Take some image object and adjust its size manually:

In[5]:=
img = RandomImage[5]
Out[5]=

Get the current bounding box size of the object and specify it as ImageSize:

In[6]:=
imageSize = ResourceFunction["CellBoundingRectangle"][
  PreviousCell[CellStyle -> "Output"], CellContents, ImageSize]
Out[6]=
In[7]:=
Image[img, ImageSize -> imageSize]
Out[7]=

First, produce an output cell:

In[8]:=
expr = TraditionalForm[Expand[(a + b)^20]]
Out[8]=

Get the current absolute wrapping width of the produced cell and specify it as ImageFormattingWidth for Rasterize in order to reproduce the current view:

In[9]:=
wrappingWidth = First@ResourceFunction["CellBoundingRectangle"][
    PreviousCell[CellStyle -> "Output"], Full, ImageSize];
Rasterize[expr, ImageFormattingWidth -> wrappingWidth, ImageSize -> 450]
Out[10]=

Specify the current absolute wrapping width of the output cell as ImageSize for Pane, in order to reproduce the current wrapping independently of the WindowSize:

In[11]:=
expr = Expand[(a + b)^20];
TraditionalForm[expr]
Out[12]=
In[13]:=
wrappingWidth = First@ResourceFunction["CellBoundingRectangle"][
    PreviousCell[CellStyle -> "Output"], Full, ImageSize];
TraditionalForm@Pane[expr, ImageSize -> wrappingWidth]
Out[14]=

Applications (2) 

Export a Wolfram Language expression exactly as it appears on the screen. First, generate some expression as an output cell:

In[15]:=
expr = ResourceFunction["CreateSortableUniqueID"][20]
Out[15]=

Adjust the window width of the current notebook in order to obtain the wrapping you wish. Then obtain the current wrappingWidth of the output cell, and export the expression as Pane with ImageSizewrappingWidth:

In[16]:=
wrappingWidth = First@ResourceFunction["CellBoundingRectangle"][
    PreviousCell[CellStyle -> "Output"], Full, ImageSize];
First@Import[
  Export["output.pdf", Pane[expr, ImageSize -> wrappingWidth]]]
Out[17]=

Properties and Relations (2) 

Define a region using Annotation:

In[18]:=
expr = Annotation[ResourceFunction["CreateSortableUniqueID"][20], "Output", "Region"]
Out[18]=

CellBoundingRectangle returns the bounding rectangle for the cell as it currently appears on the screen:

In[19]:=
ResourceFunction["CellBoundingRectangle"][
 PreviousCell[CellStyle -> "Output"], CellContents, ImageSize]
Out[19]=

Rasterize[expr,"Regions"] also returns the bounding rectangle for the region defined by Annotation, taking fixed values for some Notebook and Cell options, including PageWidth and Magnification:

In[20]:=
Differences[Rasterize[expr, "Regions"][[1, 2]]][[1]]
Out[20]=

By setting these options explicitly, we can partially reproduce the result of Rasterize with CellBoundingRectangle:

In[21]:=
CellPrint[
 ExpressionCell[expr, "Output", PageWidth -> Infinity, Magnification -> 1]]
Out[21]=
In[22]:=
ResourceFunction["CellBoundingRectangle"][
 PreviousCell[CellStyle -> "Output"], CellContents, ImageSize]
Out[22]=

AbsoluteCurrentValue[cellObj,CellSize] also returns the bounding rectangle information for the contents of the cell cellObj, but the width differs from that returned by CellBoundingRectangle[cellObj,CellContents,ImageSize] by a fixed amount with the given value of the Magnification option:

In[23]:=
WithCleanup[
 data = Table[{CurrentValue[EvaluationCell[], Magnification] = m, First[AbsoluteCurrentValue[EvaluationCell[], CellSize] - ResourceFunction["CellBoundingRectangle"][EvaluationCell[], CellContents, ImageSize]]}, {m, .5, 2, .1}],
 ListLinePlot[data, AxesLabel -> {Magnification, "Difference, printer's points"}, Mesh -> All],
 CurrentValue[EvaluationCell[], Magnification] = Inherited]
Out[23]=

Possible Issues (1) 

Cell heights returned by CellBoundingRectangle with different second arguments may differ:

In[24]:=
Last@ResourceFunction["CellBoundingRectangle"][EvaluationCell[], CellContents, ImageSize]
Last@ResourceFunction["CellBoundingRectangle"][EvaluationCell[], Full,
   ImageSize]
Out[24]=
Out[25]=

Publisher

AlexeyPopkov

Version History

  • 2.0.0 – 09 September 2022
  • 1.0.0 – 23 August 2022

Related Resources

Author Notes

CellBoundingRectangle isn't intended to be used within Dynamic.

Version 2.0.0 features a breaking change to the way the rectangle is calculated. Now, the form CellBoundingRectangle[cellObj,Full] returns the coordinates of a rectangle, which fills out the whole cellObj. In the previous version of the function it returned the coordinates of a square, that fills out the whole cellObj. Correspondingly, the form CellBoundingRectangle[cellObj,Full,ImageSize] now returns the image size of that rectangle.

License Information