11
$\begingroup$

I know it's possible to programmatically change options on the selection in a notebook. I'd like to create a button that lets me change the style of a cell. It's a style that doesn't have a keyboard shortcut, "ItemParagraph"

Something like

SetOptions[NotebookSelection[SelectedNotebook[]], CellStyle->"ItemParagraph"]

but CellStyle is for NotebookFind. Is there some way to do this? I just couldn't find the cell option I needed. I checked the Options for Cell but there are hundreds and I couldn't see anything that looked right...

$\endgroup$
3
  • $\begingroup$ Yes, I have a palette open onscreen so a button on that palette would be much faster than going through the Format --> Style --> Item Paragraph menu. $\endgroup$ Sep 24, 2013 at 15:28
  • $\begingroup$ Maybe useful : mathematica.stackexchange.com/a/9956/363 $\endgroup$ Sep 24, 2013 at 15:28
  • $\begingroup$ I believe that post shows how to set the default for NEW cells... I am wondering how to select a cell and change whatever option would be to "ItemParagraph" $\endgroup$ Sep 24, 2013 at 15:43

4 Answers 4

7
$\begingroup$

Maybe this button may help you:

Button["ItemParagraph", FrontEndExecute@FrontEndToken[SelectedNotebook[], "Style", "ItemParagraph"]]

The whole cell may be selected with:

SelectionMove[SelectedNotebook[], All, Cell]
$\endgroup$
1
  • $\begingroup$ Thank you so much, exactly what I was looking for, does just what I need, and helps me to see ways to use that for other things. $\endgroup$ Sep 25, 2013 at 21:59
12
$\begingroup$

I don't know if any of them can be callled documented but I know three ways to do this:

mentioned above:

FrontEndToken way:

(
 SelectionMove[#, All, Cell]; 
 FrontEndTokenExecute[EvaluationNotebook[], "Style", "Title"]
) & /@ Cells[CellStyle -> "Section"]

or

SelectionSetStyle way:

(
 SelectionMove[#, All, Cell];
 MathLink`CallFrontEnd[FrontEnd`SelectionSetStyle[#, "Title"]]
   ) & /@ Cells[CellStyle -> "Section"]

or (which is implemented with code from above)

CellStyleNames way:

(
 SelectionMove[#, All, Cell];
 Experimental`CellStyleNames[#] = "Title";
) & /@ Cells[CellStyle -> "Section"]
$\endgroup$
3
  • $\begingroup$ Using Scan[] along with your suggestion seems to work faster than SelectionMove[] (at least for me). Not sure why. Try Scan[(Experimental`CellStyleNames[#] = "Title") &, Cells[CellStyle -> "Section"]] $\endgroup$
    – B flat
    Dec 21, 2016 at 12:47
  • 1
    $\begingroup$ This is significantly faster.... Scan[(SetOptions[#, "Title"]) &, Cells[CellStyle -> "Section"]] But it doesn't delete the old style. It appends the new one. Still works though. You can see the double styles if you press Shift+Command+E in any title cell. Do you think there is a way to fix the code to address this? Seems like using SetOptions[] is quite a bit faster, but need to fix the double style created. $\endgroup$
    – B flat
    Dec 21, 2016 at 13:05
  • $\begingroup$ @MichaelMcCain Yep, that was the problem with SetOptions. I don't know how to fix it :/ $\endgroup$
    – Kuba
    Dec 21, 2016 at 13:17
2
$\begingroup$

A function showing a different way to modify the style of a cell:

ChangeStyle[cell_CellObject, style_String] := 
  NotebookWrite[cell, ReplacePart[NotebookRead[cell], 2 -> style]];
$\endgroup$
1
$\begingroup$

This solution below was found in the documentation under Cells->Applications. It appends the new style to the old one, which isn't optimal and creates a mess in most cases. I'll leave it here for reference. Kuba noted that Cells[] allow for more than one style and there may not be a mechanism yet for manipulating these styles easily using SetOptions[] or CurrentValue[]

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

You can also try this brute force approach below which reads the whole notebook. It is fast. But definitely not as straight forward as Kuba's solutions above.

NotebookPut[ NotebookGet[InputNotebook[]] /. Cell[x_, "Section", y__] -> Cell[x, "Title", y], InputNotebook[]];
$\endgroup$
3
  • $\begingroup$ Where in documentation? :), p.s. it adds a style instead of replacing it. $\endgroup$
    – Kuba
    Dec 21, 2016 at 13:55
  • 2
    $\begingroup$ In Cells structure tutorial multiple styles cell syntax is documented so I'd call it a mess in tool we have to deal with this. Since we can have multiple styles one may need to: replace all with one, change the latest, replace a specific one, etc etc. Tools are not there or are obscure. $\endgroup$
    – Kuba
    Dec 21, 2016 at 14:17
  • 1
    $\begingroup$ You are reading whole notebook which sees like an overkill for most cases. May be good though if you need to change a lot at once. $\endgroup$
    – Kuba
    Dec 22, 2016 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.