Conflict pageref with setspace package

pagerefsetspace

I have a document in which I would like to write each page number in the form (page number)/(total number of pages), for instance 1/2. I am using \label{LastPagePart01} at the end of the document, and \thepage/\pageref{LastPagePart01} at the bottom of each page. This works on every page, except on the last page, where there is only the page number.

I noticed that if I remove the \begin{spacing}{1.5} and \end{spacing} commands, then everything works well. I also noticed that if I add a \newpage command at the end of the document, just before the \end{spacing} command, the result is also good.

Does it mean that there is a conflict between the \pageref command and the setspace package?

Can anyone have any idea to solve this?
Thanks a lot!

\documentclass[12pt,a4paper,textsf]{article}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Préambule
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%BEGIN_FOLD % Packages % Filigrane % Mise en page et en-tête % Macros personnelles
%%%%%%%%%%%%%%%%%%%%%%
% Packages
%%%%%%%%%%%%%%%%%%%%%%
% Pour le français
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}

% Pour les en-têtes et la mise en page
\usepackage{fancyhdr}
\usepackage{setspace} %pour \begin{spacing}{...} \end{spacing}

%%%%%%%%%%%%%%%%%%%%%%
% Mise en page et en-tête
%%%%%%%%%%%%%%%%%%%%%%
% Dimensions du texte
\setlength{\parindent}{0cm}
\addtolength{\textwidth}{3cm}
\addtolength{\textheight}{0.5cm}
\addtolength{\voffset}{-2.5cm}
\addtolength{\hoffset}{-1.2cm}
\setlength{\headheight}{59.51pt}% hauteur de l'en-tête
\setlength{\headsep}{30pt}
\setlength{\footskip}{50pt}


\pagestyle{fancy}

\renewcommand{\headrulewidth}{0.pt}

%%%%%%%%%%%%%%%%%%%%%%
% Macros personnelles
%%%%%%%%%%%%%%%%%%%%%%




%END_FOLD
%%%%%%%%%%%%%%%%%%%%%%
% Commandes de titre (À ADAPTER)
%%%%%%%%%%%%%%%%%%%%%%
\author{Robin Pereboom}
\newcommand{\session}{mars 20XX}
\newcommand{\DureePartOne}{XX minutes}
\newcommand{\DureePartTwo}{XX minutes}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Début du document
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
    \begin{spacing}{1.5}
        
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        % Corps du document
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        % PREMIÈRE PARTIE - BEGIN
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        %BEGIN_FOLD % Page de garde première partie % Tableau dynamique
        \cfoot{\thepage/\pageref{LastPagePart01}}%page X/Y %pied de page centre
        %%%%%%%%%%%%%%%%%%%%%%
        % Page de garde première partie
        %%%%%%%%%%%%%%%%%%%%%%
        ~
        \vspace{1.\baselineskip}
        \begin{center}
            {\LARGE \textbf{Examen d'admission: session \session}}\\
            {\Large École de commerce de Fribourg et Bulle\\
                École de culture générale de Fribourg et Bulle}\\
            \vspace{2.\baselineskip}
            \uppercase{\textbf{\huge Mathématiques}}\\
            {\textbf{\LARGE Première partie, sans calculatrice}}\\
            \vspace{2.\baselineskip}

            
        \end{center}
    
        %***********************************************
        \newpage
        
        \part*{\LARGE{Première partie, sans calculatrice}}
        \emph{\Large{Durée: \DureePartOne.}}
        %END_FOLD
        
        %%%%%%%%%%%%%%%%%%%%%%
        % Corps de l'examen - première partie (À ADAPTER)
        %%%%%%%%%%%%%%%%%%%%%%
        
        \section{Exercice}
        \begin{enumerate}
            \item
            \item
        \end{enumerate}
        
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        % PREMIÈRE PARTIE - END
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        \label{LastPagePart01}
        
    \end{spacing}
\end{document}

Best Answer

The \cfoot command is a local setting and in this case the final page is being ejected after \end{spacing} and so it has been reverted by the end of the group. If you set it before the group starts then it is still in scope at the end

    \cfoot{\thepage/\pageref{LastPagePart01}}%page X/Y %pied de page centre
    \begin{spacing}{1.5}

enter image description here

It is not actually causing a problem here but your \label{LastPagePart01} is floating free in vertical mode so if a page break happens to come after the enumerate it may end up on the same page as the enumerate or it may end up at the top of the next page. So changing vertical spacing may accidentally push this over a page. so it's not really a conflict so much as the document markup is unsafe and changes in vertical spacing (or any change to the text) may cause the label to be separated. In general (possibly not here) figures floating to the end would also come after such a \label meansing it did not record the last page number.

Packages that provide total page counts (and recent versions of latex) do a hidden internal \label within the definition of \end{document} after any figures and other page breaking issues have been resolved and so you know you are on the last page.

In current releases you can use \PreviousTotalPages to get the total number of pages on the last run without setting a label explicitly.

Related Question