# Misalignment between fbox and rest of the text

When defining the width of an fbox as \textwidth-2\fboxsep-2\fboxrule, it still sticks out a bit with respect to the rest of the text. Is there a width/length that I'm missing?

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

\edef\oldfboxsep{\the\fboxsep}
\fboxsep=15pt
\noindent\colorbox{gray!15!white}{
\parbox{\dimexpr\textwidth-2\fboxsep-2\fboxrule\relax}{%
\lipsum[1][1-3]
}}
\fboxsep=\oldfboxsep

\lipsum[1][6-10]
\end{document}


The output is the following, with the red and blue lines showing the misalignment.

• Looks like a wayward space. Add % after white}{ on the \noindent line, and another after \lipsum[1][1-3], just to be sure. Apr 8 '20 at 16:42
• @barbarabeeton Good catch, that works! If you turn your comment into an answer I'll accept it right away. Apr 8 '20 at 18:19

This is a very common problem, easy to make, and often unnoticed. (You have sharp eyes!)

(La)TeX interprets the end of a line to be a space, so if extra spaces aren't wanted, the line ending has to be made "invisible". That is accomplished by inserting a %,

In this example, two are needed:

• after white}{ on the \noindent line,
• after \lipsum[1][1-3].

That is,

\noindent\colorbox{gray!15!white}{%
\parbox{\dimexpr\textwidth-2\fboxsep 2\fboxrule\relax}{%
\lipsum[1][1-3]%
}}


There was already one in the "obvious" place after \relax}{%, so you're aware of the principle. Just look carefully at other potential spacy spots and clean up the ones that need it.

Edit
As pointed out in a comment by the author of the question, a space at the end of a paragraph isn't ordinarily added to the width, so the % after the \lipsum isn't needed. But there are two situations in which a space at the end of a paragraph does make a difference:

• if the last element of the paragraph is forced flush right, as with an epigraph;
• if the text in the last line of the paragraph fills the line completely with no room for another space, that space will go to the next line, so it might leave a visible blank line.

I haven't tested what will happen in the first case, but have often encountered, and been asked questions about, the second. In cases where a % will not do damage, it doesn't hurt to include it.

• Why do we need one after \lipsum[1][1-3]% though? I experimented with a text where the last line is long enough to be stretched to the right (\lipsum[1][1-6]%`), and the % doesn't do anything. I suspect that LaTeX doesn't stretch the last line in a paragraph. Apr 8 '20 at 18:48
• @Betohaku -- You are correct; LaTeX doesn't stretch the last line in a paragraph (unless told to do so, which isn't the case here). I'll update the answer. Apr 8 '20 at 19:04
• Never thought about those two situations you added, you're right: In cases where a % will not do damage, it doesn't hurt to include it. Apr 8 '20 at 20:44