# How to add a caption below a cell?

How can I change my stylesheet definition for a cell to optionally add a caption below?

For example, I would like to add a caption to my "Program" cells shown here:

like this (but with the option of right or left as well as centered alignment):

There are some caption-like things to be found in the docs:

But I would prefer to have the caption as part of the cell expression (not simply a separately styled cell put right below it)

I've tried CellFrameLabels, but I stumbled on a few things:

• Controlling alignments and styling
• Auto-numbering

Related:

• Maybe try CellFrameLabels. – Silvia Aug 31 '15 at 16:39
• @Silvia Thanks, see my update. – M.R. Aug 31 '15 at 17:29

The purpose can be achieved with CellFrameLabels. Cell-expressions and Boxes can be used directly here, so styling can be done with StyleBox, alignment can be done with PanelBox etc. The counter mechanics is exact as Simon demonstrated in the linked Q/A in OP.

Here is an example:

cellFrameLabelFunc uses PanelBox with Alignment to align label lbl from left (-1) to right (1):

Clear[cellFrameLabelFunc]
cellFrameLabelFunc[lbl_, align_: 0, color_: White] :=
PanelBox[lbl,
ImageSize -> {Scaled[1], Automatic},
Alignment -> {align, Center},
Appearance -> "Frameless", Background -> color
]


We pre-assign the align_ then construct the label as following:

alignFraction = 0.3;

With[{
counterSet =
ToString[Hue[#, .4, .95], StandardForm] & /@ Range[0, 1, .2]
},

testlbl =
Style["my cell frame label", 13, Red, Italic,
FontFamily -> "Constantia"] //
ToBoxes //
RowBox[{#, " & my counter : ",
CounterBox["myCounter",
CounterFunction :> (Part[counterSet, #] &)] //
StyleBox[#,
FontFamily -> "Constantia", FontSize -> 30,
FontWeight -> Bold,
FontColor -> Hue[0.75, 0.38, 0.78]] &
}] & //
cellFrameLabelFunc[#, alignFraction] &
];


And generate the cells:

Cell["", CounterAssignments -> {{"myCounter", 0}}] // CellPrint

Cell[
ConstantArray["this is the main body ", 6] // StringJoin,
"myStyle",
CellFrameLabels -> {{None, None}, {testlbl, None}},
CounterIncrements -> "myCounter"
] // Do[CellPrint@#, {5}] &


• Great answer as always! – M.R. Sep 1 '15 at 13:34
• @M.R. Thanks for acceptance! – Silvia Sep 1 '15 at 15:22
• What do you think is the easiest way to apply this to a single cell (that already exists in my notebook)? – M.R. Sep 1 '15 at 15:46
• @M.R. I would use SetOptions[CellObject, CellFrameLabels->...]. – Silvia Sep 2 '15 at 6:32