[Tex/LaTex] tcolorbox: change background of a part of the body

tcolorbox

I have a tcolorbox with various "part" separated by \tcbline and would like to change the background of two of the (e.g. the second and the forth. Has someone a good idea how to do it?

\documentclass{article}
\usepackage[many]{tcolorbox}%
\usepackage{lipsum}

\begin{document}
\begin{tcolorbox}
\lipsum[1]
\tcbline
\lipsum[2]
\tcbline
\lipsum[1]
\tcbline
\lipsum[1]
\end{tcolorbox}
\end{document}

Best Answer

I would do such stuff by using a tcbraster or tcbitemize inside an outer tcolorbox. The following code example creates colored stripes in blue and red, but every stripe could be given an individual color:

\documentclass{article}
\usepackage[many]{tcolorbox}%
\usepackage{lipsum,geometry}

\begin{document}

  \begin{tcolorbox}[enhanced,
      boxsep=0pt,top=0pt,bottom=0pt,leftupper=0pt,rightupper=0pt,
      toptitle=1mm,bottomtitle=1mm,lefttitle=5mm,righttitle=5mm,
      colbacktitle=yellow!20,coltitle=black,title=Box with interior raster,
      colback=blue!10,clip upper]
    \begin{tcbitemize}[raster columns=1,raster equal skip=0pt,
        sharp corners,boxrule=0pt,
        raster odd row/.style={empty},
        raster even row/.style={tile,colback=red!10}]
      \tcbitem
      \lipsum[1]
      \tcbitem
      \lipsum[2]
      \tcbitem
      \lipsum[1]
      \tcbitem
      \lipsum[1]
    \end{tcbitemize}
  \end{tcolorbox}


  \clearpage
  The same with a new environment:

  \newenvironment{mybox}{%
    \begin{tcolorbox}[enhanced,
      boxsep=0pt,top=0pt,bottom=0pt,leftupper=0pt,rightupper=0pt,
      toptitle=1mm,bottomtitle=1mm,lefttitle=5mm,righttitle=5mm,
      colbacktitle=yellow!20,coltitle=black,title=Box with interior raster,
      colback=blue!10,clip upper]%
    \begin{tcbitemize}[raster columns=1,raster equal skip=0pt,
        sharp corners,boxrule=0pt,
        raster odd row/.style={empty},
        raster even row/.style={tile,colback=red!10}]%
  }{\end{tcbitemize}\end{tcolorbox}}

  \begin{mybox}
    \tcbitem
    \lipsum[1]
    \tcbitem
    \lipsum[2]
    \tcbitem
    \lipsum[1]
    \tcbitem
    \lipsum[1]
  \end{mybox}

\end{document}

enter image description here

Related Question