2

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.
<to be read again>
                   \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?

AndreKR
  • 1,025
  • Try \lipsum[10] (untested) or \lipsum[1-10] –  May 23 '19 at 15:04
  • 1
    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 '19 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 '19 at 15:19
  • 2
    @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 '19 at 15:21
  • Maybe you can use something from https://tex.stackexchange.com/questions/50111/how-to-check-if-the-value-of-a-parameter-is-a-number and the questions linked from there. – Marijn May 23 '19 at 15:32
  • @AndreKR You need to compile it twice anyway. In editors like Texmaker, you have an option to execute pdflatex twice or as many times as you need (because you need more than 2 sometimes). You should edit the script to check the exit code only after the second run. The first time it is generating the .aux file and the second time it populates the label references. – Cain May 23 '19 at 15:33
  • @AndreKR Maybe this will help with the explanation: https://tex.stackexchange.com/questions/28708/why-does-pagereflastpage-give-me-rather-than-page-number-of-the-last-pag?rq=1 – Cain May 23 '19 at 15:36
  • 1
    @cain see the comment where AndreKR acknowledges that the problem IS having to make two UNCONDITIONAL runs i.e. he needs the first run to NOT generate error your link does not show how to avoid it only confirms there is a need for this question –  May 23 '19 at 20:31

2 Answers2

2

The command \label{mytag} defines the command \r@mytag. This means that you can test if that command exists, and react accordingly:

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

\setlength{\parskip}{6cm}

\begin{document} \makeatletter \SetBgContents{ @ifundefined{r@LastPage}{% RECOMPILE% }{% \ifnum\thepage=\pageref{LastPage} This is the last page\else More pages follow\fi }% } \makeatother

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}

Teepeemm
  • 6,708
1

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
AndreKR
  • 1,025