4

.Hello tex.se community,
I have a problem when I put a\frame around an overpic environment. The top border of the frame is overlapped by the picture.

Is it a bug or a wrong use of\frame command ? I can't find anithing on the Internet or tex.se.

I just want to put a frame around the image.

Here is the mwe :

\documentclass{article}
\usepackage{graphicx}
\usepackage{overpic}

\begin{document}
\begin{figure}
    \centering
    \frame{\includegraphics[width=0.7\textwidth]{foo}}
    \caption{framed figure include with \emph{includegraphics}.}
\end{figure}
\begin{figure}
    \centering
    \frame{\begin{overpic}[width=0.7\textwidth]{foo}
        \put(50,20){foo}
    \end{overpic}}
    \caption{framed figure include with \emph{overpic}.}
\end{figure}
\end{document}

Edit 1

The issue doesn't seems to come from the viewer and thin border rendering. See this screenshot of the pdf rendering with xpdf and evince. In this case, the image is a grey square an cross the right border of the frame. screenshot

EDIT by cfr

The following image can be used to reproduce the issue:

img.png

Adapted MWE:

\documentclass{article}
\usepackage{graphicx}
\usepackage{overpic}

\begin{document}
  \begin{figure}
    \centering
    \frame{\includegraphics[width=0.7\textwidth]{img}}
    \caption{framed figure include with \emph{includegraphics}.}
  \end{figure}
  \begin{figure}
    \centering
    \frame{\begin{overpic}[width=0.7\textwidth]{img}
        \put(50,10){foo}
      \end{overpic}}
    \caption{framed figure include with \emph{overpic}.}
  \end{figure}
\end{document}

Effect:

too small frame

cfr
  • 198,882
fwachtel
  • 81
  • 1
  • 8
  • do you need the frame to be so tight? \fbox is the normal command and includes some padding. – David Carlisle Mar 08 '15 at 21:06
  • Welcome to TeX SE! What exactly is the problem? The two cases look pretty much the same when I compile - except for the additional text in the second case. (I replaced your image by another, of course, as I don't have foo.) – cfr Mar 08 '15 at 21:07
  • @DavidCarlisle I want the frame to stick to the image borders, it work with includegraphics but not with overpic. the same issue arise with fbox. – fwachtel Mar 08 '15 at 21:12
  • @cfr the second image with overpic mask the top border of the frame. – fwachtel Mar 08 '15 at 21:13
  • My guess is that the image box generated by overpic is too small in height. – fwachtel Mar 08 '15 at 21:15
  • @cfr yes even when max zoom in. If you dont have issue maybe it my install. It happen under either pdflatex or xelatex. – fwachtel Mar 08 '15 at 21:17
  • the example image is already framed so it's difficult to see but it seems to work. But even with a basic png image made with Gimp from scratch I get this error and also with vector graphics in pdf. the overlapping seems to happen on the smaller dimension, right or top. – fwachtel Mar 08 '15 at 21:39
  • @fwachtel I've edited your question to make it easier for people to reproduce the problem. If I've misrepresented the issue or you otherwise object, feel free to roll-back my edit. – cfr Mar 08 '15 at 22:51
  • @cfr sleep + internet connexion down, I answer from my phone so cannot try your example img but I think it's ok. So you can reproduce the issue ? Have you tried to see the PDF I've sent to David in comment to be sure it's not a viewer artefact ? – fwachtel Mar 09 '15 at 08:21
  • @fwachtel It is not an artefact: the example I posted shows that clearly. – cfr Mar 09 '15 at 12:58
  • @cfr I found a workaround, see my answer. Can you try it to confirm ? Anyway, thank you for your help. – fwachtel Mar 09 '15 at 14:28
  • @fwachtel Yes, I confirm that works for me. At least, it works in minimal testing (+1). I'd actually tried abs and percent but didn't remember permil and so didn't think to try it. – cfr Mar 09 '15 at 22:38

2 Answers2

3

Partial Solution

[using overpic package v0.53 2010/09/13]

Possible explanation

The overpic package makes operations on the dimensions of the image to fit with the local basis used to position overlapping object. It seems that there is some approximations in these operations that lead to a wrong dimension of the image in the local basis. As the image is placed relatively to the bottom-left corner, it goes over the picture box on right or top side.

Solution

It appear that the approximation of the image dimensions is different when the package is loaded with option percent, permil or abs.
In my case, the permil option seems to not introduce any error, with a correct framed image.

comparison

Bug fix ?

I looked the overpic.sty file but I'm not expert in TeX at all.
If somebody can confirm my thought, we could fix this.
It's only a hundred of line file.

fwachtel
  • 81
  • 1
  • 8
2

It's an artefact of your viewer, but you can avoid the problem.

I see

enter image description here

and as you see, both lose some edges, but zooming in shows

enter image description here

with all edges showing.

However you can make the pdf less delicate by drawing the frame after the image so it is on top:

Page 2 shows as follows at all zoom levels:

enter image description here

\documentclass{article}
\usepackage{graphicx}
\usepackage{overpic}

\begin{document}
\begin{figure}
    \centering
    \frame{\includegraphics[width=0.5\textwidth]{house}}
    \caption{framed figure include with \emph{includegraphics}.}
\end{figure}
\begin{figure}
    \centering
    \frame{\begin{overpic}[width=0.5\textwidth]{house}
        \put(50,20){foo}
    \end{overpic}}
    \caption{framed figure include with \emph{overpic}.}
\end{figure}

\clearpage


\begin{figure}
    \centering
    \sbox0{\includegraphics[width=0.5\textwidth]{house}}%
    \usebox{0}%
    \kern-\wd0\frame{\phantom{\usebox0}}
    \caption{framed figure include with \emph{includegraphics}.}
\end{figure}
\begin{figure}
    \centering
    \sbox0{\begin{overpic}[width=0.5\textwidth]{house}
        \put(50,20){foo}
    \end{overpic}}%
    \usebox{0}%
    \kern-\wd0\frame{\phantom{\usebox0}}
    \caption{framed figure include with \emph{overpic}.}
\end{figure}

\end{document}
David Carlisle
  • 757,742
  • I'm afraid it's not a rendering issue (see question Edit 1). you can try the pdf here http://tempsend.com/031D28982F Also my images are not white/transparent on border so your second solution isn't suitable :/ – fwachtel Mar 08 '15 at 22:23
  • See my edit above. It is easier to reproduce with a coloured semi-transparent image. – cfr Mar 08 '15 at 22:53