12

I want a nice colored frame around an image which has rounded corners. Here is my attempt, using mdframed, but, as you can see, the inner edge of the frame is a right-angle, and not rounded. I would like the inner edge of the frame to overlay (chop off) the corners of the image to produce a rounded inner edge. Is there a nice easy way to do this?

\documentclass{article} 
\usepackage[framemethod=TikZ]{mdframed} 
\usepackage{graphicx}

\begin{document} 

\parindent=0pt
\baselineskip=0pt
\parskip=0pt


\def\myimage{c:/images/donald.png}

\begin{mdframed}[roundcorner=5pt, leftmargin=2cm,innertopmargin=0pt,innerbottommargin=0pt, innerleftmargin=0pt,innerrightmargin=0pt, innerlinewidth=0pt, middlelinewidth=0pt,outerlinewidth=10pt, outerlinecolor=red]% 
\hsize=5cm\includegraphics[width=5cm, height=5cm]{\myimage}
\end{mdframed}% 

\end{document} 

Result

5
  • 1
    If, instead of \includegraphics[width=5cm, height=5cm]{\myimage} you put there something like TEXT, you'll see the round inner corners. This shows that angles are produced by your image: it overlaps the frame. Just increase the inner margins to see your beautiful frame.
    – Boris
    Nov 9, 2011 at 18:45
  • The problem caused of the output-routine. First the frame will drawn and then the contents. Nov 9, 2011 at 18:49
  • This is correct, but I'd argue this is a feature rather than a bug. I would not want my frames to overlap my contents. Maybe this calls for a variant of mdframed called, say, matte, which is drawn over the contents.
    – Boris
    Nov 9, 2011 at 18:51
  • @Boris: thanks but increasing the inner margins reveals an inner border which I do not want (the graphic will not necessarily have a white background). How can I force the frame to overwrite the image?
    – Dan
    Nov 9, 2011 at 19:18
  • 1
    I updated my answer Nov 9, 2011 at 20:02

3 Answers 3

7

Run with xelatex

\documentclass{article} 
\usepackage{pstricks,graphicx}
\newsavebox\IBox

\begin{document}    
\leavevmode
\savebox\IBox{\includegraphics{/tmp/donald}}
\put(0,0){\usebox\IBox}%
\psframe[framearc=0.2,framesep=0pt, cornersize=relative,
  linecolor=blue,linewidth=3mm](\wd\IBox,\ht\IBox)
\end{document}

enter image description here

and the same with clipping theimage:

\savebox\IBox{\includegraphics{/tmp/donald.png}}
\begin{pspicture}(\wd\IBox,\ht\IBox)
\psclip{\psframe[framearc=0.7,linestyle=none](\wd\IBox,\ht\IBox)}
\rput[lb](0,0){\usebox\IBox}%
\endpsclip
\psframe[framearc=0.7,framesep=0pt, cornersize=relative,
  linecolor=blue,linewidth=4mm](\wd\IBox,\ht\IBox)
\end{pspicture}

enter image description here

13
  • thanks, but if in your solution you change to "framesep=0pt" then you get the same problem. I do not want another border inside the outer frame.
    – Dan
    Nov 9, 2011 at 19:47
  • 1
    @Dan: that is also no problem.
    – user2478
    Nov 9, 2011 at 19:59
  • this looks good...and I can almost understand it! :-)
    – Dan
    Nov 9, 2011 at 22:42
  • as the radius increases (say framearc=0.7), then the image edges protrude outside the borders: i.stack.imgur.com/FSaRV.png. Is there a way of clipping it?
    – Dan
    Nov 10, 2011 at 11:03
  • 1
    @Dan: Please don't edit answers with follow-up questions or comments. You can add links to uploaded images in the comments as well. Simply upload it using a post which you then discard and copy the link. Nov 10, 2011 at 11:36
19

As Martin mentioned in the comment and I think mdframed is to break a butterfly on a wheel, I want to present a tikz-Solution:

\documentclass{article} 
\usepackage{tikz}
\usepackage{xparse}
\newsavebox\MyPicture
%\roundedpicture[graphcis-setup][tikz-setup]{graphics path}
\NewDocumentCommand{\roundedpicture}%
      {O{width=0.6\linewidth}
       O{draw=blue,line width=6pt,rounded corners=5pt}
       m}{%
   \savebox\MyPicture{\includegraphics[#1]{#3}}%
   \begin{tikzpicture}%
    \draw [path picture={%
                   \node at (path picture bounding box.center) {%
                       \usebox\MyPicture};},#2]
          (0,0)  rectangle (\wd\MyPicture,\ht\MyPicture);
   \end{tikzpicture}%
}
\begin{document}
\roundedpicture[width=5cm]{donald-duck}

\roundedpicture{donald-duck}
\end{document}


ORIGINAL POST uses mdframed

You can change the behavior of the background. By default it is drawn as a filled rectangle.

With the following code you put a dummy in the environment to get the picture in the foreground.

\documentclass{report}
\usepackage{lipsum}
\usepackage[framemethod=TikZ]{mdframed}
\mdfdefinestyle{short}{roundcorner=5pt, leftmargin=2cm,innertopmargin=0pt,innerbottommargin=0pt, innerleftmargin=0pt,innerrightmargin=0pt, innerlinewidth=0pt, middlelinewidth=0pt,outerlinewidth=10pt, outerlinecolor=red}


\usetikzlibrary{backgrounds}
\makeatletter
\newrobustcmd\addtikzsetting[1]{%
   \appto\md@tikz@settings{#1}%
}

\addtikzsetting{%
   \tikzset{mdfbackground/.style={path picture={
                     \node at (path picture bounding box.center) {%
                       \includegraphics[height=5cm,width=5cm]{donald-duck}
             };}}}}
\makeatother
\begin{document}
\begin{mdframed}[style=short,]
\hsize=5cm\rule{0pt}{5cm}
\end{mdframed}
\end{document}

I think mdframed isn't the correct environment. You should create you own environment via tikz.

Now the modification with a new command so that it can be centered or whatever you want.

\documentclass{report}
\usepackage{graphicx}
\usepackage{lipsum}
\usepackage[framemethod=TikZ]{mdframed}
\mdfdefinestyle{short}{roundcorner=10pt,innertopmargin=0pt,innerbottommargin=0pt, innerleftmargin=0pt,innerrightmargin=0pt, innerlinewidth=0pt, middlelinewidth=0pt,middlelinewidth=10pt, middlelinecolor=red,leftmargin=0pt,rightmargin=0pt}


\usepackage{xparse}
\makeatletter
\newsavebox\MyPicture
%\roundedpicture[mdframed-setup][graphic-setup]{graphics-name}
\NewDocumentCommand{\roundedpicture}{o o m}{%
  \savebox\MyPicture{\includegraphics[#2]{#3}}%
  \begin{minipage}{\dimexpr\wd\MyPicture+20pt\relax}
  \appto\md@tikz@settings{%
     \tikzset{mdfbackground/.style={path picture={%
         \node at (path picture bounding box.center) {\includegraphics[#2]{#3}};}%
          }%
      }%
   }%
  \begin{mdframed}[style=short,#1,nobreak]
    \hsize=\dimexpr\wd\MyPicture\relax%
     \rule{0pt}{\dimexpr\ht\MyPicture+\dp\MyPicture\relax}
      \rule{\dimexpr\wd\MyPicture\relax}{0pt}
   \end{mdframed}%
   \end{minipage}
}
\makeatother

\begin{document}
 \begin{center}
\roundedpicture[][width=3cm,height=5cm,keepaspectratio]{donald-duck}
\end{center}
\end{document}

11
  • This works beautifully (I think)...now I have to study it to find out how it works! :-)
    – Dan
    Nov 9, 2011 at 20:34
  • @Dan: Can you mark this question as answered. Nov 9, 2011 at 20:39
  • is it possible to mark 2 answers? It looks like Herbert's answer is also correct, as well as yours.
    – Dan
    Nov 9, 2011 at 22:41
  • @Dan: No. You must decide ;-). Nov 10, 2011 at 7:58
  • Why not use TikZ directly in this case? It's much simpler IMHO. Nov 10, 2011 at 11:37
1

An alternative solution with tcbincludegraphics command from tcolorbox. The first example show default settings (and image's original sharp corners) , the second fixes box (with frame) width and third shows how to define graphics options and keep adjusted border. As can be seen image corners are rounded in second and third examples.

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

\tcbset{
    myframe/.style={
        colframe=red,
        boxrule=1mm,
        boxsep=0pt,
        left=0pt,
        right=0pt,
        top=0pt,
        bottom=0pt
        }
}

\begin{document}

\tcbincludegraphics[width=5cm]{frog}

\tcbincludegraphics[myframe, width=5cm]{frog}

\tcbincludegraphics[myframe, hbox, graphics options={width=7cm, viewport=120 300 646 500, clip}]{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 .