Modifying the current page node

The special current page node allows you to draw on the current page using absolute coordinates. The anchors current page.north west and current page.north west are the top left and bottom right corners of the current physical page. So, if you want to put a nice border around the page you can simply write:

...
\usetikzlibrary{decorations.pathmorphing}
...
\tikz[remember picture,overlay] {%
    \draw[thick,red, decorate,decoration={snake}]
        (current page.north west) rectangle (current page.south east);
}

Unfortunately the current page node does not work properly if you want to print your document on stock paper that is larger than your page size. A common scenario is to print for instance a B5 page on an A4 page. This example shows you can modify the code for the current page node to take stock paper and recto and verso pages into account.

The example defines the \setpagenode macro. When called it will modify the current page node. If your page is centered on the stock paper you only need to calculate the node once. If your page is not centered the relevant page dimensions will be different depending on whether it is an odd or even page. This unfortunately requires a recalculation for every page that access the current page node. A quick hack save some typing is to force a recalculation every time the overlay style is used. Thanks to PGF's powerful pgfkeys library this is quite easy:

\pgfkeys{/tikz/overlay/.add code={}{\setpagenode}}

Note: This example is specific to the Memoir document class. For other document classes you have to consult the documentation for the appropriate page dimensions.


modifying-current-page-node

Edit and compile if you like:

\documentclass[showtrims]{memoir}


% Set stock size to A4
\setstocksize{297mm}{210mm}
% Set page size to B5
\settrimmedsize{250mm}{176mm}{*}
\isopage
\checkandfixthelayout

\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}

% Special current page bounding box rectangle that adapts to stock paper sizes
% and recto/verso pages
\newcommand\setpagenode{
    % The original current page node code can be found in the file
    % generic/pgf/modules/pgfmoduleshapes.code.tex
    \expandafter\def\csname pgf@sh@ns@current page\endcsname{rectangle}
    % Use a few low level Memoir macros to check if we are on an even or odd page.
    \strictpagecheck
    \checkoddpage
    \ifoddpage%
        \expandafter\def\csname pgf@sh@np@current page\endcsname{%
          % Set the current page.south west coordinate
          \def\southwest{\pgfpoint{\stockwidth-\paperwidth-\trimedge}%
                                  {\stockheight-\trimtop-\paperheight}}%
          % Set the current page.north east coordinate
          \def\northeast{\pgfpoint{\stockwidth-\trimedge}{\stockheight-\trimtop}}%
        }
    \else
        \expandafter\def\csname pgf@sh@np@current page\endcsname{%
          \def\southwest{\pgfpoint{\trimedge}{\stockheight-\trimtop-\paperheight}}%
          \def\northeast{\pgfpoint{\trimedge+\paperwidth}{\stockheight-\trimtop}}%
        }
    \fi
    \expandafter\def\csname pgf@sh@nt@current page\endcsname{{1}{0}{0}{1}{0pt}{0pt}}
    \expandafter\def\csname pgf@sh@pi@current page\endcsname{pgfpageorigin}
}

\begin{document}


% Force recalculation of the current page node whenever the overlay option is used.
% If you page is centered on your stock paper it is only necessary to calculate
% the current page rectangle once.
\pgfkeys{/tikz/overlay/.add code={}{\setpagenode}}

\HUGE Verso page

\tikz[remember picture,overlay] {%
    \draw[thick,red, decorate,decoration={snake}]
        (current page.north west) rectangle (current page.south east);
}


\newpage
\HUGE Recto page

\tikz[remember picture,overlay] {%
    \draw[thick,red, decorate,decoration={snake}]
        (current page.north west) rectangle (current page.south east);
}

\end{document}

Click to download: modifying-current-page-node.texmodifying-current-page-node.pdf
Open in Overleaf: modifying-current-page-node.tex