Suppose I have a notebook with lots of "DisplayFormulaNumbered" cells. These cells have a built-in counter, which continuously updates when cells are deleted, moved or added, which is nice. Within my notebook, I also have many places in "text" cells that reference these "DisplayFormulaNumbered" cells. For example,
We see from the equation (formula reference number) that...
The problem is that I like to move around (and sometimes add or delete) "DisplayFormulaNumbered" cells which means that I need the formula reference number to update as well so that it references the same formula that may be using a new reference number.
I'm looking for a stylesheet solution. Carl Woll gave a good stylesheet solution here using CellTags, however, the CellTags have to be generated first which is an extra step I'd like to avoid. I have other reasons as well.
Instead of CellTags, I'd like to use CellID to uniquely identify my cells. It's nice because every cell is guaranteed to have a unique CellID and if a new cell is created, a new CellID is generated for it. This is optimal over CellTags as I don't have to programmatically generate them. So far, I've been unsuccessful in adapting Carl Woll's CellTag solution below to a CellID solution. I like his "copy and paste" functionality so I'd like to keep that. I just want to make this work using CellID. Any ideas?
Module[{event, label}, event = "MouseClicked" :> CopyToClipboard @ Cell @ TextData[{ CounterBox[ "DisplayFormulaNumbered", Replace[CurrentValue[ParentCell @ ParentCell @ EvaluationCell[], CellTags], l_List :> First@l] ] }]; label = Cell[ TextData[{ "(", Cell @ BoxData @ TagBox[ CounterBox["DisplayFormulaNumbered"], EventHandlerTag[{event}] ], ")" }], "DisplayFormulaEquationNumber" ]; SetOptions[ EvaluationNotebook[], StyleDefinitions -> Notebook[ { Cell[StyleData[StyleDefinitions->"Default.nb"]], Cell[StyleData["DisplayFormulaNumbered"], CellFrameLabels -> {{None, label}, {None, None}} ] }, StyleDefinitions->"PrivateStylesheetFormatting.nb" ] ] ] Edit 1: To see the code above in action, open a new notebook and execute the following code to create some "DisplayFormulaNumbered" cells with some randomly created CellTags.
SeedRandom[1]; CellPrint /@ Table[Cell[w, "DisplayFormulaNumbered", CellTags -> w], {w, RandomWord[5]}]; Then execute Carl's code (above) in the same notebook. This will permanently alter the stylesheet for the notebook. Then start a text cell and click one of the numbers on the far right in one of the DisplayFormulaNumbered cells... say "(3)". Then open a text cell and paste. The 3 will appear. Now delete the first "DisplayFromulaNumbered" cell. You'll see the 3 change to a 2 in the text cell which is what it's supposed to do. Great functionality.
I'm trying to do the same exact thing but using CellIDs. As every cell already has a CellID, I won't have to create any new ones. This is optimal as opposed to using CellTags.