Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

In A package to help with layout generation? Graph paper in the background? the question was asked in the comments as to why it was necessary to shift the whole picture in order to line up the grid properly. It never had a satisfactory answer.

Unfortunately, it appears that the position as well as the grid generation routine are influenced by the paragraph indentation.

Consider the MWE below and run it with both commented as well as un-commented lines.

\documentclass{article}
\usepackage{tikz}
\begin{document}
%\null
%\noindent
\begin{tikzpicture}[remember picture, overlay]
     \draw[line width=1pt,color=teal,step=1cm] (current page.south west) grid ++(5cm,5cm);
  \end{tikzpicture}
XXXX
\end{document}

This produces the following:

enter image description here

If you un-comment the lines you get a different layout. If you add a shift of 7.8mm it results in integral squares

\draw[line width=1pt,color=teal,step=1cm,xshift=-7.8mm,yshift=-7.8mm] (current page.south west) grid ++(5cm,5cm);

What is the right way to compensate and why does the algorithm does not produce integral squares? How can I enforce integral squares?

share|improve this question
    
As it seems, the starting point of the grid is still the top left of the text area, not one of the corners. If you have a way to find out where this point is relative to the corner of your choice, you can use xshift and yshift. –  Tom Bombadil May 19 '12 at 16:04
    
@TomBombadil: Yes, exactly. See my response below how to find out that point. –  Yori May 19 '12 at 16:07
    
@Yori: I started writing my comment before your answer was posted and only afterward saw it. But the more interesting point to me would be: how do you find out such tings? –  Tom Bombadil May 19 '12 at 16:10
    
@TomBombadil: I found out about this by noticing that the placement seemed changed after compiling a second time. That indicates that there is something in the .aux file that affects the placement. When I looked at the .aux file I saw it defined a point which I guessed was the point I was looking for. Then after some googling around I figured out how to retrieve the actual coordinates of that point. It's a matter of trial-and-error I guess. –  Yori May 19 '12 at 16:16
    
@Yori I did notice that as well. That is why I put the XXX. It also draws the grid starting from that point leftwards and then rightwards. –  Yiannis Lazarides May 19 '12 at 16:44

2 Answers 2

up vote 6 down vote accepted

I guess the trick is to shift the grid slightly. The required shift is defined in the .aux file, which defines a point called pgfid<x>, where <x> is refers to the index of the tikzpicture. In general, this number is given by \pgfpictureid. The following code seems to work (after compiling twice) for all pages.

\documentclass{article}
\usepackage{tikz}
\usepackage{lipsum}

\begin{document}

\newlength\shiftx
\newlength\shifty

\makeatletter
\newcommand\calcshift{%
   \pgfsys@getposition{\pgfpictureid}\@basepoint%
   \pgf@process{\pgfpointorigin\@basepoint}%
   \setlength\shiftx\pgf@x%
   \setlength\shifty\pgf@y%
}
\makeatother

\begin{tikzpicture}[remember picture, overlay]
    \calcshift
    \draw [line width=1pt,color=teal,step=1cm,xshift=-\shiftx, yshift=-\shifty] 
         (current page.south west) grid ++(5cm,5cm);
\end{tikzpicture}

\lipsum{4}

% --- just some other tikz picture
\begin{tikzpicture}
  \draw (0, 0) rectangle (1, 1);
\end{tikzpicture}

\begin{tikzpicture}[remember picture, overlay]
    \calcshift
    \draw [line width=1pt,color=teal,step=1cm,xshift=-\shiftx, yshift=-\shifty] 
         (current page.south west) grid ++(5cm,5cm);
\end{tikzpicture}

\end{document}
share|improve this answer
    
I would love it to work, but if you add some lipsum etc, will fail on page 2. The correction needs to be properly adjusted for odd and even pages. Thanks for spotting the reference in the aux. –  Yiannis Lazarides May 19 '12 at 16:44
    
@YiannisLazarides: I expanded my code a bit by wrapping the calculation into a command, \calcshift. On my TeX distribution, this looks fine to me. If it does not work, can you give an MWE? –  Yori May 19 '12 at 19:50
    
Works perfect, thanks and great work. –  Yiannis Lazarides May 19 '12 at 20:13

A new example with a tikzpicture inside a tikzpicture but simplest is the second method

1)

\documentclass{article}
\usepackage{tikz}
\usepackage{lipsum} 
\begin{document}
\begin{tikzpicture}[remember picture, overlay]
  \node[anchor=south west,inner sep=0pt] at (current page.south west)
  {\tikz \draw[line width=1pt,color=teal] (0,0) grid ++(5cm,5cm);};
  \end{tikzpicture}%
\lipsum

\end{document}

enter image description here

2)

\documentclass{article}
\usepackage{tikz}
\usepackage{lipsum} 
\begin{document}
\begin{tikzpicture}[remember picture, overlay,shift=(current page.south west)    ]
\draw[line width=1pt,color=teal] (0,0) grid ++(5cm,5cm);
  \end{tikzpicture}%
\lipsum

\end{document} 
share|improve this answer
    
Nice solution, thank you. –  Yiannis Lazarides Jun 14 '12 at 19:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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