Define a section(subsection) with custom background colorpage

backgroundsmacrossectioningtcolorbox

Is it possible to redefine certain \section or \subsection to have custom pagecolor?

There are certain solutions like:

How to change the background color only for the current page?,

underline and background color for section or subsection

Change background color for only a section of the page?

and so on.

However, this does not quite solve what I want. Some of these solutions need predefined color boxes at fixed positions etc.

What would be nice to have is, whenever this section (say \newsection) starts, the page color from that point onwards including the section title becomes different until the section ends (which means a new normal \section starts. Basically within the environment of the \newsection ). For that, the whole page does not need to be a different color and the texts in this \newsection might continue over the next page and so does the background color.
The complete page in the range of \newsection can have a different background including possibly the page numbers, left-right margins, etc. or, it will also be sufficient to have the text and equations area in \newsection.

The second example link above is much similar but that is only on the name of the section not on the content of the section.

Best Answer

You can do this with snugshade of frame package. You need to define the desired color just before the use of snugshade. This way yo can change color for all one section or one fraction of this one. You can apply this method to a chapter. Here is un example of code:

\documentclass[10pt,a4paper]{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[italian]{babel}
\usepackage{graphicx,color,framed,lipsum}
\definecolor{shadecolor1}{RGB}{236,236,236}
\begin{document}
    \chapter{Test chapter}
    \begin{oframed}
        \lipsum
    \end{oframed}
\definecolor{shadecolor}{RGB}{236,236,236}
    \begin{snugshade}
        \section{Uno}
        \lipsum[1-3]
    \end{snugshade}
\section{Due}
\lipsum[5-7]
\definecolor{shadecolor}{RGB}{124,124,0}
\begin{snugshade}
    \section{Tre}
    \lipsum[10-15]
\end{snugshade}
\definecolor{shadecolor}{RGB}{255,0,0}
\begin{snugshade}
    \lipsum[7]
\end{snugshade}
\section{Quattro}
\lipsum[20-21]
\end{document}

This is the result: enter image description here