2
$\begingroup$

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.

$\endgroup$
5
  • $\begingroup$ How would you expect to (ideally) generate the numbered equation cells? $\endgroup$ Commented Jun 18, 2021 at 20:30
  • $\begingroup$ @CATrevillian I'm not sure what you mean. The numbered equation cells is the easy part. Just click Format>Style>DisplayFormulaNumbered. This will add a cell with numbering displayed on the right. What I need help with is referencing them in my text in a way that stays up to date when I move the numbered cells, delete them, or add new ones. I hope that makes sense. I would like to use CellID to do this. The CellTag solution above works perfectly but it's not ideal for my purposes. Again.. I'm looking for the solution but with CellID instead of CellTags. $\endgroup$ Commented Jun 18, 2021 at 21:17
  • 1
    $\begingroup$ Thanks for the clarification, Michael. I think I know (programmatically) how to adjust the code to accommodate for this change, but can you please clarify further about how exactly this code is evaluated? Is it placed in a custom stylesheet? I think this will help immensely to improve this question & any potential answers. It’s not clear even in the linked QA that it comes from how my exactly it is implemented. Perhaps this is something rather basic I overlooked, but it’s absence surely is a cause of confusion for more than just myself. Thanks! $\endgroup$ Commented Jun 19, 2021 at 1:50
  • 1
    $\begingroup$ Thanks @CATrevillian! I added an edit 1 above to explain how to evaluate the code and what it does. Yes, Carl's code modifies the stylesheet which can be saved for for the notebook to make it permanent. Does my edit 1 explain things better? Carl's solutions is awesome and works... but using CellTags has some negatives. One negative addressed earlier in that post is that duplicating a cell will also duplicate the CellTag which means you have to write code to generate new ones. It's a pain. I'd like to avoid that by using CellIDs where every cell has a unique identifier gauranteed. $\endgroup$ Commented Jun 19, 2021 at 3:56
  • $\begingroup$ I created a solution here: mathematica.stackexchange.com/questions/249892/… $\endgroup$ Commented Jun 20, 2021 at 2:17

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.