# Different text on last page

I want to add one of two different texts, depending on whether it appears on the last page.

I tried this:

\documentclass{article}
\usepackage[angle=0, opacity=1, scale=1,position={0,0}]{background}
\usepackage{lastpage}

\setlength{\parskip}{6cm}

\begin{document}
\SetBgContents{
\ifnum\thepage=\pageref{LastPage} This is the last page\else More pages follow\fi
}

A lot of paragraphs

A lot of paragraphs

A lot of paragraphs

A lot of paragraphs

A lot of paragraphs

A lot of paragraphs

\end{document}


But this fails when the .aux file is not yet created with:

! Missing number, treated as zero.
\protect
l.22 A
lot of paragraphs


I think because then \pageref{LastPage} is ?? which I cannot compare using \ifnum.

How can I make this work?

• Try \lipsum[10] (untested) or \lipsum[1-10] – user156344 May 23 at 15:04
• If you then compile it the second time it works fine. I guess compiling two times is already a requirement for you to generate references. – Cain May 23 at 15:14
• The lipsum has nothing to do with the problem. I edited the question and replaced it with simple text. – AndreKR May 23 at 15:19
• @Cain Yes, but the first time gets stuck in an ugly prompt when run manually and when run as part of a script it aborts the whole job because it returns a non-zero exit code. This is what I want to change. – AndreKR May 23 at 15:21
• Maybe you can use something from tex.stackexchange.com/questions/50111/… and the questions linked from there. – Marijn May 23 at 15:32

Solved it by using the \getpagerefnumber command of the refcountpackage:
\usepackage{refcount}

\ifnum\thepage=\getpagerefnumber{LastPage} This is the last page\else More pages follow\fi