Setting page number with fancy without lastpage package

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{\footrulewidth}{0pt}

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

\fancypagestyle{plain}{%
\fancyhf{}
\fancyfoot[R]{\thepage/\pageref{LastPage}}
\renewcommand{\footrulewidth}{0pt}}
\fancyhf{}
\fancyfoot[R]{\thepage/\pageref{LastPage}}
\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{\footrulewidth}{0pt}

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

\fancypagestyle{plain}{
\fancyhf{}
\fancyfoot[R]{\thepage/\pageref{LastPage}}
\renewcommand{\footrulewidth}{0pt}}
\fancyhf{}
\fancyfoot[R]{\thepage/\pagenum{LastPage}}
\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}

-
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). – yo' 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. – yo' Jan 21 '13 at 5:27
Use \pageref instead of \pagenum... – Werner Jan 21 '13 at 6:15

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.

-
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

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.

-