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.

I need add the numbers of the first and last page to the header of the first page. To make it look like this:

Document title, date, pp. 1 - 30

The number of the first page is \thepage, because are in the header of itself.

I'm using fancyhdr to make the headers. How can I do it preferably without using the lastpage package?

If the document is twosided with \cleradoublepage, the lastpage package doesn't work well if I add \AtEndDocument{Some text} in the preamble. Here an MWE:

\documentclass[twoside]{article}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{lastpage}% http://ctan.org/pkg/lastpage
\usepackage{fancyhdr}% http://ctan.org/pkg/fancyhdr
\fancyhf{}% Clear header/footer
\fancyhead[C]{Document title, \today, \textbf{pp.\ \pageref{FirstPage}~-~\pageref{LastPage}}}
\AtBeginDocument{\label{FirstPage}}%
\AtEndDocument{Some text that I need put at the end of text. \cleardoublepage}
\pagestyle{fancy}
\begin{document}
   \section{First section}\lipsum[1]
\end{document}
share|improve this question
add comment

1 Answer

up vote 6 down vote accepted

The following does exactly what you're after:

enter image description here

\documentclass{article}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{lastpage}% http://ctan.org/pkg/lastpage
\usepackage{fancyhdr}% http://ctan.org/pkg/fancyhdr
\fancyhf{}% Clear header/footer
\fancyhead[C]{Document title, \today, \textbf{pp.\ \pageref{FirstPage}~-~\pageref{LastPage}}}
\AtBeginDocument{\label{FirstPage}}%
\pagestyle{fancy}
\begin{document}
\section{First section}\lipsum[1-20]
\section{Second section}\lipsum[21-40]
\section{Last section}\lipsum[41-60]
\end{document}

The first page label FirstPage is set using \AtBeginDocument, while the last page label is obtain via the lastpage package as LastPage. A label for the first page is required if you plan on setting a different first page counter. If you don't want to use the lastpage package, you can use

\AtEndDocument{\label{LastPage}}

which should work in most cases. The cases where this might not work include end-of-document unprocessed floats, for example. The pageslts package provides a work-around in such cases by provided access to a VeryLastPage label:

enter image description here

\documentclass[twoside]{article}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{pageslts}% http://ctan.org/pkg/pageslts
\usepackage{fancyhdr}% http://ctan.org/pkg/fancyhdr
\fancyhf{}% Clear header/footer
\fancyhead[C]{Document title, \today, \textbf{pp.\ \pageref{FirstPage}~-~\pageref{VeryLastPage}}}
\AtBeginDocument{\label{FirstPage}}%
\AtEndDocument{Some text that I need put at the end of text. \cleardoublepage}
\pagestyle{fancy}
\begin{document}
   \section{First section}\lipsum[1]
\end{document}

The rest of the header/footer is changeable as usual.

share|improve this answer
    
Thanks! Werner. But doesn't work well if I add \AtEndDocument{Some text}, and in my document I have something as that. –  OSjerick May 22 '12 at 0:40
    
My document is a two sided article with \cleardoublepage. –  OSjerick May 22 '12 at 0:42
1  
@osjerick: \AtEndDocument can be used multiple times - it doesn't get overwritten. With the additional information, please update your original question with a full, compilable, minimal example that shows your current setup. –  Werner May 22 '12 at 0:43
    
@GonzaloMedina: Thanks. I've added your good suggestion. –  Werner May 22 '12 at 5:45
1  
@osjerick: I've updated my answer by means of using the pageslts package. It provides the label VeryLastPage that should take care of your current concerns. –  Werner May 22 '12 at 5:46
add comment

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.