# \fbox command destroys margin

I included a graphic the following way:

\includegraphics[width=1\textwidth]{questionnaire1.png}


Then I changed it so that a frame is drawn around the graphic:

\fbox{\includegraphics[width=1\textwidth]{questionnaire1.png}}


But now, the whole graphic with the border is move 1-2 cm to the right side of the page so that is not in line with the other content. How can I change this?

-
How do you use this exactly? Does it start a new paragraph, i.e. is there a empty line before it? – Martin Scharrer Apr 18 '11 at 15:27
@Martin Scharrer The picture has exactly the size of a whole page. So there is no paragraph. – RoflcoptrException Apr 18 '11 at 15:28
The size doesn't matter. There might be a paragraph nonetheless. – Martin Scharrer Apr 18 '11 at 15:32
@Martin Scharrer Yes that's true, I tried \centering for each graphic and this would work, but I think the better solution would be the code in your answer. Thanks. – RoflcoptrException Apr 18 '11 at 15:34

I would add \noindent to avoid paragraph indention, which might be the case here. Also the \fbox adds some space around its content, i.e. \fboxsep on each side. I would subtract this amount from the image width to have the image including the frame exactly \textwidth wide. You could also subtract the line width of the frame as well.

\noindent\fbox{\includegraphics[width=\dimexpr\textwidth-2\fboxsep-2\fboxrule\relax]{questionnaire1.png}}

-
This seems like a good idea, but unfortunately I get an exception ! Illegal unit of measure (pt inserted). <to be read again> * l.136 ...p-2*\fboxrule\relax]{questionnaire1.png}} ? ´ – RoflcoptrException Apr 18 '11 at 15:30
@Roflcoptr: I fixed it. The * caused the troubles. They are not required anyway. – Martin Scharrer Apr 18 '11 at 15:34
Thanks this works like a charm! – RoflcoptrException Apr 18 '11 at 15:37

Depends on why it is moving. Probably you forgot \fboxsep, \fboxrule and/or \parindent.

\documentclass{article}
\usepackage{lipsum,xcolor}
\begin{document}
\lipsum[1]

\fbox{%
\color{red}\rule{\textwidth}{1cm}}

\noindent\fbox{%
\color{red}\rule{\textwidth}{1cm}}

\noindent\fbox{%
\color{red}\rule{\dimexpr\textwidth-2\fboxrule-2\fboxsep}{1cm}}

\noindent\makebox[\textwidth]{%
\fbox{%
\color{red}\rule{\dimexpr\textwidth}{1cm}}}

\noindent
\begingroup\fboxsep=0pt
\fbox{%
\color{red}\rule{\dimexpr\textwidth-2\fboxrule}{1cm}}
\endgroup
\end{document}

-
Yes, \noindent\makebox[\textwidth] is probably the best thing to do. With [c] as second optional argument you can center the image even when it is wider than \textwidth when the frame is added. – Martin Scharrer Apr 18 '11 at 15:36