8

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}

Result of the MWE

Pablo B.
  • 537

5 Answers5

11

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}

enter image description here

Jesse
  • 29,686
7

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:

enter image description here

Mark Wibrow
  • 70,437
6

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}

enter image description here

Paul Gaborit
  • 70,770
  • 10
  • 176
  • 283
6

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.

enter image description here

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}
Jesse
  • 29,686
3

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} 

enter image description here