Example: Modifying the current page node

Published 2008-10-13 | Author: Kjell Magne Fauske

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.south east 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 to 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.

Download as: [PDF] [TEX]  •  [Open in Overleaf]

Modifying the current page node

Do you have a question regarding this example, TikZ or LaTeX in general? Just ask in the LaTeX Forum.
Oder frag auf Deutsch auf TeXwelt.de. En français: TeXnique.fr.

\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}

Comments

  • #1 Thiago de Melo, October 23, 2008 at 2:50 a.m.

    "The anchors current page.north west and current page.north west" ... at the top of the page is duplicated. It is south.west

  • #2 Kjell Magne Fauske, October 23, 2008 at 7:51 a.m.

    Thanske Thiago de Melo for pointing this out. I have now fixed the typo.

  • #3 Terrell, November 30, 2012 at 8:24 p.m.

    Hello, Neat post. There is usually a problem together with your website in web explorer, might test this? IE still is that the market leader and a first-rate section of people will leave out your fantastic writing due to this question.

Adding comments is currently not enabled.