Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
1  
Your code is far from minimal. –  Martin Schröder Mar 17 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. –  Kevin Mar 19 at 17:18

2 Answers 2

up vote 10 down vote accepted

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.

share|improve this answer
    
Nice! How could we imagine that solution!? –  Sigur Mar 17 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{...}. –  Tom Mar 18 at 22:51
2  
@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 at 23:32
    
@GonzaloMedina Not speaking about the power of [RO,LE] :) –  tohecz Mar 19 at 18:44
    
@tohecz sure; but the space in the comment was too short :) –  Gonzalo Medina Mar 19 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{}
\fancyhead[C]{Page \thepage\ of \the\pdflastximagepages}
\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{}
\fancyhead[C]{Page \thepage\ of \pageref{mylastpage}}
\begin{document}
Some text.\newpage Some more text.
\label{mylastpage}
\end{document}

MWE, part 1 MWE, part 2

share|improve this answer
    
While this might be a valuable comment (I'm not sure, I haven't studied the question in details), I don't see how does this answer the question. –  tohecz Mar 17 at 21:24
    
@tohecz Oh, thank you for letting me know, I see, I would need to implement it in the footer for instance. I'll improve it soon. –  Malipivo Mar 17 at 23:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.