2

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}}

Thx for the answers.

AlexG
  • 54,894
m42
  • 21
  • 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
  • @m42 : Ok, I see. See my edited answer, you will have to open some file you want to write to. – AlexG Jan 29 '16 at 13:30
  • AlexG Somehow I cannot get it running with your revised version. Providing a MWE is tedious, so I will live for now with the refcount solution until it works with your zref suggestion. (My current problem is that writing to the file happens in \maketitle and this doesn't work well with \AtBeginDocument. – m42 Jan 29 '16 at 13:42
  • So you try to modify the \maketitle command of an existing class file? – AlexG Jan 29 '16 at 13:49
  • \makefile happens after \begin{document}. Therefore, you don't need \AtBeginDocument if you modify \maketitle. See my edit. – AlexG Jan 29 '16 at 14:00
  • @AlexG Thanks! I now got it running. As you said, I removed AtBeginDocument. In my case I need to use \protected@write instead of \immediate\write due to the use of several other variables I need to expand. – m42 Jan 29 '16 at 14:10

1 Answers1

2

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}
AlexG
  • 54,894