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}
  • \tcbline does not create 'functional' sub boxes, as \tcblower would do – user31729 Mar 10 '16 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. – Ulrike Fischer Mar 10 '16 at 19:59
  • Yes, I had that in mind, but I am no expert in TikZ stuff. Thomas Sturm should know, I assume ;-) – user31729 Mar 10 '16 at 20:01
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

  • Could it also be raster even row/.style={empty,colback=red!10}? The question is: what's tile does in even rows? – Ignasi Mar 11 '16 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. – Thomas F. Sturm Mar 11 '16 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. – Ulrike Fischer Mar 11 '16 at 17:03

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.