# TikZ: anchors of special node “current page” seem to be incorrect in landscape

I've got a document that I wish to format in landscape in conjunction with some material added as an overlay via TikZ. I can get everything correct if I create a portrait page, but not if I create a landscape page.

Here's my MWE for portrait which shows what I want (except for page orientation)

\documentclass{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{tikz}
\usepackage{lipsum}
\begin{document}

\lipsum[1-4]

\begin{tikzpicture}[remember picture,overlay]
\node at (current page.south west) [text width=5cm,fill=red!20,above right] { This material is in the south west corner of the page };
\end{tikzpicture}

\end{document}


But when I try to get the same effect in landscape, the .south west anchor is apparently off the page. This seems to happen regardless of whether I pass landscape through the optional argument to the class declaration or through geometry.

Here's my malfunctioning MWE:

\documentclass{article}
\usepackage[margin=0.5in,landscape]{geometry}
\usepackage{tikz}
\usepackage{lipsum}
\begin{document}

\lipsum[1-4]

\begin{tikzpicture}[remember picture,overlay]
\node at (current page.south west) [text width=5cm,fill=red!20,above right] { This material is in the south west corner of the page };
\end{tikzpicture}

\end{document}


Any ideas?

Following @HarishKumar's suggestion, I can see that my compilation route is part of the problem: I'm compiling by: latexdvipsps2pdf instead of just pdflatex. However, I was under the impression that I need to go the more round about way if I'm incorporating PostScript commands in my LaTeX document. Does anyone know anything about this and care to explain why I should always use pdflatex?

-
You won't believe me. It works for both the cases (declaration in documentclass and through geometry) for me. But if you use \begin{landscape} and \end{landscape} (lscape or pdflscape packages), then it won't work. –  Harish Kumar Jan 9 '13 at 23:23
@HarishKumar. :( Maybe it's a difference of TeX versions? I'm using TeX Live 2012 and doing latex->dvips->ps2pdf route. –  A.Ellett Jan 9 '13 at 23:25
Sorry forgot to write about it. I use miktex 2.9 updated just now, and always go by pdflatex. –  Harish Kumar Jan 9 '13 at 23:26
@HarishKumar Could you make that an answer with an explanation of why my route is a bad choice? I hadn't thought about the route I took. pdflatex works fine. I originally started going the route I use because I often do things with pstricks and was under the impression that I had to go that route. –  A.Ellett Jan 9 '13 at 23:28
@A.Ellett I used to think that too :) Have a look at How to use PSTricks in pdfLaTeX? There's a few things that don't work as they should when going the latex=>dvips=>ps2pdf route –  cmhughes Jan 10 '13 at 0:23

I'm a bit hesitant about calling this a bug in pdflatex, but from my not-very-extensive investigations, it would appear that the position returned by the primitive \pdfsavepos is offset when in landscape mode and producing dvi. One way to explain the behaviour (though I've no idea how accurate this picture is) is that it is as if when \pdfsavepos runs then it actually computes the coordinates relative to the bottom left of the page. But TeX wants the coordinates relative to the top left so then pdflatex has to compute (0,paperheight) - (saved x, saved y) and return that via \pdfsavepos. However (still conjecturing) it's as if pdflatex when running in dvi mode does not know the actual page size of the shipped out page. So when it computes paperheight then it uses its "standard" paper height. This is based on the fact that the difference in the recorded positions in the aux file between a run with latex and a run with pdflatex is precisely \paperheight-\paperwidth:

pdflatex produces: \pgfsyspdfmark {pgfid1}{10467319}{17617993}

latex produces: \pgfsyspdfmark {pgfid1}{10467319}{29458709}

The difference is 11840716 (the units are sp).

And (in landscape mode) we can compare that with the paper dimensions via:

\newdimen\stuff
\stuff=11840716sp
\showthe\stuff

\stuff=\paperwidth
\showthe\stuff


then this produces:

> 180.67499pt.
l.9 \showthe\stuff

?
> 180.67499pt.
l.13 \showthe\stuff


This explanation can't be completely right because although I get a similar story (in that there is a consistent y-shift) when I try \usepackage[a5paper]{geometry} then the numbers don't add up in the same way.

For situations where one is going between two tikzpicture environments then this offset doesn't matter because it is consistent, and therefore the relative separation of the pictures is the same. It only matters for the current page node as that is "absolutely" positioned. To fix the current page, it suffices to fix where pgf thinks the page origin lies. This is held in the macro \pgf@sys@pdf@mark@pos@pgfpageorigin so we need to redefine that.

Ideally, we should redefine it to be aware of the current page size and geometry but as my experiments with a5paper showed me, I don't fully understand the computation involved. So here's the fix for landscape a4paper when set using the geometry package. If doing a mixed document, you could make the macro itself contain the conditional. By surrounding it in \ifpdf ... \fi we get a system that works for both latex and pdflatex.

\makeatletter
\ifpdf
\else
\ifGm@landscape
\def\pgf@sys@pdf@mark@pos@pgfpageorigin{\pgfqpoint{0sp}{11840716sp}}
\fi
\fi
\makeatother


\documentclass{article}
%\url{http://tex.stackexchange.com/q/91105/86}
\usepackage[landscape]{geometry}
\usepackage{tikz}
\usepackage{lipsum}

\makeatletter
\ifpdf
\else
\ifGm@landscape
\def\pgf@sys@pdf@mark@pos@pgfpageorigin{\pgfqpoint{0sp}{11840716sp}}
\fi
\fi
\makeatother

\begin{document}

\lipsum[1-4]

\begin{tikzpicture}[remember picture,overlay]
\foreach \anchor in {north west,north east,south west,south east} {
\node[anchor=\anchor] at (current page.\anchor) {some stuff at \anchor};
}
\end{tikzpicture}
\end{document}


With pdflatex this produces:

With latex; latex; dvips; pstopdf (note the double run of latex) this produces:

-