2

When writing reports it is common to have a page of meta-information at the start, written by, checked by, client, date etc. One of the fields I need is number of pages. Since I'm going to be automating the output of these reports I wondered whether there was a way of getting this from latex and then inputting it on the next compile? Something like Number of pages: \pdflength

thosphor
  • 1,193
  • 1
  • 12
  • 24

3 Answers3

5

Package zref, module totpages provides macro \ztotpages that contains the number of pages (regardless how the pages are numbered).

\documentclass{article}
\usepackage{zref-totpages}

\begin{document} Total pages: \ztotpages \end{document}

Internally it works by writing a zref label to the .aux file. If the label is not yet present in the .aux file, \ztotpages expands to 0.

Comparison last page vs. total pages

\documentclass{book}
\usepackage{blindtext}
\usepackage{zref-totpages}
\usepackage{lastpage}

\begin{document} \frontmatter

\begin{tabular}{ll} Total pages: & \ztotpages \ Last page: & \pageref{LastPage} \ \end{tabular}

\newpage \tableofcontents \mainmatter \blinddocument \end{document}

The document contains four roman-numbered front matter pages and four main matter pages. Result:

Total pages: 8
Last page:   4
Heiko Oberdiek
  • 271,626
  • Great answer. I made a link from https://tex.stackexchange.com/questions/227/how-can-i-add-page-of-on-my-document to here, so that it doesn't get lost. – Keks Dose Jun 01 '22 at 11:27
2

In OpTeX, there is the macro \lastpage. You can try:

\fontfam[lm]

Number of pages: \lastpage.

\bye

The \lastpage expands to ? in the first run of TeX and terminal says:

WARNING l.5: Try to rerun, document.ref file was created.

When you run TeX again, the \lastpage expands to the number of pages in the document. If this number is changed, then terminal says:

WARNING l.9: Try to rerun, document.ref file was changed.

When you run TeX again, the \lastpage is correct and no warning is printed on the terminal and log file.

wipet
  • 74,238
0

You can try \usepackage{lastpage} and \pageref{LastPage}. Here is a MWE:

enter image description here

\documentclass[a4paper]{article}

\usepackage{lastpage} \usepackage{hyperref} \hypersetup{hidelinks}

\usepackage{lipsum}

\title{Title \ \large Subtitle} \author{Author} \date{31.05.2022}

\begin{document}

\maketitle \begin{center} Total pages: \pageref{LastPage} \end{center}

\newpage

\lipsum

\end{document}

Vebjorn
  • 1,778