9

I'm trying to draw a background grid on a page, but current page.north west is shifted down and to the right ~4pt. Anyone know how I can get the exact north west corner of the page?

Here's an example:

\documentclass{article}
\setlength{\parindent}{0pt}
\usepackage[papersize={2.5in,0.5in},margin=0in]{geometry}
\usepackage{tikz}
\pagestyle{empty}
\begin{document}%
\begin{tikzpicture}[remember picture,overlay]%
  \node at (current page.north west) [anchor=north west]{%
    \tikz{\draw[step=0.25in,color=red,thick] (0,0)
      grid (\paperwidth,\paperheight);}%
  };%
\end{tikzpicture}%
\begin{minipage}{\linewidth}%
In Ti\emph{k}Z, \texttt{current page.north west} is shifted $\sim$4pt
right and down%
\end{minipage}%
\end{document}

enter image description here

4
  • 1
    You should try something like \draw[red,thick] (current page.north west) grid[step=0.25in] (current page.south east);
    – remjg
    Aug 20, 2013 at 1:03
  • @remjg: That works if I do south east to north west and use the background package. Thanks for the hint! Aug 20, 2013 at 1:25
  • The exact north west corner is at (current page.north west). Why are you nesting TikZ pictures in the first place (see remjg’s comment)? Aug 20, 2013 at 2:58
  • @Qrrbrbirlbel: I'm nesting a TikZ picture inside a node because (1) I don't know what I'm doing :) and (2) that's what the author of the background package did in his answer to a related question. Aug 20, 2013 at 3:26

1 Answer 1

11

You are putting the grid inside a node and nodes have inner separation. Make inner sep zero.

\documentclass{article}
\setlength{\parindent}{0pt}
\usepackage[papersize={2.5in,0.5in},margin=0in]{geometry}
\usepackage{tikz}
\pagestyle{empty}
\begin{document}%
\begin{tikzpicture}[remember picture,overlay]%
  \node[inner sep=0pt,outer sep=0pt] at (current page.north west) [anchor=north west]{%
    \tikz{\draw[step=0.25in,color=red,thick] (0,0)
      grid (\paperwidth,\paperheight);}%
  };%
\end{tikzpicture}%
\begin{minipage}{\linewidth}%
In Ti\emph{k}Z, \texttt{current page.north west} is shifted $\sim$4pt
right and down%
\end{minipage}%
\end{document}

enter image description here

If your goal is to add a grid to the page, you may use pagegrid package. Just add \usepackage{pagegrid} in the preamble to get the grid.

Response to comment

Putting inner sep=-0.4pt, will give

enter image description here

This works well for the default line width. However, it is better to use inner sep = -\pgflinewidth instead of inner sep=-0.4pt. It may be noted that the default value of \pgflinewidth is 0.4pt. (Thanks to Jake for pointing out)

5
  • Thanks! I would have expected the middle of the grid lines to line up with the page corner (so that all around the edge of the page you'd see a 1/2 thickness red line), but I can work with this. Aug 20, 2013 at 1:15
  • Yes, that's what I want. I assume that the thickness of the line is 0.8pt? I'm guessing that the center of the grid lines aren't aligned with the corner of the page without that offset because of anchor=north west (I'm guessing that puts the anchor at the north west corner of the bounding box, which would include the entire thickness of the lines). Aug 20, 2013 at 3:24
  • @RichardHansen: Is there a reason that you use a node for the grid instead of \draw ($(current page.north west)+(-\pgflinewidth/2,\pgflinewidth/2)$) grid …
    – Tobi
    Aug 20, 2013 at 9:20
  • @Tobi: No reason, other than that's what the author of the background package did in his answer to a related question. I'm still trying to understand TikZ (its syntax rules are not straightforward), so I was copying what I saw elsewhere. A comment by remjg led me to a simpler way to do what I want, but I'm still glad I learned what went wrong with the awkward approach used in my question. Aug 20, 2013 at 17:00
  • @RichardHansen: The tutorials in the manual (pgfmanual.pdf) are a good start to learn TikZ. A node can be used to add text to a picture other picture elements (such as grids) don’t require a node. The nodes are much more powerful but too powerful for this comment ;-)
    – Tobi
    Aug 20, 2013 at 20:45

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.