# Styles that prepend text to cell contents

This question is about creating a style for things like figure/table captions, or perhaps special boxes labeled with titles like "Note:", "Proof:", etc. The idea is that when I create a new cell in such a style, or change the syle of an existing cell, the label (in a specific font and style) would be prepended to whatever is in that cell. Of course, the most obvious application would be captions, that one might want to automaticall start with something like "Figure n:", where n would be an automatically incremented counter associated with the style in question.

I have seen the answer to a similar question here, and it very much sounds like what I want is not possible, in general, unless I use work-arounds like cell dingbats or cellframe labels, each of which come with their own limitations, see the question I linked to.

So, yes, this question is closely related to this one, but it's a little broader (I think), and I'm really asking for a direct answer: Can the above be done with a Stylesheet entry, or is this not possible? I would have thought that this is a fairly common request, but I really did not see much on this question anywhere on the web at all.

• I recall a duplicate question but can't find it now. I don't think it is possible to achieve that with Stylesheets only. We need something like style cell template but there isn't any similar feature. Applying such template can be done with CellDynamicExpression or something similar but the problem is how to drop that "template" when the style is removed. Could you list exact requirements so that we can try to find a workaround with dingbats or frame labels?
– Kuba
Aug 25 '16 at 17:49
• Here is a concrete scenario: Let's say I want a FigureCaption style such that the Figure caption starts with "Figure n:" in bold (same font and size as the caption itself), with n the Figure counter. Ideally I'd like the entire caption ("Figure n:" label plus caption text) to be centered, but having things left-justified, perhaps at some indentation relative to standard text cells would be o.k., too. The caption text ("This figure shows blah blah...") should immediately follow the figure label on the same line.
– Pirx
Aug 25 '16 at 20:56
• P.S.: I'm not worried about having to remove stuff manually if I change the cell style from my FigureCaption back to something else. That is a scenario that I would consider rare. So if that is a major concern, I'd say ignore it...
– Pirx
Aug 25 '16 at 20:58

I'm not worried about having to remove stuff manually if I change the cell style from my FigureCaption back to something else. That is a scenario that I would consider rare. So if that is a major concern, I'd say ignore it..

So this should work for you, add those (or analogous) styles to the stylesheet:

Cell[StyleData["Section"],
CellDynamicExpression :> (
SelectionMove[EvaluationCell[], Before, CellContents];
NotebookWrite[
EvaluationNotebook[],
TemplateBox[{CounterBox["Section"]},"TemplatedSection"],
After
];
SetOptions[EvaluationCell[], CellDynamicExpression -> None]
),


That part inserts what you want and disables this feature for given cell (because I don't trust CellDynamicExpression 126655).

It would be nice to not have to recreate all cells when the "header" is changed so the code above inserts only a TemplateBox for TemplateSelection which we can define later in the stylesheet and modify separately:

Cell[
StyleData["TemplatedSection"],
TemplateBoxOptions -> {
DisplayFunction :> (
TemplateBox[
{#1,". section note",": "},"RowDefault",Editable->False
]&
)
},

I've noticed that sometime when the TemplateBox is edited cells are not updated properly/counter number is broken, but as soon as once creates one section and deletes it, everything is fixed. So that is only a minor inconvenience.