# Border around figure does not fit

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?

-

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.

-

Line breaks in the source code are like white space. You can remove them by ending the line by %, then the extra space before and after the image would be gone:

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


Perhaps you already noticed that people often end lines by %. This possible whitespace is a reason.

Often it makes no difference, as white space is ignored after a control sequence such as a macro name, after numbers, between argument delimiters. Here it's relevant white space. If in doubt, comment the line break out.

-

For an image you can use \frame

\documentclass{article}
\usepackage{graphicx,xcolor}
\begin{document}

\begin{figure}
\centering
\frame{\scalebox{5}{F}}
\end{figure}

\end{document}

-