3
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usepackage{lipsum}         

\begin{document}
    \lipsum[2]
    \noindent 
    \begin{tikzpicture}
    \draw[red, line width=.2cm] (0,0)--(\textwidth,  0) ;
    \end{tikzpicture} 
\end{document}

enter image description here

Here is the code and the picture.

Do you know why the red line is overflow ?

Thanks for your help.

Clairon.

8
  • Would you mind if I added a clearer example to your question? (using a zero sep \fbox and a line instead of text)
    – daleif
    Aug 19, 2022 at 12:26
  • 2
    TikZ/PGF adjust the bounding box on both sides about half the line width. For better or worse. Quick fix is either define your own bounding box \useasboundingbox (0,0) (\textwidth);, use the trim left and trim right keys or use \clip (depending on the complexity or your real diagram). Aug 19, 2022 at 12:27
  • @Qrrbrbirlbel \useasboundingbox?
    – daleif
    Aug 19, 2022 at 12:28
  • @Qrrbrbirlbel good observation, hadn't realised that it was proportional to the line size.
    – daleif
    Aug 19, 2022 at 12:29
  • 1
    @daleif Otherwise, for a pure vertical line the bounding box's width would be 0pt which might make the line intrude on surrounding text. There's also line joins and weird angle and all that annoying stuff. Aug 19, 2022 at 12:34

2 Answers 2

1

When PGF (which TikZ is a frontend to) builds a picture then it keeps track of that pictures bounding box. This is what is then used by TeX to determine the size of the graphic. Usually, PGF simply tracks the coordinates that are used and adjusts the bounding box to contain them. Occasionally, this isn't quite right (bézier curves tend to get overlarge bounding boxes) but by-and-large it is and being more accurate is more effort that it is worth. However, one adjustment that is made is to take the line width into account, since although a coordinate might be where a path ends, the actual path will extend slightly beyond that coordinate in some direction. Again, PGF takes the simple approach and simply adds half the current line width to ensure that the bounding box is big enough.

So if you draw a path of line width 1cm from (0,0) to (5,0) then the actual bounding box will have lower left corner of (-.5,-.5) and upper right corner of (5.5, .5).

Now, this additional half linewidth gets added on in both horizontal and vertical directions even if the line doesn't actually need them. This is what I meant by PGF taking the simple approach.

In your code, your line is of width .2cm so an extra .1cm is added in all directions to the obvious bounding box, including to the left and right. So your graphic is actually \textwidth + .2cm in width, hence the overflow.

Fortunately, whenever PGF takes a simple option it often provides a way to correct for it. In this case, you can issue the keys trim left and trim right to the tikzpicture. These take x-coordinates as arguments (or points, whereupon TikZ just reads in the x-coordinate of that point) and so trim left=0 or trim left={(0,0)} (the braces are because of the comma in the coordinate) says "Trim the left-hand side of the bounding box so that its left-hand edge has x-coordinate 0." (note that the "trim" can add space if the coordinate given lies off to the left of the actual picture).

So in your case you would add trim left=0, trim right=\textwidth.

Here's some code to illustrate that. I've added a rectangle at the end of each picture to show the current bounding box in effect at that point which shows the excess region beyond the red line. In the second one, I've used the trim keys to adjust the bounding box. Note that this only affects the bounding box that is passed to TeX - the tikzpicture itself thinks that the bounding box is unchanged (if you want to change it from TikZ's point of view, look up the key use as bounding box in the manual).

\documentclass{article}
%\url{https://tex.stackexchange.com/q/654551/86}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usepackage{lipsum}         

\begin{document}
\lipsum[2]

    \noindent 
    \begin{tikzpicture}
\draw[red, line width=.2cm] (0,0)--(\textwidth,  0) ;
\draw[overlay] (current bounding box.south east) rectangle (current bounding box.north west); 
    \end{tikzpicture} 

    \noindent 
    \begin{tikzpicture}[trim left=0,trim right=\textwidth]
\draw[red, line width=.2cm] (0,0)--(\textwidth,  0) ;
\draw[overlay] (current bounding box.south east) rectangle (current bounding box.north west); 
    \end{tikzpicture} 

\end{document}

Demonstration of the additional space added by PGF to the bounding box of a picture depending on the line width

Thanks to Qrrbrbirlbel in the comments for pointing out that one can use just x-coordinates in the arguments to the trim keys.

3
  • 1
    I'd even just use trim left=0, trim right=\textwidth since the y value is irrelevant (and we don't use named nodes). Aug 19, 2022 at 12:48
  • @Qrrbrbirlbel You're right - I thought I'd tried that and it hadn't worked, but in retrospect I think I was expecting, say, trim ABC=1cm to trim by the length 1cm not to the distance 1cm, so I looked at the code (quicker than loading up the manual!) and saw that one could specify a point and that worked. Aug 19, 2022 at 12:52
  • @Qrrbrbirlbel On the principle that the simpler code is better, I've edited my answer in line with your comment. Incidentally, I hadn't seen your comment on the question when I started composing my answer - I'm not trying to steal your rep! (I feel like this is the second time recently that I've done this ...) Aug 19, 2022 at 12:56
3

For simple line after paragraph, I would rather use colored \rule as follows:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{xcolor}
%--------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%
\usepackage{lipsum}                             % for dummy text
%---------------------------------------------------------------%

\begin{document}    
    \lipsum[66]
    \textcolor{red}{\rule{\textwidth}{2mm}}
\end{document}

enter image description here

(red lines indicate text area borders)

Not the answer you're looking for? Browse other questions tagged .