1

I'm trying to get a wider (relative to the grid line width) line to extend to end of a grid without going past the end (in red). With the default options, the red line doesn't reach the boundary. Setting line cap=rect on the red line then makes it too long. I tried clipping, but it seems the reference point is center of the black line instead of the boundary, leading me back to result with the default options. Is there an easy way to have the red line go the end of the boundary of the box without fiddling with the coordinates?

This seems similar, but the line caps here also terminate before the outer boundary of the grid.

MWE:

\documentclass{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
  \draw[line width=1pt, step=1, line cap=rect] (1,1) grid (4,2);

  \begin{scope}
  \clip (1,0) rectangle (4,5);
  \draw[line width=2pt, yshift=2, color=red, line cap=rect] (1,1) -- (4,1);
  \end{scope}
\end{tikzpicture}
\end{document}

Current result:

enter image description here

Desired result:

enter image description here

Almacomet
  • 361

1 Answers1

2

I think you will have to fiddle at least a bit with the coordinates, as you have to tell your red line that the grid has a line width of 1pt:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \draw[line width=1pt, step=1, line cap=rect] (1,1) grid (4,2);

  \draw[line width=2pt,red,yshift=2,shorten >=-0.5pt,shorten <=-0.5pt] (1,1)  -- (4,1);

\end{tikzpicture}
\end{document}

enter image description here

Ulrike Fischer
  • 327,261