How to set the background of a section to light gray and leave background of content below and above the section as default?
Try to group the entire section and set the backround color to gray from the "Writing assistant pallete".
You can also use create your own stylesheet by going to "Format->Edit Stylesheet->Choose a style->Section". Then you change the bckround color to gray using the "Writing assistant pallete". Every time you create a new section, the backround color will be automatically gray
If you wish to do this programmatically instead of using the mouse in the front end, you can place the following code after the section you wish to change the color of, then it should select the section and change the background:
SelectionMove[SelectedNotebook, Previous, CellGroup]; obj = SelectionMove[SelectedNotebook, Previous, CellGroup] SetOptions[NotebookSelection, All, Background -> LightGray]
This could be developed into a button if necessary to be re-used, as it may be a bit clunky to past this in after every section you wish to change the color of.
Note that you can change
Previous in the
SelectionMove commands to
Next if you wish to place this code before the
Section to be colored. In addition, this construction can be used to change a variety of other cell styling options.