4
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – Kuba Aug 25 '16 at 17:49
  • $\begingroup$ 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. $\endgroup$ – Pirx Aug 25 '16 at 20:56
  • $\begingroup$ 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... $\endgroup$ – Pirx Aug 25 '16 at 20:58
3
$\begingroup$

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]     
 ),
 MenuSortingValue->10000]

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
         ]&
      )
   },
   MenuSortingValue->10000 
]

enter image description here

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.

$\endgroup$
  • $\begingroup$ This is great. I did not think this was possible. I've been playing around with your code. It seems a little glitchy like you said, but I think I should be able to apply the style sheet again or do what you said programmatically to refresh the counters so they are always up to date. $\endgroup$ – Michael McCain Oct 18 '16 at 22:36
  • $\begingroup$ Pirx and I seem to be working on similar things. $\endgroup$ – Michael McCain Oct 18 '16 at 22:39

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.