Tikz grid does not use tikzpicture unit lengths

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

\def\[email protected]@x{1cm}%
\def\[email protected]@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 \[email protected]. Then tikz can judge if it is with a unit by \[email protected], 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\[email protected]@@[email protected]#1#2#3){%
  \[email protected]@,{#3}%
  \[email protected]@%
    \[email protected]@splitxyz{#1}{#2}#3,%
  \else%
    \[email protected]{#2}%
    \[email protected]%
      \[email protected]{#3}%
      \[email protected]%
        \def\[email protected]{#1{\pgfpoint{#2}{#3}}}%
      \else%
        \def\[email protected]{#1{\pgfpointadd{\pgfpoint{#2}{0pt}}{\pgfpointxy{0}{#3}}}}%
      \fi%
    \else%
      \[email protected]{#3}%
      \[email protected]%
        \def\[email protected]{#1{\pgfpointadd{\pgfpoint{0pt}{#3}}{\pgfpointxy{#2}{0}}}}%
      \else%
        \def\[email protected]{#1{\pgfpointxy{#2}{#3}}}%
      \fi%
    \fi%
  \fi%
  \[email protected]%
}%

Similar parse mechanism is also applied to xstep and ystep

    \[email protected]{\[email protected]@x}%
    \[email protected]%
      \[email protected]{\pgfpoint{\[email protected]@x}{0pt}}%
    \else%
      \[email protected]{\pgfpointxy{\[email protected]@x}{0}}%
    \fi%
    \[email protected]=\[email protected]%
    \[email protected]=\[email protected]%
    \[email protected]{\[email protected]@y}%
    \[email protected]%
      \[email protected]{\pgfpoint{0pt}{\[email protected]@y}}%
    \else%
      \[email protected]{\pgfpointxy{0}{\[email protected]@y}}%
    \fi%
    \advance\[email protected] by\[email protected]%
    \advance\[email protected] by\[email protected]%
    \pgfpathgrid[stepx=\[email protected],stepy=\[email protected]]%
      {\pgfqpoint{\[email protected]}{\[email protected]}}{\pgfqpoint{\[email protected]}{\[email protected]}}%

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