1
$\begingroup$

For a given notebook, I can turn on CellIDs by opening the notebook's stylesheet and adding the following code:

  Cell[StyleData["Notebook"],
  CreateCellID->True]

When any new cell is created, a new unique identifier (CellID) is generated and permanently attached to it. I am working on a problem that requires this but also requires I use CellTags. Here's my question:

For any "DisplayFormulaNumbered" style cell in my notebook, I would like its CellTag to be assigned the cell's CellID. In other words, if the cell has a CellID of 123456789, then I would like the CellTag for that same cell to be "123456789" and I would like this to happen the moment the cell is created.

I am hoping for a stylesheet solution. The idea is something like this...

 Cell[StyleData["DisplayFormula"],
 CellTags->CellID]

although I know this doesn't work. Do you have any ideas?

$\endgroup$
3
  • $\begingroup$ I have been trying to do just this! And while I can easily get this to work (as in, assigning a CellID as a CellTag) i on a new cell, it does not seem to be so simple when applying it to a stylesheet modification (to me, at least). You’ll need to do something like CellTags->ToString[CurrentValue[EvaluationCell[],CellID]], I think. $\endgroup$ Jun 20, 2021 at 0:09
  • $\begingroup$ haha! Interesting! I'll try messing with that now. $\endgroup$
    – B flat
    Jun 20, 2021 at 0:14
  • 1
    $\begingroup$ After working on this all day I got a stylesheet solution. It just requires the user to click any one of the (n)'s which the user would do anyways when referencing a cell. Clicking just one of them syncs all of them. I'll post my solution above... just execute the code to create the stylesheet, the stylesheet can then be used for any notebook. I'm open to improvements on my code or alternate solutions! $\endgroup$
    – B flat
    Jun 20, 2021 at 1:58

1 Answer 1

1
$\begingroup$

This stylesheet solution syncs CellTags to CellIDs every time (n) is clicked. To try this out...Execute the following code in a notebook to change alter its stylesheet.

Module[{event, label, cellid},
 
 event = "MouseClicked" :> {
    
    (*this scans and syncs ALL CellTags in the notebooks to their CellIDs everytime an (n) is mouse clicked*)
    
    Scan[(cellid = "CellID" /. Developer`CellInformation[#];
       SetOptions[#, CellTags -> ToString@cellid]) &, 
     Cells[InputNotebook[], CellStyle -> "DisplayFormulaNumbered"]];
    
    (*this copies state of counter at CellTag.*)
    
    CopyToClipboard@
     Cell@TextData[{CounterBox["DisplayFormulaNumbered", 
         Replace[CurrentValue[ParentCell@ParentCell@EvaluationCell[], 
           CellTags], l_List :> First@l]]}]};
 
 (*this labels DisplayFormulaNumbered cell as (n) using and creates a mouseclick event handler to initiate the copy when clicking any (n)*)
 
 label = Cell[
   TextData[{"(", 
     Cell@BoxData@
       TagBox[CounterBox["DisplayFormulaNumbered"], 
        EventHandlerTag[{event}]], ")"}], 
   "DisplayFormulaEquationNumber"];
 
 (*this embeds it all in stylesheet*)
 
 SetOptions[EvaluationNotebook[], 
  StyleDefinitions -> 
   Notebook[{Cell[StyleData[StyleDefinitions -> "Default.nb"]],
     Cell[StyleData["Notebook"], CreateCellID -> True],
     Cell[StyleData["DisplayFormulaNumbered"],
      CellFrameLabels -> {{None, label}, {None, None}}]},
    StyleDefinitions -> "PrivateStylesheetFormatting.nb"]]]

Now that the stylesheet is changed, create a few "DisplayFormulaNumbered" cells and a text cell. In the text cell, make some references to the "DisplayFormulaNumbered" cells by clicking one of the (n)'s to copy and then paste it in the text cell. Notice, the reference will update when "DisplayFormulaNumbered" cells are added or deleted so that the reference stays intact. This works with as many references as you need.

If a "DisplayFormulaNumbered" cell is created by copy-pasting another "DisplayFormulaNumbered" cell, just click any (n) to resync all of them. I don't see this as a problem as one would click the new (n) anyways to create the reference which the point of having numbered cells in the first place.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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