[Tex/LaTex] Wrong total number of pages in page numbering

floatspage-numbering

My document is 39 pages long. However, the total page number (after numerous processing) is 36. I thus have page 1/36, 2/36, … 36/36, as well as 37/36, 38/36, and 39/36.

Page 36 matches with the end of my text: the end of page 36 and pages 37 to 39 are filled with (floating) tables only.
If I add enough content in order to have regular text displayed after all floating tables, the numbering becomes right, i.e., the total page number equals 39.

enter image description here

I have tried to add \null after the last floating table, but it has no effect.

The total number of pages is computed via the \@totpages variable that is defined as following in the .cls file I use. (This class is the template of the journal I want to publish in, based on the article class.)

\def\@totpages{0}
\AtEndDocument{\immediate\write\@auxout{\string\gdef\string\@totpages{\thepage}}}

Question: How to force the total of page number to be compute after the last float being printed?

Best Answer

Load the lastpage package and then redefine the footer or \@totpages to use \pageref{LastPage}.

The lastpage package tries very hard to get the correct last page number even if there are pending floats and so is much more reliable than most manual solutions.