[Tex/LaTex] A line of length \textwidth in TikZ

lengthsmarginstikz-pgf

Why does the following latex document:

\documentclass{minimal}
\usepackage{tikz}

\begin{document}
    \noindent
    \begin{tikzpicture}
        \draw (0,0) -- (\textwidth,0);
    \end{tikzpicture}
\end{document}

give an overfull \hbox of 0.4pt? The line cap option has no effect. An ultra thick line gives a larger overfull \hbox of 1.6pt.

Is there a better (more TikZ-like) way to achieve the same effect, i.e., draw a line to the edge of the text box? Ideally I'd like to do this without scaling the figure.

Best Answer

The code can be adapted very easily to not get the warning anymore:

\documentclass{minimal}
\usepackage{tikz}

\begin{document}
    \noindent
    \begin{tikzpicture}
        \draw[line cap=rect] (0,0) -- (\linewidth-\pgflinewidth,0);
    \end{tikzpicture}
\end{document}

EDIT: I think I also have an explanation: Each time tikz draws a line, it uses 2 bounding boxes (cf. PGF manual v 2.10 - page 579):

As a side-effect, the path construction commands keep track of two bounding boxes. One is the bounding box for the current path, the other is a bounding box for all paths in the current picture.

Section 7.13 details these parameters.

What I think is happening, is that regardless of the way how the line ends (line cap), its bounding box protrudes by \pgflinewidth beyond the edge of the text area by this length. This is probably necessary to accommodate the whole line in case of nicely rounded line caps. Since this requires in fact .5\pgflinewidth on each end and the bounding box of the line starts at exactly (0,0) the overhang will be \pgflinewidth at the end of the line.