1

I want to set different background colors for various cell styles (e.g. Section, Text), so that whenever I choose a specific style for some cell in the notebook, it automatically gets colored accordingly. Is this possible to do via evaluating a single simple command inside the notebook?

I've tried, for example, Cell[StyleData["Text"], Background -> Gray], but it doesn't work at all. Is there a way to use SetOptions for this?

bbgodfrey
  • 61,439
  • 17
  • 89
  • 156
AMA
  • 21
  • 4
  • 2
    There are dozens of of questions relating to setting the background colors of cells. Check them by searching on cell background color. – bbgodfrey May 29 '21 at 23:44
  • Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory [tour] now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. – bbgodfrey May 29 '21 at 23:45
  • @bbgodfrey Yes, I believe I've read through all the relevant ones, but I still couldn't make it work as described in the question... And thank you for the intro! – AMA May 30 '21 at 03:48
  • 1
    @bbgodfrey Say, I can write SetOptions[#, Background -> Blue] & /@ Cells[CellStyle -> "Section"], but it needs to be evaluated every time the new Section cell is created. – AMA May 30 '21 at 04:00
  • 1
    You may need to create a private stylesheet to accomplish this, because it appears that defined styles from the Default stylesheet, when being entered, override your command, SetOptions[#, Background -> Gray] & /@ Cells[EvaluationNotebook[], CellStyle -> "Text"]. Only after they are entered into your Notebook does your command apply when executed later. Warning: I am no expert on this topic. – bbgodfrey May 30 '21 at 14:12

2 Answers2

2

Undoubtedly, there are commands to specify background colors for Default styles, but I found creating Private Style Definitions to be easy and convenient. With an open notebook, open the Format menu and choose Edit Stylesheet ... to create

enter image description here

Then choose styles from the dropdown menu at the left and format them from the Format menu, in this case by choosing Background colors. For instance,

enter image description here

To save the result, choose Install Stylesheet at the right and follow the instructions. Then, close the notebook. In the future, when opening a notebook, open the Format menu, choose Stylesheet, and select the private stylesheet created earlier. Styles will have the backgrounds as defined, for instance.

enter image description here

bbgodfrey
  • 61,439
  • 17
  • 89
  • 156
  • Thank you. It's probably the most convenient way after all. Do you know if the created stylesheets get saved as files? I wanted to delete the ones I've created but couldn't find them by name anywhere. – AMA May 31 '21 at 16:14
  • 1
    @AMA Following the advice here, I executed $UserBaseDirectory, went to that directory, and then to subdirectory /SystemFiles/FrontEnd/StyleSheets/, which contains the private stylesheet, which can be deleted. Note that the name of the private stylesheet remains in the Format > Stylesheet menu, and I do not know how to remove it. – bbgodfrey May 31 '21 at 17:31
0

you can use this command which makes the whole notebook light gray. But I don't know how to change the background of a single cell. SetOptions[EvaluationNotebook[], Background -> LightGray]

PooryWan
  • 16
  • 1
  • 5