# How to clear footer for the last page?

In the preamble of an exam I am creating, I currently have:

%headers, footers
\usepackage{fancyhdr}
\pagestyle{fancyplain} %header
\fancyhf{} % sets both header and footer to nothing
\renewcommand{\headrulewidth}{0pt} % your new footer definitions here
\rfoot{\large \bf{GO ON TO NEXT PAGE}}


Of course, I'd like for "GO ON TO THE NEXT PAGE" to be removed on the last page. Is there any way I can do this?

Edit/Update: By the way, at the end, I ended up approaching this problem differently than what the answers below provided because the document went through many changes in the last day or so (from me asking questions on this website). I ended up defining a page style:

\fancypagestyle{laststyle}
{
\fancyhf{}
\fancyfoot[R]{\textbf{STOP}}
}


and putting \thispagestyle{laststyle} where I wanted it.

-

## 2 Answers

Add

\AtEndDocument{\thispagestyle{empty}}


to the document preamble. Alternatively, replace empty with whichever page style you want to match your other document components. What should also work is to use

\AtEndDocument{\rfoot{}}


to clear the right footer.

Here is a minimal example showing the usage and output:

\documentclass{article}

\usepackage{lipsum}

%headers, footers
\usepackage{fancyhdr}
\pagestyle{fancyplain} %header
\fancyhf{} % sets both header and footer to nothing
\renewcommand{\headrulewidth}{0pt} % your new footer definitions here
\rfoot{\large \textbf{GO ON TO NEXT PAGE}}
\lfoot{\large\thepage}
\AtEndDocument{\rfoot{}}
\begin{document}

\lipsum[1-50]

\end{document}


The above may not work in all instances, as it depends on when the pages are shipped out. One may use some help from pageslts's VeryLastPage label and refcount to extract the value:

\usepackage{pageslts,refcount}

%headers, footers
\usepackage{fancyhdr}
\pagestyle{fancyplain} %header
\fancyhf{} % sets both header and footer to nothing
\renewcommand{\headrulewidth}{0pt} % your new footer definitions here
\rfoot{\ifnum\getpagerefnumber{VeryLastPage}=\value{page}\else\large \textbf{GO ON TO NEXT PAGE}\fi}
\lfoot{\large\thepage}
\pagenumbering{arabic}

-
You're being lucky; try with \lipsum[1-48]a\\a\\a\\a –  egreg Jul 14 at 22:04
@egreg: Corrected using pageslts's VeryLastPage label. Probably similar to your zref-totpages approach. –  Werner Jul 14 at 22:39

The safest method is checking if the page is really the last one, which can be obtained with zref-totpages:

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

%headers, footers
\usepackage{fancyhdr}
\pagestyle{fancy} %header
\fancyhf{} % sets both header and footer to nothing
\renewcommand{\headrulewidth}{0pt} % your new footer definitions here
\fancyfoot[R]{\ifnum\value{page}=\ztotpages\else\large \textbf{GO ON TO NEXT PAGE}\fi}
\fancyfoot[L]{\large\thepage}
\begin{document}

\lipsum[1-48]
a\\
a\\
a\\
a
\end{document}


The example, taken from Werner's answer, fails with his code: both page 9 and 10, with the simple \AtEndDocument{\rfoot{}} would have an empty footer.

Instead of \lfoot and \rfoot I prefer \fancyfoot and \fancyhead; they're almost equivalent, but the “newer” ones are better.

-
Is not \AtEndDocument{\rfoot{}} equivalent to \rfoot{}\end{document}? If yes, why does it fail? –  Sigur Jul 14 at 22:27
@Sigur The page breaking mechanism is asynchronous; in the (admittedly a bit contrived) example, the last four line paragraph is typeset and the page break has not yet been decided when \end{document} is scanned. Now \end{document} executes \rfoot{} and so this setting applies also to the last but one page, because TeX decides to break the page after having performed that action. –  egreg Jul 14 at 22:34