# Why can't I change the style of a cell repeatedly?

I ran into this when I was working with an example from the Help Pages of V9. Cells is a new function added in V9. The following appears to work the first time it is evaluated in a notebook with nb assigned to some appropriate value, say, EvaluationNotebook[].

Scan[(CurrentValue[#, StyleNames] = "Title") &, Cells[nb, CellStyle -> "Section"]]


However, if I try again to change the cells affected by the first evaluation to another style or back to the original style, nothing changes. Neither this

Scan[(CurrentValue[#, StyleNames] = "Text") &, Cells[nb, CellStyle -> "Title"]]


nor this

Scan[(CurrentValue[#, StyleNames] = "Section") &, Cells[nb, CellStyle -> "Title"]]


has any effect.

For other CurrenValue targets such as FontSize, it's easy to change the value repeatedly.

 Scan[(CurrentValue[#, FontSize] = 100) &,  Cells[nb, CellStyle -> "Section"]]
Scan[(CurrentValue[#, FontSize] = 30) &,  Cells[nb, CellStyle -> "Section"]]

-
closely related: Set the style of a cell – Kuba Apr 1 at 9:19

It appears that it only works once because it messes up the cell expressions it modifies the first time it is evaluated.

I started with a notebook with several kinds of cells but only one text cell, which had the cell expresion

Cell["Some text", "Text"]


I evaluated

nb = EvaluationNotebook[];
Scan[(CurrentValue[#, StyleNames] = "Section") &, Cells[nb, CellStyle -> "Text"]]


and everything looked good -- the text cell was reformatted to a section cell. However, the cell expression was now

Cell["Some text", "Text", "Section"]


It should have been

Cell["Some text", "Section"]


So I think we are looking at a bug.

-
Yeah, anyway, one not so comfortable way is to reset the nb each time re-change the Style of Cell. – HyperGroups May 8 '13 at 4:38
quaternions.blog.163.com/blog/static/20608214720134802320281 here, I re-format all inline cells by Replace Rules Cell[]->Cell[,Background->Red] about CellExpression. is there any other simple way? Such like the option, DefaultInlineFormatType which I found seems only take effect for new Created Inline Cells – HyperGroups May 8 '13 at 4:45
@HyperGroups. Your comment seems to be a new question. Please ask it properly by posting it as a question, so it can be given proper attention and so people can answer it using the full facilities of Mathematica.SE – m_goldberg May 8 '13 at 21:13
I made a bugreport on this. WRI answered that its not a bug: ("In this case, Mathematica is behaving as designed. In particular, the StyleNames item does not apply to the cells you are looking at. Further, StyleNames is an undocumented symbol, meaning that it's usage can change without warning. In this particular case, however, if you simply ask for the current value of StyleNames for a given cell, you get back an empty list. Please see the attached notebook for details.") – sacratus Mar 30 at 21:30
@sacratus. I think it would be good if you were to post your comment as an answer. I will certainly up-vote if you do. I would be interested if would give details of contents of the "attached notebook" referenced in tech support reply. – m_goldberg Mar 31 at 3:54

As suggested i post my commend as an answer:

I made a bugreport on this (March 27) and got an answer from WRI 3 days later that says:

In this case, Mathematica is behaving as designed. In particular, the StyleNames item does not apply to the cells you are looking at. Further, StyleNames is an undocumented symbol, meaning that it's usage can change without warning. In this particular case, however, if you simply ask for the current value of StyleNames for a given cell, you get back an empty list. Please see the attached notebook for details.

The content of the attached notebook is as followes:

StyleNames is undocumented

Here is the command you used to create a notebook, simplified a little bit.

nb = CreateDocument[{TextCell["abc", "Section"]}]


The following extracts a cell object.

 cell = Cells[nb, CellStyle -> "Section"][[1]]


CurrentValue of cell returns nothing for StyleNames

 CurrentValue[cell, StyleNames]


StyleNames is undocumented functionality.

 ?StyleNames


Out:

 SystemStyleNames
Attributes[StyleNames] = {Protected}


For me this answer was not very satisfying. In summary they just told me that StyleNames is undocumented and that it cannot be used to change Styles.

From my interpretation it's a value for extra Styles.

  CurrentValue[cell, StyleNames] = "MyStyle"


is not meant to change the current Style rather than add "MyStyle" to the existing styles of a cell. (my interpretation)

What i've learned from this, is that you cannot trust in the functionality of an undocumented Symbol like StyleNames, because it's functionality is not clear and can change without warning! I can also recommend to contact WRI for possible bugs, they where very fast and polite and gave me many thanks for my email. Use: support AT wolfram DOT com for sending bugreport to WRI.

EDIT:

I a further Email i wrote WRI that the example in the documentation of Cells(Applications) is very missleading, because it makes you think, that StyleNames is for changing the cell Style. The friendly WRI-guy told me that he made a report on this part of the documentation.

WORK AROUNDS

See this answer: by kuba for 3 different ways to change to CellStyle!

-
Ok, thanks for reporting. Now take a look at SelectionMove page, there is Scope section and an example with SetOptions[NotebookSelection[nb], "Section"]. This code does exactly what we observed with StyleNames, moreover it is documented. What do you think? Have they told you how to reliably change cell style? Could you continue report with observations about selection move, if you agree with those. – Kuba Apr 2 at 8:25
@Kuba I dont have this part of Scope, maybe because i am using Mathematica 9 and No he told me that he cannot offer me a workaround at this moment. Well i think with this information, we can say that mathematica is not working "as designed". Here again it just adds a secondary Style to the Cell, not really changing it. But i think the internal report on the docs, that now has been made after my emails, will make its way to the right person inside of WRI. – sacratus Apr 2 at 8:38
Ahh sorry, it is in NotebookSelection page. – Kuba Apr 2 at 8:39
@Kuba ah ok, well i think the problem is, that the CellStyle isnt really an Option. If it would be an option one could use SetOptions[NotebookSelection[nb], CellStyle -> "Section"] and we would not have this problem. But with your workarounds we have all we need ;) – sacratus Apr 2 at 8:49
But they do not use Rule there: SetOptions[NotebookSelection[nb], "Section"]` suggesting this is a way to go. – Kuba Apr 2 at 8:52