How can I write the final value of LastPage (\pageref{LastPage}) to an external file (knowing that it needs 2-3 compiliations of the document)? (I need it to adapt a .cls file.)

[Update 2016-01-29, 13:30]

Found the following solution using the refcount package:



Thx for the answers.

  • Do you know how to write to external file? if yes just use \AtEndDocument{\immediate\write\mywrite{\thepage}} – touhami Jan 29 '16 at 12:00
  • @m42 I would not try to re-invent the wheel. Just use \zref@extract{LastPage}{abspage} where needed within your class file. – AlexG Jan 29 '16 at 12:40
  • @AlexG I gave it a try, but it didn't work in my setting. I tried @write\tocfile{}{\number\numexpr\zref@extract{LastPage}{abspage}} and got a "Missing number, treated as zero" error message – m42 Jan 29 '16 at 13:05
  • @m42: Don't write the LastPage information into some external file. That has been done for you already. Simply insert \zref@extract{LastPage}{abspage} at the place within your *.cls where you need this information. – AlexG Jan 29 '16 at 13:15
  • @AlexG Well, I need this in the *.cls file to write the LastPage value into an external file. I.e., part of functionality of the *.cls file is to create an external file with some content (used for other purposes). – m42 Jan 29 '16 at 13:18

The package zref, using the lastpage module is an option. The information about the page count is written to the *.aux file.

Since a class file is to be modified, the "Programmer's" interface of zref, \zref@extract{<reference name>}{<property name>} should be used to retrieve the information.

The *.aux file is read on subsequent compilation runs just before starting to typeset the document, making the information stored in the *.aux file available.

Custom auxiliary output files need to be opened first before they can be written to.



%overwrite the `\maketitle' definition of the class file
  \immediate\write\mytocfile{The last page number is: \zref@extract{LastPage}{page}.}%
  \immediate\write\mytocfile{The total page count is: \zref@extract{LastPage}{abspage}.}%

%%this my go into your cls file
%  \immediate\write\mytocfile{The last page number is: \zref@extract{LastPage}{page}.}%
%  \immediate\write\mytocfile{The total page count is: \zref@extract{LastPage}{abspage}.}%

\title{The Document Title}


The last page number is \makeatletter\zref@extract{LastPage}{page}.\makeatother

The absolute page count is \makeatletter\zref@extract{LastPage}{abspage}.\makeatother


| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.