8

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}
3
  • \tcbline does not create 'functional' sub boxes, as \tcblower would do
    – user31729
    Commented Mar 10, 2016 at 19:54
  • @ChristianHupfer I know, but it should be possible to set nodes and draw the background with an overlay or something like this. Commented Mar 10, 2016 at 19:59
  • Yes, I had that in mind, but I am no expert in TikZ stuff. Thomas Sturm should know, I assume ;-)
    – user31729
    Commented Mar 10, 2016 at 20:01

1 Answer 1

10

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

5
  • Could it also be raster even row/.style={empty,colback=red!10}? The question is: what's tile does in even rows?
    – Ignasi
    Commented Mar 11, 2016 at 7:44
  • @Ignasi empty would remove all frames and backgrounds and the color would not show up. tile removes the frame completely to avoid previewer glitches with a 0pt border. Here, alternatively to tile, you could use frame hidden or frame engine=empty. Commented Mar 11, 2016 at 7:48
  • This worked quite fine. It took me a bit time to figure out how to draw the lines (one has to exclude the first raster). Also I had an "equal height group" and so the background of the main box was seen at the bottom of some boxes but then it looked good. Thanks. Commented Mar 11, 2016 at 17:03
  • Is this environment breakable? (even if I add the breakable option, it does not break!)
    – JPMD
    Commented Apr 3, 2023 at 15:11
  • Breakable works for me. Of course, only the outer box is breakable (mybox), the inner color blocks are not. So, you get breaks from one inner color block to another, but not inside a color block. Commented Apr 27, 2023 at 9:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .