# Writing value of LastPage to external file

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:

\RequirePackage{lastpage}
\RequirePackage{refcount}

@write\tocfile{}{%
\number\numexpr\getpagerefnumber{LastPage}}


• 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.

\documentclass{article}
\usepackage{kantlipsum}
\usepackage[lastpage]{zref}

\newwrite\mytocfile
\immediate\openout\mytocfile=mytocfile.txt

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

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

\title{The Document Title}

\begin{document}
\maketitle

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

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

\pagenumbering{arabic}
\kant[1-10]
\pagenumbering{Roman}
\kant[11-20]

\end{document}
`