# Cleveref's \cpageref{} command leads to “Missing number, treated as zero” error, when used in a document using (at least) two page numbering schemes

Update: I have updated this question & the corresponding code. The minimal working example code was overhauled after receiving some valuable suggestions from @Johannes_B regarding how to make it more concise. So, many thanks to him! :)

In my thesis I use \pagenumbering{arabic} (default page numbering scheme) for the Main matter and \pagenumbering{Roman} for the Supplementary materials. Both these parts include tables/figures/listings/equations etc. Also, I use cleveref to typeset cross-references and hyperref to link them.

On not more than 4-5 occasions, I had to collectively reference figures from the main matter as well as supplementary materials, within the main matter text. Naturally, I used the combination of \cref{} & \cpageref{} commands from cleveref, as usual.

The output of \cref{} is just fine, exactly as it should be. However, \cpageref{} fails, if it contains labels from both (arabic & Roman -numbered) parts of the thesis.

For instance, from the MWE given below, since \cpageref{} on line 47 contains only the main matter labels, it works! But, insert just one additional label from the supplementary material and it WILL FAIL! (on line 52) .. So,

\hspace*{\fill}THIS IS FINE! \textendash\ Pages: \cpageref{main,mainA}\hspace*{\fill}\\[4pt]
%......... ~on line 47~


... works, because main, mainA belong only to the main matter. But,

%\hspace*{-1pt}\hspace*{\fill}Now comes the Troublemaker \textendash\ Pages:
%\cpageref{main,mainA,supp}\hspace*{\fill}\\[4pt] %......... ~on line 52~


... fails, because main, mainA belong to the main matter (arabic) & supp belongs to the supplementary materials (Roman). However,

\hspace*{\fill}\cpageref{main,mainA} and \cpageref{supp}\hspace*{\fill} % see below!
%......... ~on line 32~


... works, because separate \cpageref{} commands were issued for labels from arabic & Roman parts. It shows that, there's otherwise no problem with \cpageref{}, if it receives a single argument at a time.

Corresponding *.log file for the given MWE, shows following error message:

! Missing number, treated as zero.
I
l.56     }
%                 % end fbox
A number should have been here; I inserted 0'.
(If you can't figure out why I needed to see a number,
look up weird error' in the index to The TeXbook.)


Please note that getting rid of the multiple page numbering is not an acceptable option to me. Also, could the solution to this problem be made compatible with hyperref as well?

Following is the minimal working example, in that, simply uncomment line 52 to generate the error!

MWE:

 \documentclass{report}
\usepackage[%
dvipsnames,
svgnames,
x11names,
table
]{xcolor}
\usepackage{blindtext}
\usepackage{capt-of}
\usepackage{hyperref}
\usepackage{cleveref}
\setlength{\fboxrule}{0.005pt}
\hypersetup{%
plainpages=false,
hypertexnames=true,
}%
%%%%%   %%%%%
% This code generates: Main matter (2 pages) + Supplementary matter (2 pages) %
%%%%%   %%%%%
\begin{document}
\setcounter{chapter}{1}
\chapter{Main Test Chapter}

{               % begin group
\Large%
\hspace*{\fill}%
\fbox{%               % begin fbox
\parbox[c][2cm][c]{0.95\textwidth}{%               % begin parbox
\noindent
\hspace*{\fill}\Cref{main,mainA,supp} given on following pages %
\textendash\hspace*{\fill}\\[8pt]
\hspace*{\fill}\cpageref{main,mainA} and \cpageref{supp}\hspace*{\fill} % see below!
}%                  % end parbox
}%                  % end fbox
\hspace*{\fill}%    % required empty line!

\vspace*{3ex}%
\hspace*{-12pt}\hspace*{\fill}\rule{1.5in}{0.01pt}\hspace*{\fill}%
\vspace*{3ex}%      % required empty line!

\noindent
\hspace*{4pt}
\hspace*{\fill}%
\fbox{%               % begin fbox
\parbox[c][2.4cm][c]{0.94\textwidth}{%               % begin parbox
%
\hspace*{\fill}THIS IS FINE! \textendash\ Pages: \cpageref{main,mainA}%
\hspace*{\fill}\\[4pt]
%
%%%%%                                                     %%%%%
%%%%%%%%% Uncomment following line to generate ERROR! %%%%%%%%%
%%%%%                                                     %%%%%
%\hspace*{-1pt}\hspace*{\fill}Now comes the Troublemaker \textendash\ Pages: %
%\cpageref{main,mainA,supp}\hspace*{\fill}\\[4pt]%,supp}
%
\hspace*{\fill}And now comes a Good Child \textendash\ Pages: \pageref{main}, %
\pageref{mainA}, \& \pageref{supp}.\hspace*{\fill}%
%
% Note: Above, \pageref{} was used, only to compare!
%
}%                  % end parbox
}%                  % end fbox
\hspace*{\fill}%
}               % end group
{               % begin group
\renewcommand{\figurename}{\Large Figure}
\vspace*{0.2in}
\captionof{figure}{\Large First SUB-FLOAT}
\label{mainA}

\vspace*{0.2in}
\blindtext[1]

\cleardoublepage

\vspace*{0.3in}
\captionof{figure}{\Large First SUB-FLOAT}
\label{main}

\vspace*{2in}
\blindtext[2]
}               % end group
\cleardoublepage
{               % begin group
\pagenumbering{Roman}
\setcounter{chapter}{0}
\setcounter{figure}{0}
\setcounter{table}{0}
\renewcommand{\thechapter}{\Roman{chapter}}
\renewcommand{\figurename}{\Large Supplemental Figure}

\chapter{Supplementary Test Chapter}

\vspace*{1in}
\captionof{figure}{\Large First SUB-FLOAT}
\label{supp}

\vspace*{1in}
\blindtext[5]
}               % end group
\cleardoublepage
\end{document}


Here's a snapshot of the first page (in "non-error-mode"!):

• This posting looks very much like a bug report/feature request. You should probably contact Toby Cubitt, the author and maintainer of the cleveref package, directly and bring up this issue. Until Toby provides an update, you're probably stuck with creating page references to items in the supplementary part of the document via \pageref. – Mico Jun 1 '15 at 5:02
• @Mico Hummm, that's really disappointing! I thought it must be something simple and solvable. Nonetheless, I've emailed Toby from the ID I found here: dr-qubit.org/latex.php#cleveref Let's see what happens! :) – Amar Jun 1 '15 at 14:14

TLDR; Page referencing in documents using multiple page numbering schemes should be fixed in the latest cleveref pre-release version (0.21) available from my web site: http://dr-qubit.org/latex.html

In case you're interested in the detail...

The "Missing number, treated as zero" error was caused by the cleveref sorting and compression code implicitly assuming that all page numbers are numeric. (At least any numbers that are cross-referenced using \cpageref etc. I guess the fact that no one had reported this glaring bug before is empirical evidence that very few people are cross-referencing pages in the front-matter of their documents.)

This compilation error is relatively easily fixed, by storing the numerical value of the page counter in the aux file, alongside what \thepage produces, and using that for sorting and compression. (Just as cleveref already does for labels.)

However, correctly sorting and compressing page references in documents with multiple page numbering schemes is harder. Although they have the same page counter value, we don't want "page i" and "page 1" compressed into a single reference. And, when sorting, "pages i-iii and 1-3" is preferable to "pages i, 1, ii, 2, iii and 3". Figuring out how to correctly group page references is tricky, because \thepage can be redefined to produce arbitrary output. For example, if one page numbering scheme labels pages as "first, second, third..." and another numbering scheme elsewhere in the same document labels pages as "primus, secundus, tertius...", how does cleveref determine which group to assign "quintus" to? TeX is Turing-complete, so in the worst case solving this in general requires solving the Halting problem!

The solution I've adopted is to group page references according to how \thepage formats a page counter value of 1, according to whatever \thepage macro is in operation where the \label is defined. This could potentially group together or compress pages from different parts of the document whose page numbering scheme happens to format page 1 identically. But that would make for a very confusing page numbering scheme! Short of solving the Halting problem in TeX, this seems a good heuristic.

To implement this, the latest pre-release version (0.21) of cleveref completely overhauls the page referencing logic to internally use the same code path as label referencing. The "type" of a page reference is set automatically to whatever \thepage produces for a page counter value of 1, as discussed above. It is not currently possible to override the page reference "type" manually (as one can for the label type, by passing an optional argument to \label). But if there is demand for this, it would not now be too difficult to add.

A final side-bonus of this is that page reference compression and sorting should now work correctly in documents that have per-chapter (or per-section) page numbering, as long they add the chapter number to the front of the typeset page numbers (or section number, of whatever unique prefix* is used to disambiguate page 1 in chapter 1 from page 1 in chapter 2). In particular, cleveref should now be compatible with the chappg package.

* If no such prefix is added, "page 1" in chapter 1 cannot be distinguished from "page 1" in chapter 2 anyway, so cleveref has no hope of sensibly sorting page references in this case. If you want to do this, you're on your own!

• Thank you so much @toby-cubitt for your in depth reply. I really appreciate it! – Amar Sep 6 '18 at 18:16