1
$\begingroup$

I've gotten in the habit of changing the background color of modified chunks of code using the Format - Background Color menu on the selected code. E.g.

enter image description here

Now I'd like to clear these out of a large notebook to start anew, but selecting all cells and setting the background color back to None using the same menu item doesn't work. Is there another way to remove all these chunks of colored code from a notebook?

Running Cell - Show Expression reveals the internal structure is:

Cell[BoxData[
 RowBox[{
  RowBox[{"test", "=", 
   StyleBox[
    RowBox[{"1", "+", "2", "+", "3"}],
    Background->RGBColor[1, 0.5, 0]]}], ";"}]], "Input",
 CellChangeTimes->{{3.88184852903022*^9, 3.881848539448594*^9}},
 CellLabel->"In[19]:="]

but I don't know how to modify the internal structure of all cells programmatically.

$\endgroup$
4
  • 3
    $\begingroup$ I would suggest something like NotebookGet[...] /. (Background -> ...) :> Sequence[] // NoteboomPut. This should be a lot more robust & performant than doing the modifications via the front-end, and it has the benefit that you get a copy of the original notebook, rather than having to risk destroying something $\endgroup$
    – Lukas Lang
    Jan 4, 2023 at 19:43
  • 2
    $\begingroup$ There is a Remove Formatting somewhere in Edit menu, can't check atm. So select all and click it. $\endgroup$
    – Kuba
    Jan 4, 2023 at 20:22
  • 2
    $\begingroup$ @Kuba Yes, somehow I overlooked Format - Clear Formatting which works! $\endgroup$
    – Chris K
    Jan 4, 2023 at 20:34
  • 1
    $\begingroup$ @Kuba I realize now that Format - Clear Formatting is too broad for my purposes, because it removes all formatting (including the TI style that I use in usage messages). So I will have to stick with something like @LukasLang suggested. $\endgroup$
    – Chris K
    Jan 4, 2023 at 22:34

1 Answer 1

1
$\begingroup$

Based on @LukasLang's comment, the follow seems to work for me:

old = Get["old.nb"];
NotebookPut[old /. StyleBox[stuff___, Background -> RGBColor[_, _, _]] :> stuff];

then save the new notebook.

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