3

How to draw a well formed grid absolutely positioned on a page with tikz?

Each of the following pages has some problem. Is there a rounding error somewhere?

\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{grffile}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\usepackage{lipsum}
\usepackage{tikz}

\usetikzlibrary{calc}

\date{}
\title{}

\begin{document}

\thispagestyle{empty}

% wrong join on left, not closed on right
\begin{tikzpicture}[remember picture, overlay]
\draw[xshift=-1mm,yshift=-1mm,step=8.0mm,thin,black] ($(current page.south west) + (17mm, 17mm)$) grid ($(current page.south west) + (17mm,17mm) + (176mm,264mm)$);
\end{tikzpicture}

\pagebreak

\thispagestyle{empty}

% wrong join on left, not closed on right
\begin{tikzpicture}[remember picture, overlay]
\draw[xshift=0mm,yshift=0mm,step=8.0mm,thin,black] ($(current page.south west) + (17mm, 17mm)$) grid ($(current page.south west) + (17mm,17mm) + (176mm,264mm)$);
\end{tikzpicture}

\pagebreak

\thispagestyle{empty}

% wrong join on left, wrong join on right
\begin{tikzpicture}[remember picture, overlay]
\draw[xshift=0mm,yshift=0mm,step=8.0mm,thin,black] ($(current page.south west) + (17mm, 17mm)$) grid ($(current page.south west) + (17mm,17mm) + (177mm,265mm)$);
\end{tikzpicture}


\end{document}

enter image description here

2

Here is a perfect grid:

enter image description here

\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\usepackage{lipsum}
\usepackage{tikz}
\usetikzlibrary{calc}
\date{}
\title{}
\begin{document} 

\thispagestyle{empty}

\begin{tikzpicture}[remember picture, overlay, thin]
\coordinate (o) at ($(current page.south west) + (17mm, 17mm)$);
\foreach \x in {0,...,22}
  \draw ([xshift=8mm*\x]o) -- ++(0mm,8mm*33);
\foreach \y in {0,...,33}
  \draw ([yshift=8mm*\y]o) -- ++(8mm*22,0mm);
\end{tikzpicture}

\end{document}

Here is what the TikZ-pgf manual (sec. 14.8 The Grid Operation) page 154, says about this issue:

due to rounding errors, the "last" lines of a grid may be omitted. In this case, you have to add an epsilon to the corner points

So, you shouldn't rely on TikZ grids when high accuracy is required. This may change in a future version (I hope!).

EDIT:

Here is a manual fix if you insist on using the grids (I don't recommend):

\begin{tikzpicture}[remember picture, overlay]
\draw[step=8.0mm, thin,black] ($(current page.south west) + (17.29mm, 17.1mm)$) grid ($(current page.south west) + (193.30mm,281.11mm)$);
\end{tikzpicture}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.