我如何将 LastPage (\pageref{LastPage})的最终值写入外部文件(知道它需要 2-3 次文档编译)?(我需要它来适应 .cls 文件。)
[更新 2016-01-29 13:30]
使用以下解决方案找到引用计数包裹:
\RequirePackage{lastpage}
\RequirePackage{refcount}
@write\tocfile{}{%
\number\numexpr\getpagerefnumber{LastPage}}
谢谢您的回答。
答案1
包zref
,使用lastpage
模块是一个选项。有关页数的信息将写入文件*.aux
。
zref
因为要修改类文件,所以应使用的“程序员”界面\zref@extract{<reference name>}{<property name>}
来检索信息。
*.aux
在开始排版文档之前,将在后续的编译运行中读取该文件,从而使存储在*.aux
文件中的信息可用。
自定义辅助输出文件需要先打开,然后才能写入。
\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}