LastPage 未定義 - 無法取得「第 x 頁,共 y 頁」 - 即使經過多次編譯

LastPage 未定義 - 無法取得「第 x 頁,共 y 頁」 - 即使經過多次編譯

這是我的 MWE:

\documentclass[11pt,letterpaper,oneside,notitlepage]{article}

\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{natbib}
\usepackage{graphicx}
\usepackage[loose]{units}
\usepackage{url}
\usepackage{pgf,tikz}
\usepackage{fancyhdr}
\usepackage{lastpage}
\usepackage{multirow}
\usepackage{dcolumn}
\newcolumntype{.}{D{.}{.}{-1}}
\usepackage{booktabs}
\usepackage{alltt}
\usepackage[font=small,format=plain,labelfont=bf,up,textfont=it,up]{caption}
\usepackage[margin=1in]{geometry}


% these commands use the fancyhdr package to get "x of y" style
% page numbering.  The headrulewidth command gets rid of a decorative 
% horizontal rule that is default with "fancy" pagestyle.
\pagestyle{fancy}
\cfoot{\thepage\ of \pageref{LastPage}}
\renewcommand{\headrulewidth}{0pt}


\begin{document}

\cfoot{{Cyclic triaxial test report  \ \ \ \ \ \ \   Page \thepage\ of \pageref{{LastPage}} \ \ \ \ \ \ \ Test ID: {testid} \\ {{\bf PRELIMINARY REPORT - NOT FOR ENGINEERING USE!}}}}

\subsection*{1}
\subsection*{2}
\subsection*{3}
\clearpage 
\subsection*{a}
\clearpage 
\subsection*{b}
\clearpage 
\subsection*{c}
\clearpage 
\subsection*{d}
\clearpage 
\clearpage 
\end{document}

有人可以提供建議嗎?正如標題所說,即使經過多次編譯,我也無法開始LastPage工作。我在用著pdfLaTeX

答案1

\pageref抑制;參數中多餘的一對大括號你有

\pageref{{LastPage}}

應該是

\pageref{LastPage}

額外的一對大括號會導致 LaTeX 查找錯誤的字串{LastPage}來產生交叉引用(正確的字串是LastPage)。您的程式碼會產生一些關於此的警告:

LaTeX Warning: Reference `{LastPage}' on page 1 undefined on input line 35.

之前解釋過的經過一些修改的程式碼:

\documentclass[11pt,letterpaper,oneside,notitlepage]{article}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{natbib}
\usepackage{graphicx}
\usepackage[loose]{units}
\usepackage{url}
\usepackage{pgf,tikz}
\usepackage{fancyhdr}
\usepackage{lastpage}
\usepackage{multirow}
\usepackage{dcolumn}
\newcolumntype{.}{D{.}{.}{-1}}
\usepackage{booktabs}
\usepackage{alltt}
\usepackage[font=small,format=plain,labelfont=bf,up,textfont=it,up]{caption}
\usepackage[margin=1in]{geometry}


% these commands use the fancyhdr package to get "x of y" style
% page numbering.  The headrulewidth command gets rid of a decorative 
% horizontal rule that is default with "fancy" pagestyle.
\pagestyle{fancy}
\fancyfoot[C]{\thepage\ of \pageref{LastPage}}
\renewcommand{\headrulewidth}{0pt}

\begin{document}

\fancyfoot[C]{{Cyclic triaxial test report\qquad Page \thepage\ of \pageref{LastPage}\qquad  Test ID: {testid} \\ {\bfseries PRELIMINARY REPORT -- NOT FOR ENGINEERING USE!}}}

\subsection*{1}
\subsection*{2}
\subsection*{3}
\clearpage 
\subsection*{a}
\clearpage 
\subsection*{b}
\clearpage 
\subsection*{c}
\clearpage 
\subsection*{d}AAA
\clearpage 
\clearpage 
\end{document}

在此輸入影像描述

我換成\cfoot{...}了最現代的\fancyfoot[C]{...}interfaz。我還固定了headheight長度;您的 MWE 發出警告

Package Fancyhdr Warning: `\headheight` is too small (`12.0pt`): Make it
at least `13.59999pt`.

所以我geometry按照訊息的建議使用設定來增加長度。另請注意,這\bf是一個舊的 TeX 命令,不應在現代文件中使用;你應該使用\bfseries

也許您應該考慮其他方法來獲得頁腳中的間距;您可以使用\qquad(就像我在範例程式碼中所做的那樣)或\hspace{<length>}取代所有這些單一空格。

答案2

我最近針對這種情況測試了一個版本\pdfximage\pdflastximagepageslualatex我不推薦它用於生產中的這個特定問題。如果產生的 PDF 文件為空或損壞,且未事先刪除該(空)PDF 文件,則本範例將停止下一次執行 TeX。

編輯:其次,並非所有書籍都從第 1 頁開始,\pdfximage命令可以在文件的後面部分使用,因此此範例僅用於演示,還需要改進。

%! lualatex example.tex
\documentclass[a4paper]{article}
\def\myfile{example.pdf}
\IfFileExists{\myfile}  % Is document typeset for the first time?
  {\pdfximage{\myfile}} % Positive response...
  {\relax}              % Negative response...
\usepackage{fancyhdr}
\pagestyle{fancy}\fancyhf{}
\fancyhead[C]{Page \thepage\ of \the\pdflastximagepages}
\begin{document}
Some text.\par\newpage Some more text.
\end{document}

很抱歉,我傾向於透過避免使用該lastpage套件來解決問題,而不是回答問題。原因是我們經常需要在最後一頁之前進行引用,例如當省略版權頁或空白頁甚至許多頁時,例如當書籍包含附錄、註釋頁、帶有廣告的頁等時。 ,其中我可以將其放在\label{mylastpage}文件中的任何位置。

\documentclass[a4paper]{article}
\usepackage{fancyhdr}
\pagestyle{fancy}\fancyhf{}
\fancyhead[C]{Page \thepage\ of \pageref{mylastpage}}
\begin{document}
Some text.\newpage Some more text.
\label{mylastpage}
\end{document}

MWE,第 1 部分 MWE,第 2 部分

相關內容