Tikz grid does not use tikzpicture unit lengths

Grid separation is controlled by xstep and ystep which store the value in \tikz@grid@x and \tikz@grid@y respectively. Their initial value are defined in tikz.code.tex

\def\tikz@grid@x{1cm}%
\def\tikz@grid@y{1cm}%

In your second example, you set x=0.5cm, y=0.5cm, which means (-2, -2) is parsed as (-1cm, -1cm) and (2, 2) is parsed as (1cm, 1cm). This is because when coordinates is passed to parse, they are first checked if the x and y coordinate is with unit by \tikz@checkunit. Then tikz can judge if it is with a unit by \iftikz@isdimension, if not the coordinate is set by \pgfpointxy, otherwise the coordinate is set by \pgfpoint. Actually there are four cases to handle, related code is

\def\tikz@@@parse@regular#1#2#3){%
  \pgfutil@in@,{#3}%
  \ifpgfutil@in@%
    \tikz@parse@splitxyz{#1}{#2}#3,%
  \else%
    \tikz@checkunit{#2}%
    \iftikz@isdimension%
      \tikz@checkunit{#3}%
      \iftikz@isdimension%
        \def\pgfutil@next{#1{\pgfpoint{#2}{#3}}}%
      \else%
        \def\pgfutil@next{#1{\pgfpointadd{\pgfpoint{#2}{0pt}}{\pgfpointxy{0}{#3}}}}%
      \fi%
    \else%
      \tikz@checkunit{#3}%
      \iftikz@isdimension%
        \def\pgfutil@next{#1{\pgfpointadd{\pgfpoint{0pt}{#3}}{\pgfpointxy{#2}{0}}}}%
      \else%
        \def\pgfutil@next{#1{\pgfpointxy{#2}{#3}}}%
      \fi%
    \fi%
  \fi%
  \pgfutil@next%
}%

Similar parse mechanism is also applied to xstep and ystep

    \tikz@checkunit{\tikz@grid@x}%
    \iftikz@isdimension%
      \pgf@process{\pgfpoint{\tikz@grid@x}{0pt}}%
    \else%
      \pgf@process{\pgfpointxy{\tikz@grid@x}{0}}%
    \fi%
    \pgf@xb=\pgf@x%
    \pgf@yb=\pgf@y%
    \tikz@checkunit{\tikz@grid@y}%
    \iftikz@isdimension%
      \pgf@process{\pgfpoint{0pt}{\tikz@grid@y}}%
    \else%
      \pgf@process{\pgfpointxy{0}{\tikz@grid@y}}%
    \fi%
    \advance\pgf@xb by\pgf@x%
    \advance\pgf@yb by\pgf@y%
    \pgfpathgrid[stepx=\pgf@xb,stepy=\pgf@yb]%
      {\pgfqpoint{\pgf@xa}{\pgf@ya}}{\pgfqpoint{\tikz@lastx}{\tikz@lasty}}%

In your example they are just 1cm, so the same fig can be draw by

\draw (-1cm, -1cm) grid (1cm, 1cm);

and \fill (1, 1) circle [radius=3pt]; is equal to \fill (0.5cm, 0.5cm) circle (3pt); which is not on any grid point.


According to the pgfmanual, the grid is drawn with steps as configured through /tikz/step=h which as far as the manual says is a number or dimension or coordinate and has (no default, initially 1cm).

So if you want the grid to be not drawn with 1cm, you have to change the step, as in (or locally, just for the grid drawing command):

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
    \draw[very thick] (-2,-2) rectangle (2,2);
    \draw (-2,-2) grid (2,2);
    \fill (1,1) circle [radius=3pt];
\end{tikzpicture}


\begin{tikzpicture}[x=0.5cm,y=0.5cm,step=0.5cm]
    \draw[very thick] (-2,-2) rectangle (2,2);
    \draw (-2,-2) grid (2,2);
    \fill (1,1) circle [radius=3pt];
\end{tikzpicture}

\end{document}

enter image description here

Tags:

Tikz Pgf