5

Currently using:

\edef\lastpagenumber{\pagenumber}

after starting a new page, then in subsequent text:

\ifnum\pagenumber>\lastpagenumber
  do something
\else
  do something else
\fi

But sometimes \pagenumber does not seem to increment, even though a new page has started.

egreg
  • 1,121,712
  • 2
    You can not test in that way (which format is this? latex and plain do not define \pagenumber) teX typesets complete paragraphs (including expanding all macros before considering page breaks. Page numbers are not reliably known until the output routine. (search for questions about that for more info) – David Carlisle Apr 19 '12 at 13:35
  • You need to set a label at both positions and then test if their page numbers are different. This needs two compiler runs. See my answer to Test if a paragraph has a page break in it?, which might even be a duplicate. – Martin Scharrer Apr 19 '12 at 13:40
  • You could also have a look at the source code of the varioref package which uses some code like you want. – Martin Scharrer Apr 19 '12 at 13:44
  • The format is ConTeXt, which apparently defines the \pagenumber macro. Apologies if the post is a duplicate, though the referenced post is for LaTeX. Not sure how to use LaTeX packages in ConTeXt. – Ray LeClair Apr 19 '12 at 14:00
  • What exactly do you want to achieve with this code? – Aditya Apr 19 '12 at 17:43
  • I am using an inline generated METAPOST figure as a background for text added to the text box. When sufficient content has been added to the text box that a new page starts, I want to reset the background to be empty. I am not manually editing the ConTeXt input file, but instead, generating the ConTeXt input file on the fly (using Python). So I need the code in the ConTeXt input file to detect the new page so that the background can be reset. – Ray LeClair Apr 20 '12 at 12:25

1 Answers1

3

Check out the perpage package. If you create a dummy counter

\usepackage{perpage}
\newcounter{mydummy}
\MakePerPage{mydummy}

then right after every

\stepcounter{mydummy}

the macro \theperpage will expand to the current page number at the point in the text where the counter has been stepped. Not necessarily right away: you might get a warning "references may have changed", requiring you to rerun LaTeX.

user9588
  • 2,201