1

I'm trying to create an outline for my TikZ grid with thicker lines, but there doesn't seem to be any correction for the edges, so things get jagged:

\documentclass{article}

\usepackage{tikz}

\begin{document}
  \begin{tikzpicture}
    \pgfmathsetmacro{\step}{10 / (19 - 1)}
    \draw[step=\step] (0, 0) grid (10cm, 10cm);
    \draw[step=10cm, line width = 0.7mm] (0, 0) grid (10cm, 10cm);
  \end{tikzpicture}
\end{document}

Tikz Jagged Grid Outline

I thought TikZ's grid would natively fix this, as it doesn't sound to be an uncommon use case. Maybe there's a grid argument I'm missing?

Anyways, I've tried fixing this with simple lines. I thought I should maybe just pad things with half the line width on the top and the bottom and I would be good to go. But that didn't work at all. So, what's the formula here? Am I getting something wrong in how I'm calculating things in TeX?

\documentclass{article}

\usepackage{tikz}

\newcommand{\boardOutline}[1]{
  \pgfmathsetmacro{\boardOutlineLineWidth}{0.8mm}
  % Just so the board outline isn't jagged
  \pgfmathsetmacro{\boardOutlinePadding}{\boardOutlineLineWidth / 2} % Multiplying by `0.0176` was the closest I got to something non-jagged

  % Top
  \draw[black, line width = \boardOutlineLineWidth]
    (-\boardOutlinePadding, #1) -- (#1 + \boardOutlinePadding, #1);
  % Right
  \draw[black, line width = \boardOutlineLineWidth] 
    (#1, 0) -- (#1, #1);
  % Bottom
  \draw[black, line width = \boardOutlineLineWidth]
    (-\boardOutlinePadding, 0) -- (#1 + \boardOutlinePadding, 0);
  % Left
  \draw[black, line width = \boardOutlineLineWidth]
    (0, 0) -- (0, #1);
}

\begin{document}
  \begin{tikzpicture}
    \pgfmathsetmacro{\step}{10 / (19 - 1)}
    \draw[step=\step] (0, 0) grid (10cm, 10cm);
    \boardOutline{10cm}
  \end{tikzpicture}
\end{document}

This is what I get if I divide by 2:

Grid Outline with Dividing by 2

The closest I've got to something non-jagged was multiplying by 0.0176, I have no idea why.

5
  • 2
    generally better to draw a single multi-step path rather than multiple paths then the corners are taken care of (with options for mitering etc) Commented Feb 11 at 15:11
  • 2
    @Philippe Fanaro try line cap=rect
    – polyn
    Commented Feb 11 at 15:15
  • @DavidCarlisle Interesting. But, on a side note, given that my line padding width seems to be wrong, I think even if drew a single path, it would still be wrong.
    – psygo
    Commented Feb 11 at 15:15
  • @polyn Dang. I think that solves everything (in the first code block of the question).
    – psygo
    Commented Feb 11 at 15:17
  • @polyn I don't think line cap = rect answers why dividing by 2 isn't working, but I think it's a helpful answer.
    – psygo
    Commented Feb 12 at 12:40

1 Answer 1

0

I don't know why using the padding of line width / 2 doesn't work.

But, like @polyn mentioned in the comments, using line cap = rect does work, e.g.:

\draw[..., line cap = rect] (0, 0) grid (10cm, 10cm);

You must log in to answer this question.

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