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]{...}インターフェイスに変更しました。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最近、この状況に対してと\pdflastximagepagesを使用したバージョンをテストしましたlualatexが、実稼働環境でのこの特定の問題には推奨しません。生成された 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

関連情報