[Tex/LaTex] Border around figure does not fit

floatsgraphicsspacing

I want to insert a bitmap image in my document. I would like to put a tight and thin border line around the image. Apparently, the way to do this is with \framebox. I want to have the border fit exactly around the image (no space between the border and the image). So I set \setlength\fboxsep{0pt}. However, for some reason the box does not fit. At the top and bottom, the line fits exactly, but there is extra space at the left and right side.
Here is my code:

\begin{figure}[htb]
\centering
\setlength\fboxsep{0pt}
\setlength{\fboxrule}{1pt}
\framebox{
    \includegraphics{Fig1.jpg}
}\end{figure}

The same issue will become apparent with a very simple example like this:

\setlength{\fboxsep}{0pt}
\fbox{%
  \rule{4cm}{3cm}
}

Am I doing something wrong, or did I run across a bug?

Best Answer

There is no difference between a line of text made up of characters and a line that includes a graphic. TeX will consider it as just a single box; it will also consider all line endings as an extra space. You will have to use percentage characters as shown in the minimal below:

\documentclass{article}
\usepackage{graphicx,xcolor}
\begin{document}
\begin{figure}[htb]
\centering
\setlength\fboxsep{0pt}
\setlength{\fboxrule}{1pt}
\framebox{%
\color{red}
  \rule{35pt}{35pt}%
}
\end{figure}
\end{document}

I have used a rule to indicate the includegraphics. It is a good idea to always provide a minimal working example for other's to use to test their answers and a rule is convenient in this respect.

Related Question