How to set actual width for a rounded corners image using TikZ?

This question is closely related to LaTeX Photo With Rounded Corners. Suppose I want to put a rounded corners image in a marginnote, so the image fits the \linewidth of the margin. If I use the technique proposed in the answers to that question, what fits the \linewidth is not the image, but the image plus the white frame to produce the rounded corners.

A MWE with three images. The first one, just normal. The second one with the method of @PeterGrill. The third one is what I want to get, I've used \hspace, \vspace and a little calculation to fit the image. But I don't know if this solution could be problematic as I'm expanding the TikZ picture outside the marginpar. I'd like a TikZ-only solution. Would it be better, doesn't it?

\documentclass[twoside,a4paper]{tufte-handout}

\usepackage{blindtext}
\usepackage{tikz}
\usepackage{graphicx}
\usepackage{calc}
\newcommand*{\ClipSep}{0.4cm}%

\begin{document}
\marginnote{\includegraphics[width=\linewidth]{frog.jpg}
%\raggedright \blindtext
}

\marginnote{
\begin{center}
\begin{tikzpicture}
\node [inner sep=0pt] at (0,0) {\includegraphics[width=\linewidth]{frog.jpg}};
\draw [white, rounded corners=\ClipSep, line width=\ClipSep]
(current bounding box.north west) --
(current bounding box.north east) --
(current bounding box.south east) --
(current bounding box.south west) -- cycle
;
\end{tikzpicture}
\end{center}

\raggedright \blindtext}

\marginnote{
\begin{center}
\hspace*{-\ClipSep}\begin{tikzpicture}
\node [inner sep=0pt] at (0,0) {\includegraphics[width=\linewidth + \ClipSep]{frog.jpg}};
\draw [white, rounded corners=\ClipSep, line width=\ClipSep]
(current bounding box.north west) --
(current bounding box.north east) --
(current bounding box.south east) --
(current bounding box.south west) -- cycle
;
\end{tikzpicture}
\end{center}
\vspace*{-\ClipSep}
\raggedright \blindtext}

\Blindtext

\end{document}


I apologize to the OP for providing not a TikZ-only solution, but there are already three good TikZ-only answers and a future reader of this question may have use of my answer relying upon the tcolorbox package (which is actually based on TikZ). With the current version 3.30 (2014/11/17), a really short answer can be given using the \tcbincludegraphics macro. It wraps the well-known \includegraphics into a tcolorbox; so, rounded corners can be used.

The full solution text is:

\documentclass[twoside,a4paper]{tufte-handout}

\usepackage{blindtext}
\usepackage{tikz}
\usepackage[skins]{tcolorbox}
\newcommand*{\ClipSep}{0.4cm}%

\begin{document}

\marginnote{\tcbincludegraphics[blank,arc=\ClipSep]{frog.jpg}
\blindtext}

\Blindtext

\end{document}


• Wah! nice feature which I missed. – user11232 Nov 22 '14 at 11:51
• @HarishKumar The \tcbincludegraphics shortcut is very new :-) – Thomas F. Sturm Nov 22 '14 at 11:55
• Please don't apologize. It's a very good answer. Thanks for the contribution. – Pablo B. Nov 23 '14 at 8:03
• This worked better for me than the TikZ solution (from elsewhere) that I tried. – Joe Corneli Feb 20 '18 at 17:23

How about a path picture? Then the clipping is done for you. However you have to get the height of the image first which I did by putting it into a temporary box. It is probably more efficient to set the minimum width and minimum height keys to \wd\tmpbox and \ht\tmpbox+\dp\tmpbox respectively, but I just used \phantom.

\documentclass[twoside,a4paper]{tufte-handout}
\usepackage{blindtext}
\usepackage{tikz}
\newcommand*{\ClipSep}{0.4cm}%
\newbox\tmpbox
\begin{document}
\marginnote{%
\setbox\tmpbox=\hbox{\includegraphics[width=\linewidth]{example-image}}%
\tikz\node [draw=white,
inner sep=0pt, outer sep=0pt, rounded corners=\ClipSep,
path picture={\node at (path picture bounding box.center) {\box\tmpbox};}]
{\phantom{\copy\tmpbox}};
\raggedright \blindtext}

\Blindtext

\end{document}


Only part of the output is shown:

Here is a solution (with double inclusion of picture):

\documentclass[twoside,a4paper]{tufte-handout}
\usepackage{blindtext}
\usepackage{tikz}
\usepackage{graphicx}
\usepackage{calc}
\newcommand*{\ClipSep}{0.4cm}%
\begin{document}
\marginnote{%
\begin{tikzpicture}
\node [inner sep=0pt,draw=white](a) {\includegraphics[width=\linewidth]{example-image}};
\filldraw[white,line width=0pt](a.south west) rectangle (a.north east);
\clip[rounded corners=\ClipSep] (a.south west) rectangle (a.north east);
\node [inner sep=0pt](a) {\includegraphics[width=\linewidth]{example-image}};
\end{tikzpicture}
\raggedright \blindtext}

\Blindtext

\end{document}


Different frog ;-) from internet. The proposed solution utilizes the current bounding box information to draw 4 arcs at 4 corners and fill them with white color. The radius of the corners is defined by \r that can be adjustable.

Code

\documentclass[twoside,a4paper]{tufte-handout}

\usepackage{blindtext}
\usepackage{tikz}
\usepackage{graphicx}
\usepackage{calc}
\newcommand*{\ClipSep}{0.4cm}%

\begin{document}
\marginnote{\includegraphics[width=\linewidth]{frog.jpg}
%\raggedright \blindtext
}

\marginnote{
\begin{center}
\begin{tikzpicture}
\node [inner sep=0pt] at (0,0) {\includegraphics[width=\linewidth]{frog.jpg}};
\draw [white, rounded corners=\ClipSep, line width=\ClipSep]
(current bounding box.north west) --
(current bounding box.north east) --
(current bounding box.south east) --
(current bounding box.south west) -- cycle
;
\end{tikzpicture}
\end{center}

\raggedright \blindtext}

\marginnote{
\begin{center}
%\hspace*{-\ClipSep}
\begin{tikzpicture}
\node [inner sep=0pt] at (0,0) {\includegraphics[width=\linewidth]{frog.jpg}};
%\draw [white, rounded corners=\ClipSep, line width=\ClipSep]
%    (current bounding box.north west) --
%    (current bounding box.north east) --
%    (current bounding box.south east) --
%    (current bounding box.south west) -- cycle
%    ;
\def\r{0.5}
\path[fill=white] (current bounding box.north west) -- ++(\r,0)  arc(90:180:\r) -- cycle;
\path[fill=white] (current bounding box.north east) -- ++(-\r,0) arc (90:0:\r)  -- cycle;
\path[fill=white] (current bounding box.south west) -- ++(\r,0)  arc(-90:-180:\r)--cycle;
\path[fill=white] (current bounding box.south east) -- ++(-\r,0) arc(-90:0:\r)  -- cycle;
\draw[white,line width=0pt](current bounding box.north west) rectangle (current bounding box.south east);
\end{tikzpicture}
\end{center}
%\vspace*{-\ClipSep}
\raggedright \blindtext}

\Blindtext

\end{document}

• Great solution with TikZ. Thanks. It's exactly what I was asking for, although there are other valid solutions out there. – Pablo B. Nov 23 '14 at 8:05
• You are welcome. Glad it helps. Indeed, all are good ones. – Jesse Nov 23 '14 at 8:08

Here is another answer that uses a box and tikz. To make this look different, I have used a macro \myclippedpic.

\documentclass[twoside,a4paper]{tufte-handout}
\usepackage{blindtext}
\usepackage{tikz}
\newcommand*{\ClipSep}{0.4cm}%
\newbox\tmpbox
\newcommand{\myclippedpic}[1]{%
\setbox\tmpbox=\hbox{\includegraphics[width=\linewidth]{#1}}%
\begin{tikzpicture}
\clip[rounded corners=\ClipSep] (0,0) rectangle (\wd\tmpbox,\ht\tmpbox);
\node [inner sep=0pt] at (current bounding box.center) {\includegraphics[width=\linewidth]{#1}};
\end{tikzpicture}
}
\begin{document}
\marginnote{%
\myclippedpic{example-image}
\raggedright \blindtext}

\Blindtext

\end{document}