# Is memoir's page layout incompatible with TikZ?

Can someone explain what's going on here?:

\documentclass[showtrims,svgnames]{memoir}
\usepackage{lipsum}
\settrimmedsize{210mm}{148mm}{*}
\setstocksize{236mm}{164mm}
\settrims{5mm}{5mm}
\setulmarginsandblock{20mm}{22mm}{*}
\setlrmarginsandblock{20mm}{30mm}{*}
\checkandfixthelayout
\trimLmarks
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[remember picture, overlay]
\draw[red]  (current page.south west) rectangle
(current page.north east);
\end{tikzpicture}
\lipsum[1-4]
\end{document}


renders (in XeLateX) as shown below.

I should like better to understand how TikZ defines/establishes the placing of the "current page" node. I can see that the south west corner is aligned with the stock, but it looks like the north west corner has been calculated from the wrong dimensions.

Here's page 1 of the PDF output:

-
The red box seems to have the same dimensions as the trimmed size, but not the correct offset. –  Michael Palmer Feb 14 '12 at 20:14
@MichaelPalmer: Yes, it appears that the dimensions are those of the trimmed page, but the origin is relative to the stock. –  Brent.Longborough Feb 15 '12 at 9:56

## 1 Answer

Sizes are correct because TikZ/PGF uses \paperwidth and \paperheight for page dimensions, which memoir correctly sets without bleed. Origin however isn't calculated at all, a {0pt}{0pt} is hardcoded in pgfmoduleshapes, where current page is created. You may redefine that, but I suggest creating a new bounding box called memoir page, which will cut the trim but also take odd and even pages into account. The code is below and works for me in pdflatex and xelatex with one catch - the command has to be repeated twice (hardly anything extraordinary for LaTeX).

\documentclass[showtrims,svgnames]{memoir}
\usepackage{lipsum}
\settrimmedsize{210mm}{148mm}{*}
\setstocksize{236mm}{164mm}
\settrims{5mm}{5mm}
\setulmarginsandblock{20mm}{22mm}{*}
\setlrmarginsandblock{20mm}{30mm}{*}
\checkandfixthelayout
\trimLmarks
\usepackage{tikz}

% this is the added code. Use \calculatetrims when you change page geometry
% You also have to use "memoir page" instead of "current page" in tikz
\usepackage{calc}
\makeatletter
\newlength\memoirleftodd \newlength\memoirrightodd
\newlength\memoirlefteven \newlength\memoirrighteven
\newlength\memoirtop \newlength\memoirbottom
\def\calculatetrims{
\setlength\memoirbottom{\stockheight-\paperheight-\trimtop}
\setlength\memoirleftodd{\stockwidth-\paperwidth-\trimedge}
\setlength\memoirlefteven{\trimedge}
\setlength\memoirtop{\memoirbottom+\paperheight}
\setlength\memoirrightodd{\memoirleftodd+\paperwidth}
\setlength\memoirrighteven{\memoirlefteven+\paperwidth}}
\expandafter\def\csname pgf@sh@ns@memoir page\endcsname{rectangle}
\expandafter\def\csname pgf@sh@np@memoir page\endcsname{%
\def\southwest{\checkoddpage\pgfqpoint{
\ifoddpage\memoirleftodd\else\memoirlefteven\fi}{\memoirbottom}}%
\def\northeast{\checkoddpage\pgfqpoint{
\ifoddpage\memoirrightodd\else\memoirrighteven\fi}{\memoirtop}}}
\expandafter\def\csname pgf@sh@nt@memoir page\endcsname{{1}{0}{0}{1}{0pt}{0pt}}
\expandafter\def\csname pgf@sh@pi@memoir page\endcsname{pgfpageorigin}
% (Brent's additional code goes here)
\makeatother

\begin{document}

\calculatetrims    % You must write this before first overlay picture

\begin{tikzpicture}[remember picture, overlay, line width=3pt]
\draw[red] (memoir page.south west) rectangle (memoir page.north east);
\end{tikzpicture}
\lipsum[1-4]
\begin{tikzpicture}[remember picture, overlay, line width=3pt]
\draw[red] (memoir page.south west) rectangle (memoir page.north east);
\end{tikzpicture}
\end{document}


If anyone needs to do this with regard to the stock, here's a suggestion, which can be substituted for the above code, or added at the point shown:

\expandafter\def\csname pgf@sh@ns@memoir stock\endcsname{rectangle}
\expandafter\def\csname pgf@sh@np@memoir stock\endcsname{%
\def\southwest{\pgfqpoint{0pt}{0pt}}%
\def\northeast{\pgfqpoint{\stockwidth}{\stockheight}}%
}
\expandafter\def\csname pgf@sh@nt@memoir stock\endcsname{{1}{0}{0}{1}{0pt}{0pt}}
\expandafter\def\csname pgf@sh@pi@memoir stock\endcsname{pgfpageorigin}


(Don't forget this needs to be inside a \makeatletter/\makeatother pair.)

-
Thank you. That's brilliant. The reason you need it twice is that, for my MWE, I left out the atbegshi stuff to put it on every page. So in the MWE, it's needed once per page. I've also taken the liberty of adding to your answer, for the (simpler) case where one needs to bleed into the stock. –  Brent.Longborough Feb 17 '12 at 13:09
Glad I could help. TikZ is by far the most impressive package I have discovered recently. –  Frg Feb 17 '12 at 17:35
I was about to make a question pointing to the same issue. I think this would be worth adding as a contribution to the bundle of TikZ/PGF packages and, if possible, extend it to the package tikzpagenodes. –  Marcos Jan 7 at 7:45