7
$\begingroup$

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:

enter image description here

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

enter image description here

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

enter image description here

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
  • Adding/removing them easily
  • Auto-numbering

Related:

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

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}] &

styled CellFrameLabels with auto-numbering

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.