[Tex/LaTex] Setting page number with fancy without lastpage package

fancyhdrlastpagepage-numbering

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}

Best Answer

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.