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 am using the following code to generate footer with fancy to show the page number

\documentclass[twoside,a4paper,10pt]{article}
\usepackage{lastpage}
\usepackage{fancyhdr}
\pagestyle{fancy}%
\renewcommand{\headrulewidth}{0.1pt}
\renewcommand{\footrulewidth}{0pt}

\renewcommand{\sectionmark}[1]{%
\markright{#1}{}}

\fancypagestyle{plain}{%
\fancyhf{}
\fancyfoot[R]{\thepage/\pageref{LastPage}}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}}
\fancyhf{}
\fancyhead[L]{\rightmark}
\fancyhead[R]{\leftmark}
\fancyfoot[R]{\thepage/\pageref{LastPage}}
\renewcommand{\headrulewidth}{0.1pt}
\renewcommand{\footrulewidth}{0pt}

This works in my local computer since I have the lastpage package installed. But I need to run LaTeX on the server where I don't have the lastpage and I don't have right to install the package. So is there any way to implement the same thing without the lastpage package?

Here I upload the complete code but I just find that if there is any \newpage command, it won't compile, is that any way to fix this?

\documentclass[twoside,a4paper,10pt]{article}
\usepackage{hyperref}
\usepackage{lastpage}
\usepackage{fancyhdr}

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

\renewcommand{\sectionmark}[1]{
\markright{#1}{}}

\fancypagestyle{plain}{
\fancyhf{}
\fancyfoot[R]{\thepage/\pageref{LastPage}}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}}
\fancyhf{}
\fancyhead[L]{\rightmark}
\fancyhead[R]{\leftmark}
\fancyfoot[R]{\thepage/\pagenum{LastPage}}
\renewcommand{\headrulewidth}{0.1pt}
\renewcommand{\footrulewidth}{0pt}

\begin{document}
  \title{XXX}
  \author{YYY}
  \maketitle

  This is first page 
  %\newpage  % it won't compile if \newpage was there

  This is second page
  %\newpage % if won't compile if \newpage was there

  This is last page

\end{document}
share|improve this question
    
Please notice that \markright takes only one argument and that the end-of-line after \renewcommand{\sectionmark}[1]{ can insert a space into the output. Either put it in one line or add % at the end of the first line (to eat spaces). –  tohecz Jan 21 '13 at 5:26
    
And the problem is probably your typo in \fancyfoot[R]{\thepage/\pagenum{LastPage}}. You have the line there twice and the second one contains \pagenum instead of \pageref. –  tohecz Jan 21 '13 at 5:27
    
Use \pageref instead of \pagenum... –  Werner Jan 21 '13 at 6:15
add comment

2 Answers

up vote 3 down vote accepted

You have a couple of options:

  1. Run latex on lastpage.dtx to create lastpage.sty. Keep this in your working folder on the server; there is no need to install it.

  2. Use the pageslts package which emulates (and extends) lastpage. Not only does it provide the LastPage reference, it also has VeryLastPage, in case of unprocessed floats \AtEndDocument.

  3. Manually insert a label at the end of your document:

    \label{LastPage}
    

    If there are unprocessed floats, insert the \label as part of the last float.

share|improve this answer
    
thanks, it helps. Now I can compile it in the local folder. But still have one more question, I just updated my top post. –  user1285419 Jan 21 '13 at 5:20
add comment

You can try something like putting a page reference on the last page

\label{mylastpage}

After compiling twice, you should be able to get the right page.

I believe that there might be some circumstances where this might give you the wrong page number. I'll look into it some more.

share|improve this answer
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.