# Clip background image inside tcolorbox

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:

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

• How should the clipping process know which part of the image shall be clipped? The top? The bottom? Left or right? – user31729 Jan 6 '17 at 17:11
• There is a clip lower inside \tcbset. I tried with upper too, without success. – luchonacho Jan 6 '17 at 17:16
• I know, but that's not my question ;-) You have to specify which portions of the image shall be cut out. – user31729 Jan 6 '17 at 18:13
• I thought I was, according to the tcolorbox manual, "Clip Environments" section (9.4). If not, how to crop them? – luchonacho Jan 6 '17 at 18:27

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

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}


At some point, it is easier just to use plain TikZ. Note that the default inner sep=.333em and the line thickness for ultra thick is 2pt (half inside, half outside). I added the opacity just to tone down the image.

\documentclass{report}
\usepackage[table,dvipsnames]{xcolor}
\usepackage[most]{tcolorbox}
\usepackage{booktabs}
\usepackage{environ}
\usepackage{showframe}% debugging tool, MWE only

\usetikzlibrary{backgrounds}

\NewEnviron{mycolorbox}[1]% #1 = image filename (no special characters)
{\par\noindent\tikz{%
\node[draw,ultra thick,text width={\dimexpr\textwidth-.666em-2pt}]{\BODY};
\begin{scope}[on background layer]
\clip (current bounding box.south west) rectangle (current bounding box.north east);
\node[inner sep=0pt,opacity=0.5] {\includegraphics{#1}};
\end{scope}
}\par}

\begin{document}

\begin{mycolorbox}{example-image}
\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*}
\end{mycolorbox}

\end{document}