The 1 inch offsets come actually from TeX itself, and are kind of a pain because you need to take them into account as late as possible in any calculations.
The paper size is defined in a configuration file (I forget its name), but with PDFTeX, you should be able to use commands \pdfpagewidth
(default 210 true mm
for A4), and \pdfpageheight
(default 297 true mm
for A4).
Most relevant settings regarding page layout are:
\hsize
for h orizontal size (typeblock width)
\vsize
for v ertical size (typeblock height)
\hoffset
for h orizontal offset (offset from 1 inch from left edge of paper)
\voffset
for v ertical offset (offset from 1 inch from top edge of paper)
For spread layout, you can just toggle the \hoffset
in the output routine.
Because you define the \hsize
and \hoffset
, there's no need to define margins, but I guess you could write a macro for counting \hsize
and \hoffset
based on given margins.
Note that the line widths may not look even in the posted PNGS. This is an artefact of a small screen/PDF viewer combination and has nothing to do with the actual PDF. It just affects my PNG clippings.
I think this solution satisfies the various desiderata:
- Can be adapted to other paper sizes by adjusting the definitions of the
\step
s.
- The north-west corner of the paper is at the origin and the grid lines align with the physical dimensions of the page in the sense that the north-west corners of both a small square and a large square of the grid are aligned with the north-west corner of the page.

- The grid overlays other page contents, including
tikzpicture
s, even if these use overlay, remember picture
themselves.

\showgrid
can be specified anywhere on the page where the grid is required, including before any tikzpicture
s, even if they themselves use overlay, remember picture
.
- The grid is shown only on the page(s) it is requested. No grid will be used for the following page. (But it would be easy to adapt this so that it was shown on every page or whatever. See the documentation of
atbegshi
.)
I drew the grid by hand, drawing the horizontal and vertical lines separately. I use the backgrounds
library to ensure that the lighter lines are not drawn over the darker lines (which looks rather odd).
I use atbegshi
to ensure the grid is placed above any and all page content.
\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{backgrounds}
\usepackage{anyfontsize}
\newcommand{\showgrid}{%
\AtBeginShipoutNext{\AtBeginShipoutAddToBoxForeground{%
\begin{tikzpicture}
[
overlay,
remember picture,
inner sep=0pt,
outer sep=0pt,
minor line/.style={help lines, draw=gray!25, on background layer},
major line/.style={help lines, draw=gray},
]
\foreach \step in {0,...,210} {
\pgfmathsetmacro\gridlineconfig{ifthenelse(equal(int(mod(\step,10)),0),"major line","minor line")}%
\draw [\gridlineconfig] ($(current page.north west) + (\step mm,0)$) -- ($(current page.south west) + (\step mm,0)$);
}
\foreach \step in {0,...,297} {
\pgfmathsetmacro\gridlineconfig{ifthenelse(equal(int(mod(\step,10)),0),"major line","minor line")}%
\draw [\gridlineconfig] ($(current page.north west) - (0,\step mm)$) -- ($(current page.north east) - (0,\step mm)$);
\node [anchor=north] at ($ (current page.north west) + (\step mm,0) $) {\fontsize{1}{2}\selectfont \step};
\node [anchor=west] at ($ (current page.north west) - (0,\step mm) $) {\fontsize{1}{2}\selectfont \step};
}
\end{tikzpicture}
}%
}%
}
\usepackage{atbegshi}
\begin{document}
\thispagestyle{empty}
\showgrid
\begin{tikzpicture}[overlay,remember picture,every node/.style={fill=red,inner sep=0pt,outer sep=0pt}]%
\node [minimum width=2cm,minimum height=2cm] at (current page.center) {};
\end{tikzpicture}
\end{document}
Best Answer
Just to add another useful package that does it:
(as many people load
geometry
anyways, it is a nice gadget)