10
$\begingroup$

Is there a command to obtain all the CellTags and the style of the cell they are in? This question is derived from this question which was answered by jVincent. Based on his answer I want to be able to create a function which takes in a string. Looks for the text "\ ref{String_}" (There should be no space between \ and ref, I'm not sure how to avoid mathjax here, if you know how to please make the proper edits) and uses that information to insert a CounterBox. The problem is that the type of counterboxes I want to insert require two arguments, the first is the style of a cell and the second is the CellTag. So really, this is a two part question:

1) How do you find out all the CellTags available? The obvious answer is by going to Cell > Add/Remove Cell Tags. This only works if you have a cell selected. If you give a cell a cellTag it will appear under "All cellTags in the notebook". So obviously Mathematica keeps a list of all the CellTags available in the notebook. The question is, how do we get this list?

2) Once we found out if this celltag is available, is there a way to obtain the style of the cell in which it is in? For instace, I might create a "DisplayFormulaNumbered" cell, and tag it as "eq1".

So why all of this? The idea is to have some cell with text like this:

From equation ref{eq1} we then ...

Note that I did not include the backlash before ref because I don't know how to avoid mathjax from transforming it at this point. Back to the idea, now using jVincent's code we could create a button that when the cell is selected, it looks for all instances of ref, grabs the input (in this case eq1), looks to see if it is a valid tag, if it isn't then it just issues a warning telling you that there's no valid tag, otherwise it replaces the ref section for the counterbox which as inputs the Cell style in which the tag is contained and the tag.

$\endgroup$

2 Answers 2

12
$\begingroup$

First question, try

nb=InputNotebook[];
NotebookTools`NotebookCellTags[nb]

Second question, a start could be

NotebookFind[nb, "lakj", All, CellTags, AutoScroll -> False]; 
Flatten[{NotebookRead[nb]}][[All, 2]]

EDIT, thanks John Fultz

That last line is inefficient and can fail at times. A better alternative is

"Style" /. Developer`CellInformation[nb]
$\endgroup$
5
  • $\begingroup$ Very nice, how did you find out about this undocumented function? If I can make it replace the ref section then I'll be good. I might not even need to do the second part of the question. Thank you for the function. $\endgroup$
    – jmlopez
    Commented Jun 8, 2012 at 18:53
  • 3
    $\begingroup$ The last line of the second part of your answer isn't a very good way to do this. It fails if NotebookRead[] ends up pulling in elements of a CellGroup, and it also unnecessarily forces a read of the entire Cell expression. Much better would be to do "Style" /. Developer`CellInformation[nb]. $\endgroup$
    – John Fultz
    Commented Jun 10, 2012 at 7:02
  • $\begingroup$ @JohnFultz, Please stop my excitement, but is Developer`CellInformation the function I need to obtain the CellTag and thus finally properly answer this question? $\endgroup$
    – jmlopez
    Commented Jun 11, 2012 at 15:26
  • 1
    $\begingroup$ @jmlopez, I'm not sure I fully grokked your question, so I may miss the mark here. Developer`CellInformation will allow you to grab all of the CellTags in a notebook for top-level cells, but it won't iterate through all of the inline cells (were you hoping it would?). $\endgroup$
    – John Fultz
    Commented Jun 15, 2012 at 22:29
  • $\begingroup$ @JohnFultz, I was hoping that if cell = Cell[BoxData[FormBox[RowBox[{RowBox[{"f", "(", "x", ")"}], " ", "=", " ", "x"}],TraditionalForm]],"NumberedEquation",CellTags -> "eq:myEq"] then by using the function you mentioned I would be able to extract "eq:myEq" out of the cell. But I had no luck :( $\endgroup$
    – jmlopez
    Commented Jun 16, 2012 at 0:59
5
$\begingroup$

If you want detailed information abou tagging, you should definitively know this: the joy of tagging, a Mathematica user conference talk by David Reiss. It will give you the information you ask for and much more...

$\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.