# help with string patterns and RemoveCellTags

I am building a table of contents using AuthorTools. I used it to create cell tags so the TOC can make hyperlinks, but then I decided I wanted to change them. But I am having trouble removing the old tags.

Here's what I did, after first making a few sample Section and Subsection cells:

<< AuthorTools
nb = Notebooks[][[1]]
Paginate[nb]
MakeContents[nb, "Book", CellTagPrefix -> "c:"]


That worked as desired (and it made tag names like "c:1", "c:2"), but then I decided I wanted to change the prefix to "toc:". To remove the old tags, I tried:

RemoveCellTags[nb, StringExpression["c:", __]]


but it didn't change any cell's tags. The following did work, however, to remove just one tag:

RemoveCellTags[nb, "c:1"]


So I think I am using StringExpression wrong. And yet, this works fine:

StringCases[{"c:3", "c:4"}, StringExpression["c:", __] ]
(* OUTPUT: {{"c:3"}, {"c:4"}}  *)


So, what should the input be to RemoveCellTags to get rid of all tags that start with "c:"?

That package was written some time ago and has not been maintained. You'll need to make the following changes to the Common.m file in the package:

Find this code in the file

RemoveCellTags[nb_NotebookObject?NotebookOpenQ, pat_String] :=
Block[{tags},
tags = NotebookCellTags[nb]//Flatten//Union;
tags = Select[tags, StringMatchQ[#, pat]&];
SelectionMove[nb, All, Notebook];
SelectionRemoveCellTags[nb, tags];
nb
]


and change it to

RemoveCellTags[nb_NotebookObject?NotebookOpenQ, pat:(_String|_StringExpression)] :=
Block[{tags},
tags = NotebookCellTags[nb]//Flatten//Union;
tags = Select[tags, StringMatchQ[#, pat]&];
SelectionMove[nb, All, Notebook];
SelectionRemoveCellTags[nb, tags];
nb
]
`

When I did that it worked fine for me

• And for the record, I found that Common.m file (on the Mac) in: /Applications/Mathematica.app/Contents/AddOns/Applications/AuthorTools
– Seth
Feb 14, 2016 at 4:03