# TikZ — Line caps extend over/past grid line boundaries

I am plotting a line L in a rectangular coordinate system. The coordinate system has grid lines. (I'm using \draw[help lines] (-5,-5) grid (5,5), so the grid lines form a 10-by-10 box for the coordinate system.) The line L is ultra thick. I have extended L to the edges of the grid lines. The problem is that when you zoom-in on those two points where L meets the boundary of the grid lines, the corners of the line caps for L extend slightly past the grid lines. Is there a way to plot L to the edges of the grid lines, only to have the line caps of L be cut off once it meets the grid lines? Basically, I want L extended to the grid lines, and I don't want anything--including the line caps for L--to go over top of and past the grid lines.

-
Does the solution provided in A line of length \textwidth in TikZ solve your problem? For example, drawing L with [line cap=rect] option and perhaps removing some \pgflinewidth? Give some feedback. –  Werner Jul 12 '12 at 3:06

Choosing a good line cap (like butt) can help... but it's not sufficient (look at first blue line). Using a clip region is the solution (like with my second scope).

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\begin{scope}
\draw[help lines] (-5,0) grid (0,5);

\draw[line width=5mm,line cap=round] (-5,4) -- (0,4);
\redpt{-5,4}
\draw[line width=5mm,line cap=rect] (-5,3) -- (0,3);
\redpt{-5,3}
\draw[line width=5mm,line cap=butt] (-5,2) -- (0,2);
\redpt{-5,2}
\draw[line width=5mm,line cap =butt,blue] (-5,1) -- (0,3.5);
\redpt{-5,1}
\end{scope}

\begin{scope}[shift={(0,-5.2)}]
\draw[help lines] (-5,0) grid (0,5);

\clip (-5,0) rectangle (0,5);

\draw[line width=5mm,line cap=round] (-5,4) -- (0,4);
\redpt{-5,4}
\draw[line width=5mm,line cap=rect] (-5,3) -- (0,3);
\redpt{-5,3}
\draw[line width=5mm,line cap=butt] (-5,2) -- (0,2);
\redpt{-5,2}
\draw[line width=5mm,line cap =butt,blue] (-5,1) -- (0,3.5);
\redpt{-5,1}
\end{scope}
\end{tikzpicture}
\end{document}


-
Thank you very much, all, especially you, PolGab. I'm still the TikZ novice. This solved my problem. I wish I had read about clipping beforehand. –  Nathan Jul 12 '12 at 15:32
@Nathan: Thank you for your thanks. But it's more effective to mark this answer as accepted... ;-) –  Paul Gaborit Jul 12 '12 at 15:57
Ah, I see I see. That I can do, friend. ;) –  Nathan Jul 12 '12 at 17:43