# LastPage not defined - can't get “page x of y” - even after multiple compiles

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

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

• Your code is far from minimal. – Martin Schröder Mar 17 '14 at 17:12
• Very sorry for offending you. I assumed my problem may have stemmed from a package conflict, so I left those in. The subsections and clearpages retain the page structure of my document, which is pertinent to the issue of footers and pagecounts. – nivek Mar 19 '14 at 17:18

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

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


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.

• Nice! How could we imagine that solution!? – Sigur Mar 17 '14 at 16:55
• This is a bit of a tangent, but can you elaborate about the advantages of \fancyfoot[C]{...}? I haven't noticed a difference between it and \cfoot{...}. – twsh Mar 18 '14 at 22:51
• @Tom the author of fancyhdr implemented \fancyhead, \fancyfoot since version 1.99 (1996) and declared "more or less deprecated" the initial family \chead, \lhead, etc (although they still can be used and the documentation itself uses them). \fancyhead, \fancyfoot have a more convenient syntax: for example, in twoside mode, using \lhead[a]{b} it's not immediately clear which ("a" or "b") will go on even and which on odd pages; on the other hand, \fancyhead[LE]{...}, \fancyhead[LO]{...} clearly shows what goes where. The implementation is also better. – Gonzalo Medina Mar 18 '14 at 23:32
• @GonzaloMedina Not speaking about the power of [RO,LE] :) – yo' Mar 19 '14 at 18:44
• @tohecz sure; but the space in the comment was too short :) – Gonzalo Medina Mar 19 '14 at 19:02

I've recently tested a version with \pdfximage and \pdflastximagepages via lualatex for this situation, but I am not recommending it for this particular problem in production. If a generated PDF file is empty or broken, this example stops the next run of TeX if that (empty) PDF file is not deleted in advance.

Edit: Next to it, not all books start at page 1 and \pdfximage command could be used later in the document, so this example is for demonstration only, it would need improvements.

%! 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{}
\begin{document}
Some text.\par\newpage Some more text.
\end{document}


I am sorry I tend to solve the problem by avoiding the use of the lastpage package instead of answering the question. The reason is that we often need before the last page to refer to, e.g. when omitting the Colophon or an empty page or even many pages, e.g. when book contains Appendixes, pages for notes, pages with advertisements etc. This is my common solution where I can put \label{mylastpage} anywhere in the document.

\documentclass[a4paper]{article}
\usepackage{fancyhdr}
\pagestyle{fancy}\fancyhf{}
`