[Tex/LaTex] Why isn’t \textwidth the width of the text

margins

I'm trying to display two things (a bordered box and a shaded box), both of which stretch across the entire text area – that is, since I have 1cm margins, the box should start 1cm from the left edge of the page and continue until 1cm from the right edge of the page.

Unfortunately it seems to start too far in (~1.5cm) and continue too far to the right (~0.5cm from the edge.) What am I doing wrong?

\documentclass[a4paper,10pt]{report}
\usepackage[portrait,a4paper,margin=1.0cm,headsep=5mm]{geometry}
\usepackage{color}
\begin{document}

\framebox[\textwidth]{test}

\colorbox[gray]{0.85}{
    \parbox{\textwidth}{test}
}

\end{document}

Best Answer

It is an indentation problem. When you start a new paragraph the text (or everything else) is indented by a certain amount specified in \parindent. The solution is to say to LaTeX that doesn't have to indent that line using \noindent:

\documentclass[a4paper,10pt]{report}
\usepackage[portrait,a4paper,margin=1.0cm,headsep=5mm]{geometry}
\usepackage{color}
\begin{document}

\framebox[\textwidth]{test}

\colorbox[gray]{0.85}{
    \parbox{\textwidth}{test}
}
\noindent\framebox[\textwidth]{test}

\noindent\colorbox[gray]{0.85}{
    \parbox{\textwidth}{test}
}

\end{document}

I am not sure but I assume that the \colorbox is larger than \textwidth because of an inner border respect to its content that should be considered when you specify the \parbox width