[Tex/LaTex] LastPage not defined – can’t get “page x of y” – even after multiple compiles

countersheader-footerlastpage

Here is my 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}

Can anyone offer advice? As the title says, even after multiple compiles, I can't get LastPage to work. I'm using pdfLaTeX.

Best Answer

Suppress the extra pair of braces in the argument of \pageref; you have

\pageref{{LastPage}}

and it should be

\pageref{LastPage}

The extra pair of braces causes LaTeX to look for the wrong string {LastPage} to produce the cross-reference (the right string is LastPage). Your code produces some warnings about this:

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

The code with some modifications explained before:

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

enter image description here

I changed \cfoot{...} to the most modern \fancyfoot[C]{...} interfaz. I also fixed the headheight length; your MWE produces a warning

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

so I used the geometry settings to increase the length as suggested by he message. Notice also thet \bf is an old TeX command that shouldn't be used in modern documents; you should use \bfseries instead,

Perhaps you should conside other method for getting your spacing in the footer; you can use \qquad (as I did in my example code) or \hspace{<length>} instead of all those single spaces together.