7

I am using the tcolorbox LaTeX package to create some boxes with rounded corners. Everything works well when text goes in those boxes.

But I want to fill some of those boxes with just a picture (usually jpg format). Using \includegraphics inside the box indeed puts the picture in the box, but the rectangular picture does not have the rounded corners that I want. In other words, I want the corners of the picture to become rounded and not spill outside the box with rounded corners.

I would be grateful if someone can let me know how to do this. It would be nice to do so in tcolorbox, but I am also open to other solutions.

Thanks!

1
  • 4
    Welcome! Please provide us with a minimal working example (MWE) showing how you are including an image right now. You can use one of the standard images e.g. example-image-a or whatever from the mwe package to enable people to reproduce your framework easily. (You don't need to load the package - you can just use the images in it straight off.) Generally, though, you'll need to mask or clip the image using something like TikZ or pgfimage or another package with similar functionality. Depending on your configuration, you may be using something like this in tcolorbox already.
    – cfr
    Apr 5, 2016 at 0:56

2 Answers 2

5

Since tcolorbox is implemented using tikz, this is actually simpler.

\documentclass{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\sbox0{\includegraphics{example-image}}%
\path[clip,draw,rounded corners=1.5cm] (0,0) rectangle (\wd0,\ht0);
\path (0.5\wd0,0.5\ht0) node[inner sep=0pt]{\usebox0};
\end{tikzpicture}
\end{document}

demo

3
  • 1
    That's a wonderful solution. Thank you so much! Apr 5, 2016 at 7:50
  • How can i remove the black path that is drawn around the image?
    – flymg
    Jul 7, 2022 at 17:06
  • The black edge is actually part of the image itself. You would have to make the rectangle smaller to remove the edges. Jul 7, 2022 at 17:25
12

watermark graphics is a tcolorbox option which uses graphics file as box background. If no text is desired inside upper or lower parts, height option fixes box size.

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}

\begin{document}
\begin{tcolorbox}[enhanced, watermark graphics=frog, watermark overzoom=1.0, title=example]
\lipsum[1]
\end{tcolorbox}

\begin{tcolorbox}[enhanced, watermark graphics=frog, watermark overzoom=1.0, notitle, height=5cm]
\end{tcolorbox}
\end{document}

enter image description here

Another option could be to use clip upper option, which adjust upper part to interior size and form. In this case \includegraphics command forms the upper part. Following code is copied from page 166 in tcolorbox (v3.90) documentation.

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}

\newcommand{\mygraphics}[2][]{%
\tcbox[enhanced,boxsep=0pt,top=0pt,bottom=0pt,left=0pt,
right=0pt,boxrule=0.4pt,drop fuzzy shadow,clip upper,
colback=black!75!white,toptitle=2pt,bottomtitle=2pt,nobeforeafter,
center title,fonttitle=\small\sffamily,title=\detokenize{#2}]
{\includegraphics[width=\the\dimexpr(\linewidth-4mm)/2\relax]{#2}}}

\begin{document}

\mygraphics{frog}
\mygraphics{lion}

\end{document}

enter image description here

update tcolorbox more recent versions include command tcbincludegraphics which combines an includegraphics command inside a tcolorbox. Therefore previous examples can be simplified. More information can be found in section 11 Inclusion of Boxed Image Files. Here you have some examples:

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}

\tcbset{colframe=red!70!black, 
    size=tight, boxrule=1mm, arc=2mm, auto outer arc,
    nobeforeafter}
\begin{document}
\tcbincludegraphics{frog}

\tcbincludegraphics[width=.5\linewidth, graphics options={width=3cm}]{frog}
%
\tcbincludegraphics[width=.5\linewidth, hbox, graphics options={width=3cm}]{frog}
\end{document}

enter image description here

You must log in to answer this question.

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