[Tex/LaTex] Clip background image inside tcolorbox

backgroundstcolorbox

I am creating a table with special dates and a background image, like this one.

I want to automatize the process, and create tables with different heights, but using the same background image. My solution is to have a long image inside a tcolorbox, where the upper section of the image is just an homogeneous color. Then, I want tex to crop it automatically, according to the size of the table. My chosen background image is here (as you can see, I simple extended the original, shorter image).

My code is the following:

\documentclass{report}
\usepackage[table,dvipsnames]{xcolor}
\usepackage[most]{tcolorbox}
\usepackage{booktabs}

\tcbset{
    bgtable/.style={
        freelance,
        frame code={\draw[ultra thick] (interior.north west) -- (interior.north east);}
        center title,
        fonttitle=\bfseries\sffamily,
        watermark graphics=#1,
        watermark stretch=1.00,
        %watermark zoom=1.0,
        clip lower,
        arc=0pt,
        outer arc=0pt,
        nobeforeafter}
}

\begin{document}

\begin{tcolorbox}[bgtable=Winter_term_card.png]
    \begin{tabular*}{11cm}{c c}
        \multicolumn{2}{c}{\large\textbf{Second Term}} \\
        \textbf{Week 1} & \\
        \midrule{1-2}
        06/01 & \textbf{Wed:} Event 1 blah blah. \\
        \textbf{Week 2} & \\
        \midrule{1-2}
        16/01 & \textbf{Fri:} Event 2 blah blah. \\
        \textbf{Week 3} & \\
        \midrule
        21/01 & \textbf{Mon:} Event 3 blah blah. \\  
        \bottomrule
    \end{tabular*}
    \vspace{2cm}    
\end{tcolorbox}

\end{document}

Yet, the image is not being clipped. Instead, the image shrinks to occupy the whole of the tcolorbox space. The result is this:

enter image description here

Any idea how to proceed? I just can't get it to work.

Best Answer

As OP mentions, tcolorbox section Clip Environments provides a solution. Following code is just an adaptation of picturebox environment in page 166.

picturebox has a mandatory parameter which is the background image. This image is scaled to cover tcolorbox width but keeping the aspect ratio. As you can see provided image shows a long upper part almost uniform

enter image description here

Therefore I preferred to define the background of the tcolorbox showing the image from bottom to top, this way is easy to observe how taller boxes include larger part of picture but without scaling it.

The solution uses a tcbclipenvironment with a node which includegraphics instead of the watermark option proposed in original code.

\documentclass[a4paper]{report}
\usepackage[table,dvipsnames]{xcolor}
\usepackage[most]{tcolorbox}
\usepackage{booktabs}
\usepackage{lipsum}
\usepackage[vmargin={1cm,1cm}]{geometry}

\makeatletter
\newtcolorbox{picturebox}[2][]{%
    enhanced,
    frame hidden,
    interior hidden,
    fonttitle=\bfseries,
    sharp corners,
    underlay={%
        \begin{tcbclipinterior}
            \node[anchor=south]
             at (interior.south) {%
                \includegraphics[%
                width=\tcb@width, 
                keepaspectratio]{#2}};
        \end{tcbclipinterior},
        },
    overlay={\draw[ultra thick] (interior.north west)--(interior.north east);},
    #1
}
\makeatother

\begin{document}

\begin{picturebox}{Background.jpg}
\lipsum[1]
\end{picturebox}

\begin{picturebox}{Background.jpg}
\lipsum[1-2]
\end{picturebox}

\begin{picturebox}{Background.jpg}
\lipsum[1-3]
\end{picturebox}

\begin{picturebox}{Background.jpg}
\lipsum[1-8]
\end{picturebox}

\end{document}

enter image description here