[Tex/LaTex] PDF Latex lastpage does not work

cross-referencinglastpage

I've added the last page package tried to display the pager but the last page wont display.
What's wrong with my code, the last page will just display a "??":

{.select date_format(now()," %M %d, %Y %r") as  run_date}
{.select username from deskpad.user where user_id=@user_teller}

\documentclass{article}
\usepackage[paperheight=11in,paperwidth=8.5in,margin=0.1in,headheight=0.5in,footskip=0.4in,includehead,includefoot]{geometry}
\usepackage{graphicx}
\usepackage{fancyheadings}
\usepackage{fancybox}
\usepackage{longtable}
\usepackage{setspace}
\usepackage{textcomp}
\usepackage{colortbl}
\usepackage{booktabs}
\usepackage{fancyhdr}
\usepackage{lastpage}

\usepackage[absolute]{textpos}

\renewcommand{\familydefault}{\sfdefault}

\pagestyle{fancy}
\renewcommand{\headrulewidth}{0pt}

\fancyhf{}
\fancyhead[l]{
}
\fancyfoot[l]{
\small{
\begin{tabular}{p{4.0in} p{4.0in}}
\hspace{15pt} \small{ \emph{ Date Printed: {.\run_date\} } } & \raggedleft{ \small{ \emph{ Page \thepage\ of \pageref{Last Page} } } \hspace{15pt}} \tabularnewline
\end{tabular}
}
}

Best Answer

The correct key is \pageref{LastPage}, without spaces. Two LaTeX runs are necessary for getting the correct reference when the number of pages changes: on the first run the value is written in the .aux file and read back in in the following run.


Here's also a better way to achieve the same result for the footer, using the L and R fields without any guess about the widths.

Note that fancyheadings is obsolete and should not be loaded, fancyhdr is sufficient.

I omitted also the packages not necessary for the example; the kantlipsum package is just to provide the \kant command that produces mock text.

\documentclass{article}
\usepackage[
  paperheight=11in,
  paperwidth=8.5in,
  margin=0.1in,
  headheight=0.5in,
  footskip=0.4in,
  includehead,
  includefoot
]{geometry}
\usepackage{graphicx}
\usepackage{fancyhdr}
\usepackage{lastpage}

\usepackage{kantlipsum}

\pagestyle{fancy}
\renewcommand{\headrulewidth}{0pt}

\fancyhf{}
\fancyfoot[L]{%
  \small\hspace{15pt}\emph{Date Printed: \today}%
}
\fancyfoot[R]{%
  \small\emph{Page \thepage\ of \pageref{LastPage}}\hspace{15pt}%
}

\begin{document}

\kant[1-20]

\end{document}