3

I need to know when I change pages. I thought I had a solution using everypage. It works using \newpage, but not using \vspace.

\documentclass{article}
\usepackage{everypage}
\usepackage{tikzpagenodes}

\newif\ifnewpage
\newpagefalse
\AddEverypageHook{NEWPAGE\global\newpagetrue}

\newcommand{\pagetest}{\noindent
\ifnewpage{ It worked!}\else{ What's going on?}\fi
\begin{tikzpicture}[remember picture,overlay]
\ifnewpage\node[above right]{ Okay};
%\global\newpagefalse
\else\node[above right]{Still not found.};
\fi
\pgfextracty{\offset}{\pgfpointanchor{current page text area}{north}}
\node[below]{distance to top of page = \the\offset};
\end{tikzpicture}
\global\newpagefalse}

\begin{document}
\pagetest
\newpage
\pagetest
\vspace{60\baselineskip}

\pagetest
\end{document}

Based on the first page, the hook runs after tikz (or perhaps tikz saves the registers when it is called to use later}. Note that either \newpagefalse will do.

lockstep
  • 250,273
John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • 1
    Do you want to draw something on everypage? – yannisl Aug 12 '14 at 17:16
  • The way you have structured your MWE, do you realize that your \pagetest (even if it works the way you would like), will merely tell you whether it is the first invocation of \pagetest on the given page? Maybe that is what you mean by "testing for a new page", but I find the two descriptions quite distinct in meaning. – Steven B. Segletes Aug 12 '14 at 17:31
  • @Steven B. Segletes - Yes, I keep an internal record of how much space I have used and need to reset it on a new page. BTW, I just discovered that Tikz is writing what appears to be origin locations on the aux file. It would still have to wait until the page is done to write them. – John Kormylo Aug 12 '14 at 17:45

1 Answers1

2

It turns out that tikz uses aux to compute the page number already.

\documentclass{article}
\usepackage{tikzpagenodes}

\makeatletter
\let\tikzpage=\oddpage@page
\makeatother

\newcommand{\pagetest}{\noindent
\begin{tikzpicture}[remember picture,overlay]
\node[right]{current page = \tikzpage{} which may not equal \thepage};
\end{tikzpicture}
}

\begin{document}
\pagetest
\vspace{60\baselineskip}

\pagetest
\end{document}

page 2

All one has to do is compare \tikzpage with another counter. Note, the value is computed inside tikz and may not be accurate anywhere else.

The actual application this is used for is Alignment of text in two columns: forcing empty lines in one, to avoid collisions in the other

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • At this date, it appears tikz no longer uses the ifoddpage package, nor does it store anything in the aux file except the pgfid, and origin location. You will need to call \checkoddpage yourself. – John Kormylo Apr 22 '20 at 04:34