Fixing problems with your preferred solution
Edit: Solution 3 Refactored DOES NOT WORK AS EXPECTED
It only works in limited cases where I find Cells
by their CellStyle
and change CellStyle
but not if I mix-n-match different properties for finding and changing. I don't know why that is. If you have insight please contribute.
Stricly speaking, the usage of All
as the trird argument of Replace
(as well as usage of ReplaceAll
) is dangerous because it means that the replacement will be performed on all levels of the original Cell
expression (returned by NotebookRead
), while you wish to modify only the Rest
of the latter. Hence the Solution 3 should be rewritten as follows in order to make it safe:
NotebookWrite[#,
Block[{cellExpr = NotebookRead[#]},
Join[cellExpr[[1 ;; 1]],
Replace[Rest[cellExpr], "Input" -> "Code", {1}]]]] & /@
Cells[CellStyle -> "Input"]
The reason why your replaceCellOps
doesn't allow to "mix-n-match different properties for finding and changing" is that you designed it in the way which normally doesn't allow to do this. Here is a safe version which allows arbitrary replacements in the list of cell options:
Clear[replaceCellOptions]
replaceCellOptions[rules_, cells_] :=
Block[{cellExpr = NotebookRead[#]},
If[Head[cellExpr] === Cell,
NotebookWrite[#,
Join[cellExpr[[1 ;; 1]],
Replace[Rest[cellExpr], rules, {1}]]]]] & /@ cells;
Usage example (it will change styles of all "Input"
cells to "Code"
and prepend "Code "
to their cell labels):
replaceCellOptions[{"Input" -> "Code",
HoldPattern[CellLabel -> label_] :> CellLabel -> "Code " <> label},
Cells[CellStyle -> "Input"]]
Better solution: Stylesheet-based approach
An alternative solution which is both much more efficient and safe is to manupulate the private Notebook's stylesheet in order to change the style of cells, as shown here.
Replace "Input"
style with "Code"
style:
SetOptions[
EvaluationNotebook[],
StyleDefinitions -> Notebook[
{
Cell[StyleData[StyleDefinitions -> "Default.nb"]],
Cell[StyleData["Input", StyleDefinitions -> StyleData["Code"]]]
}
]
]
Now input cells are formatted with "Code"
style, while in their definitions they still have style "Input"
:

You can change arbitrary Cell
options via private stylesheet. For example, make "Input"
cells not just formatted with "Code"
style, but also Italic
:
SetOptions[EvaluationNotebook[],
StyleDefinitions ->
Notebook[{Cell[StyleData[StyleDefinitions -> "Default.nb"]],
Cell[StyleData["Input", StyleDefinitions -> StyleData["Code"]],
FontSlant -> Italic]}]]
If you wish to replace one of the "Code"
style definitions for "Input"
cells, you should place the "intercept" before the line Cell[StyleData["Input", StyleDefinitions -> StyleData["Code"]]
. For example, making the font weight to be Plain
(instead of Bold
by default):
SetOptions[EvaluationNotebook[], StyleDefinitions -> Notebook[{
Cell[StyleData[StyleDefinitions -> "Default.nb"]],
Cell[StyleData["Input"], FontWeight -> Plain],
Cell[StyleData["Input", StyleDefinitions -> StyleData["Code"]]],
}]]
Reset everything to the defaults:
SetOptions[EvaluationNotebook[], StyleDefinitions -> "Default.nb"]
P.S. To force updating the styles of the notebook you may need to evaluate
FrontEndTokenExecute["ToggleShowExpression"]
or
NotebookClose[CreateNotebook[Visible -> False]]
as described here.
SetOptions
to change the options Evaluatable and Background instead? $\endgroup$CellStyle
or other properties. so it's more of a learning exercise for me. $\endgroup$Alt
key (might be other keys on OS other than Windows) and left click the bracket of a certain cell to select all cells with the same style in the same notebook. $\endgroup$