1
$\begingroup$

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?

$\endgroup$
5
  • 1
    $\begingroup$ There are dozens of of questions relating to setting the background colors of cells. Check them by searching on cell background color. $\endgroup$
    – bbgodfrey
    May 29, 2021 at 23:44
  • $\begingroup$ 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. $\endgroup$
    – bbgodfrey
    May 29, 2021 at 23:45
  • $\begingroup$ @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! $\endgroup$
    – AMA
    May 30, 2021 at 3:48
  • 1
    $\begingroup$ @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. $\endgroup$
    – AMA
    May 30, 2021 at 4:00
  • 1
    $\begingroup$ 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. $\endgroup$
    – bbgodfrey
    May 30, 2021 at 14:12

2 Answers 2

2
$\begingroup$

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

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – AMA
    May 31, 2021 at 16:14
  • 1
    $\begingroup$ @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. $\endgroup$
    – bbgodfrey
    May 31, 2021 at 17:31
0
$\begingroup$

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]

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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