# “pgf” and “pgfmorepages” cause varioref to be off by one page

I am trying to place a frame around several pages in a document I am creating. The frame works fine, "varioref" gets the page number wrong.

\documentclass[10pt,letterpaper,notitlepage]{report}%
\usepackage[verbose=silent]{microtype} %Subliminal refinements towards typographical perfection%
\usepackage[letterpaper, left=0.50in, right=0.50in, top=0.50in, bottom=1.00in]{geometry} %add showframe'' to see the margins%
\usepackage{fontspec}
\usepackage{luatextra}
\defaultfontfeatures{Ligatures=TeX}
\usepackage{fontenc} % Allows the user to select font encodings%
\usepackage[dvipsnames,svgnames,x11names,table,hyperref,fixpdftex]{xcolor} % Driver independent access to several kinds of colors, tints, etcetera%
\usepackage[en-US]{datetime2}%
\usepackage[full]{textcomp} % Font families with sub-encoding%
\usepackage[inline]{enumitem} % Control layout of itemize, enumerate, description - use enumerate* for inline lists}%
\usepackage{anyfontsize} % Lets the user select any font size i.e. \fontsize {}{}\selectfont%
\usepackage{calc} % Simple arithmatic in latex commands%
\usepackage{fancyhdr} % Custom page headers and footers%
\usepackage{graphicx} % Enhanced support for graphics % Driver independent color extentions for latex and pdflatex%
\usepackage{lastpage} % If the hyperref package is used, the references are hyperlinked to their aims. If these hyperlinks shall be suppressed, \pageref*{...} instead of \pageref{...}can be used.%
\usepackage{luacolor}
\usepackage{pgf}
\usepackage{pgfmorepages}
\usepackage[nospace]{varioref} % This package defines the commands \vref, \vpageref, \vrefrange, and \vpagerefrange for LATEX2".%
\usepackage[hidelinks]{hyperref} % Must be loaded last. It extends the functionality of all the LATEX cross-referencing commands (including the table of contents, bibliographies etc) to produce \special commands which a driver can turn into hypertext links%

\pgfpagesdeclarelayout{boxed}
{
\edef\pgfpageoptionborder{0pt}
}
{
\pgfpagesphysicalpageoptions
{%
logical pages=1,%
}
\pgfpageslogicalpageoptions{1}
{
border code=\pgfsetlinewidth{2pt}\pgfstroke,%
border shrink=\pgfpageoptionborder,%
resized width=.95\pgfphysicalwidth,%
resized height=.95\pgfphysicalheight,%
center=\pgfpoint{.5\pgfphysicalwidth}{.5\pgfphysicalheight}%
}%
}

\pgfpagesuselayout{boxed}

\pagestyle{fancy} % set page style to fancy%
\fancyfoot{} % clear the footers%
\renewcommand{\footrulewidth}{2.0pt} % Create a separator line between the document and the footer 2.0pt wide%
\pagenumbering{arabic}%
\fancyfoot[r]{{\footnotesize \textbf{Page \thepage\ of~\pageref*{LastPage}}}}%%

\begin{document}
Start of document\vref{page 3}.
\cleardoublepage
this is page 2. \label{page 2}
\cleardoublepage
this is page three \label{page 3}
\end{document}


This produces: Start of document on page 4.

If I comment out "\pgfpagesuselayout{boxed}", the correct page number is shown. The last page number is correctly printed on each page either way.

I cannot find a way to correct this. Is it just an incompatibility between "varioref" and "pgf" and "pgfmorepages"?

-- Thanks!

Jerry

This has nothing to do with varioref. A normal \pageref would break too.

The problem is described in the pgf documentation, along with a suggested fix:

Another word of caution: using pgfpages will produce wrong page numbers in the .aux file. The reason is that TEX instantiates the page numbers when writing an .aux file only when the physical page is shipped out. Fortunately, this problem is easy to fix: First, typeset our file normally without using the \pgfpagesuselayout command (just put the comment marker % before it) Then, rerun TEX with the \pgfpagesuselayout command included and add the command \nofiles. This command ensures that the .aux file is not modified, which is exactly what you want.

But using pgfpages and so modifying the shipout only to get a frame is overkill. You can do this with eso-pic or with a current LaTeX with the shipout/background hook (adjust the 10pt to your liking):

\documentclass[10pt,letterpaper,notitlepage]{report}%

\usepackage{fancyhdr,lastpage}

\usepackage{tikz}

\pagestyle{fancy} % set page style to fancy%
\fancyfoot{} % clear the footers%
\renewcommand{\footrulewidth}{2.0pt} % Create a separator line between the document and the footer 2.0pt wide%
\pagenumbering{arabic}%
\fancyfoot[r]{{\footnotesize \textbf{Page \thepage\ of~\pageref*{LastPage}}}}%%