如果要求精确获取最终排版后目录最后一个条目所在的页码,可以通过三轮编译,笨是笨了点,但是能达到目的。
- 常规生成
lof文件,同时计算目录所有条目的数量,写入aux文件 - 通过
\listoffigures命令加载lof文件中的\contentsline命令,对目录的每一项进行排版,当处理最后一个条目时,向aux文件延时写入条目所在的页码,这样写入的页码会是当前条目所在页需要被shipout时的准确页码 - 第2轮生成的
aux为此轮编译提供准确的页码
如果图片数量不多,只修改\l@figure,每处理一个条目就覆盖一次页码,也是可以的。
\documentclass{article}
\usepackage{graphicx}
\usepackage{lipsum}
\usepackage{showframe}
\usepackage{pgffor}
\newcommand{\figlistlastpage}{0}
\makeatletter
\newcounter{totalfigurecount}
\setcounter{totalfigurecount}{0}
\newcounter{curfigurecount}
\setcounter{curfigurecount}{0}
%第一轮计算条目总数
\let\oldaddcontentsline\addcontentsline
\renewcommand{\addcontentsline}[3]{%
\oldaddcontentsline{#1}{#2}{#3}%
\ifnum\pdfstrcmp{#1}{lof}=0
\stepcounter{totalfigurecount}
\fi
}
%第一轮写入条目总数
\AtEndDocument{
\if@filesw
\immediate\write\@auxout{\string\gdef\string\finalfigcount{\thetotalfigurecount}}%
\fi
}
%第二轮延迟写入最后一个条目的排版页码
\let\oldcontentsline\contentsline
\renewcommand{\contentsline}[4]{%
\oldcontentsline{#1}{#2}{#3}{#4}%
\ifnum\pdfstrcmp{#1}{figure}=0
\stepcounter{curfigurecount}
\ifnum\value{curfigurecount}=\finalfigcount\relax
\if@filesw
\protected@write\@auxout{}{
\string\gdef\string\figlistlastpage{\thepage}%
}
\fi
\fi
\fi
}
%\let\old@l@figure\l@figure
%\renewcommand{\l@figure}[2]{%
% \old@l@figure{#1}{#2}%
% % 每处理一个条目就会覆盖写入一次,最后留下的就是最后一页的页码
% \protected@write\@auxout{}{%
% \string\gdef\string\figlistlastpage{\thepage}%
% }
%}
\makeatother
\begin{document}
% ----- 这部分是为了模拟 图目录之前还有其他内容填充 的情形 ----- %
\lipsum[1-4]
This is Text. This is Text. This is Text. This is Text. This is Text. This is Text. This is Text. This is Text. This is Text. This is Text. This is Text. This is Text. This is Text. This is Text. This is Text. This is Text. This is Text. This is Text. This is Text. This is Text. This is Text. This is Text. This is Text. This is Text.
% =================================================== %
\listoffigures
\figlistlastpage
\foreach \n in {1, 2, ..., 24} {
\begin{figure}[ht]
\centering
\includegraphics[width=.48\linewidth]{example-image-a}
\caption{This is text}
\end{figure}
\begin{figure}[ht]
\centering
\includegraphics[width=.48\linewidth]{example-image-a}
\caption{This is text}
\end{figure}
\clearpage
}
%
%\begin{figure}[ht]
%\centering
%\includegraphics[width=.48\linewidth]{example-image-a}
%\caption{This is text}
%\end{figure}
\end{document}























问 如何使 \listoffigures 的盒子强制`shipped out`?