2

I meant to restrict the overlay to all but the last page, but this setup fails due to \zref@extractdefault{abspage}{abspage}{0} stuck at 0. Is there a solution to this specific issue, and a better approach altogether?

\documentclass{article}
\usepackage{lipsum}
\usepackage{atbegshi}
\usepackage{tikz}
\usepackage{xcolor}
\usepackage{zref-abspage}
\usepackage{zref-lastpage}

\makeatletter
\ExplSyntaxOn

\ProvideDocumentCommand{\OverlayTypeset}{ o m }
{
  % \AtBeginShipout
  \AddToHook{shipout/background}{
    \int_compare:nTF
    {
      \zref@extractdefault{abspage}{abspage}{0}
      <
      \zref@extractdefault{LastPage}{abspage}{0}
    }
    {%
      \int_show:n{\zref@extractdefault{abspage}{abspage}{0}}
      \int_show:n{\zref@extractdefault{LastPage}{abspage}{0}}
      \begin{tikzpicture}[remember~picture,overlay,text=red]
        \node[scale=6,opacity=0.5] at (current~page.center) {#2};
      \end{tikzpicture}
    }
    { }
  }
}

\ExplSyntaxOff
\makeatother

\OverlayTypeset{DRAFT}
\begin{document}
\lipsum[1-10]
\end{document}

log:

%[Loading MPS to PDF converter (version 2006.09.02).]
%) (/opt/texlive/2025/texmf-dist/tex/latex/epstopdf-pkg/epstopdf-base.sty
%(/opt/texlive/2025/texmf-dist/tex/latex/latexconfig/epstopdf-sys.cfg))
%> \zref@extractdefault {abspage}{abspage}{0}=0.
%<recently read> }
%                 
%l.39 \end
%         {document}
%? 
%> \zref@extractdefault {LastPage}{abspage}{0}=2.
%<recently read> }
%                 
%l.39 \end
%         {document}
%? 
%[1{/opt/texlive/2025/texmf-var/fonts/map/pdftex/updmap/pdftex.map}]
%> \zref@extractdefault {abspage}{abspage}{0}=0.
%<recently read> }
%                 
%l.39 \end{document}
%                   
%? 
%> \zref@extractdefault {LastPage}{abspage}{0}=2.
%<recently read> }
%                 
%l.39 \end{document}
%                   
%? 
%[2] (./debug-119.aux) )</opt/texlive/2025/texmf-dist/fonts/type1/public/amsfont
%s/cm/cmr10.pfb>
%Output written on debug-119.pdf (2 pages, 25603 bytes).
%Transcript written on debug-119.log.
%Latexmk: Getting log file 'debug-119.log'
%Latexmk: Examining 'debug-119.fls'
%Latexmk: Examining 'debug-119.log'
%Latexmk: Log file says output to 'debug-119.pdf'
%Latexmk: Using bibtex to make bibliography file(s).
%Latexmk: Errors, so I did not complete making targets
%Collected error summary (may duplicate other messages):
%  pdflatex: Command for 'pdflatex' gave return code 1
%      Refer to 'debug-119.log' and/or above output for details
%
%Latexmk: Sometimes, the -f option can be used to get latexmk
%  to try to force complete processing.
%  But normally, you will need to correct the file(s) that caused the
%  error, and then rerun latexmk.
%  In some cases, it is best to clean out generated files before rerunning
%  latexmk after you've corrected the files.

snapshot of pdf

2
  • 1
    why don't you just use the kernel label? Commented 18 hours ago
  • 1
    latexmk output is useless for debugging. but it tells you where the log file for pdflatex is, so you should post the complete first error from that file. Commented 18 hours ago

3 Answers 3

4

\zref@extractdefault{abspage}{abspage}{0} is always 0 as there is no label abspage. To compare with the current abspage number use \ReadonlyShipoutCounter instead. The package atbegshi is obsolete, it does nothing anymore.

\documentclass{article}
\usepackage{lipsum}
\usepackage{tikz}
\usepackage{xcolor}
\usepackage{zref-abspage}
\usepackage{zref-lastpage}

\makeatletter
\ExplSyntaxOn

\ProvideDocumentCommand{\OverlayTypeset}{ o m }
{
  % \AtBeginShipout
  \AddToHook{shipout/background}{
    \int_compare:nTF
    {
      \ReadonlyShipoutCounter
      <
      \zref@extractdefault{LastPage}{abspage}{0}
    }
    {%
      %\int_show:n{\zref@extractdefault{abspage}{abspage}{0}}
      %\int_show:n{\zref@extractdefault{LastPage}{abspage}{0}}
      \begin{tikzpicture}[remember~picture,overlay,text=red]
        \node[scale=6,opacity=0.5] at (current~page.center) {#2};
      \end{tikzpicture}
    }
    { }
  }
}

\ExplSyntaxOff
\makeatother

\OverlayTypeset{DRAFT}
\begin{document}
\lipsum[1-10]
\end{document}

enter image description here

2
  • \ProvideDocumentCommand? Commented 8 hours ago
  • @egreg I simply copied from the question ... Commented 7 hours ago
4

I would just use the format's labelling. You do not need zref for this nowadays.


\documentclass{article}
\usepackage{lipsum}
\usepackage{tikz}

\makeatletter
\ExplSyntaxOn

\ProvideDocumentCommand{\OverlayTypeset}{ o m }
{
  \AddToHook{shipout/background}{
    \int_compare:nTF
    {
      \value{page} < \PreviousTotalPages
    }
    {
      \begin{tikzpicture}[remember~picture,overlay,text=red]
        \node[scale=6,opacity=0.5] at (current~page.center) {#2};
      \end{tikzpicture}
    }
    { }
  }
}

\ExplSyntaxOff
\makeatother

\OverlayTypeset{DRAFT}
\begin{document}
\lipsum[1-10]
\end{document}
5
  • "Why don't you use kernel labels?" I've settled on \ReadonlyShipoutCounter < \PreviousTotalPages so now I don't need the zref-*'s. Commented 10 hours ago
  • 1
    @Erwann yes, I think \ReadonlyShipoutCounter will work and is a better option if you use multiple formats for page e.g. if you restart the counter for the main matter having numbered frontmatter independently. Commented 10 hours ago
  • Why \ProvideDocumentCommand? I can't see any reason for it instead of \NewDocumentCommand. Commented 8 hours ago
  • @egreg that is just copied from the OP's code. Commented 8 hours ago
  • @egreg Ulrike's answer also uses \ProvideDocumentCommand? Commented 8 hours ago
2

But it seems to me that, in general, for such a simple task one can just use \ifnum together with the lastpage and refcount packages. For example:

% Compile three times.
\documentclass{article}
\usepackage{lipsum}
\usepackage{tikz}
\usepackage{lastpage}
\usepackage{refcount}

\AddToHook{shipout/background}{%
  \ifnum\value{page}<\getpagerefnumber{LastPage}\relax
    \begin{tikzpicture}[remember picture,overlay,text=red]
      \node[scale=6,opacity=0.5] at (current page.center) {DRAFT};
    \end{tikzpicture}%
  \fi
}

\begin{document}
\lipsum[1-10]
\end{document}
2
  • 1
    Thanks, but I try to gear my code toward LaTeX3. Commented 10 hours ago
  • @Erwann Thanks for your comment. Using LaTeX3 is certainly a good option, but in this particular case the classic \ifnum approach is simpler and works without additional setup. LaTeX3 and classic LaTeX are meant to coexist, so either style is perfectly fine here. Commented 9 hours ago

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.