2

I need to append a draft page only if the total number of pages in the main document is odd.

The following works well

\documentclass{exam}
\usepackage{blindtext,tikz,ulem,atveryend,hyperref}
\usepackage[totpages,user]{zref}
\newcommand\DraftPage{%https://tex.stackexchange.com/a/263613/2288
    \clearpage
    \begingroup
    \null
    \thispagestyle{empty}%
    \begin{tikzpicture}[remember picture,overlay]
    \node at ([yshift=-2cm]current page.north) {\LARGE\textbf{\uline{Draft Page}}};
    \draw[line width=5pt]
    (current page.north west) -- (current page.south east);
    \draw[line width=5pt]
    (current page.south west) -- (current page.north east);
    \end{tikzpicture}
    \addtocounter{page}{-1}%
    \hypersetup{pageanchor=false}%
    \clearpage
    \endgroup
}
\AtVeryEndDocument{
    \ifodd\ztotpages 
    \DraftPage
    \fi
}
\begin{document}
    \blindtext[1-30]\\
    \textbf{Total number of pages is \ztotpages.}
    \DraftPage
\end{document}

enter image description here

enter image description here

until changing it to

\begin{document}
    \blindtext[1-30]\\
    \textbf{Total number of pages is \ztotpages.}
\end{document}

then I get on the second page a wrongly rendered appended draft page.

enter image description here

enter image description here

So, I have two questions:

1- How to make the command \DraftPage not affect the total number of pages detected by \ztotpages in the former working case? In other words. \ztotpages needs to be 1.

2- Why does the second case result in the wrong output? and how to fix it?

Diaa
  • 9,599

2 Answers2

2

Edit:

An another solution, hopefully this time more stable:

\documentclass{exam}
\usepackage{tikz,ulem,etoolbox,hyperref}
\usepackage{lipsum}
\usepackage[totpages,user]{zref}
\newcommand\DraftPage{
    \cleardoublepage                % <---
    \thispagestyle{empty}
    \ifodd\thepage\else             % <---
    \centering\LARGE\textbf{\uline{Draft Page}} % <--- 
    \begin{tikzpicture}[remember picture,overlay]
    \draw[line width=5pt]
    (current page.north west) -- (current page.south east);
    \draw[line width=5pt]
    (current page.south west) -- (current page.north east);
    \end{tikzpicture}
   \fi%                             % <---
}
\AtEndDocument{\DraftPage}          % <---

\begin{document}
    \lipsum%\lipsum\lipsum\lipsum
    \lipsum[1-3]
    \textbf{Number of pages is \ztotpages.}
\end{document}

This solution in documents with odd number of pages add \DraftPage on the end of document. This is obtained after at least two compilation of documents. I tested the solution with documents with up to five pages of text. All results was correct after two compilations.

  • Definition of the \DraftPage is changed. Now it contain the test if the last page is odd.
  • In my MWE I use lipsum instead blindtext since I'm more familiar with it.
  • In solution is used the etoolbox package and its document hook \AtEndDocument instead of atveryend package and its hook \AtVeryEndDocument, which to my best knowledge doesn't work as you expected (but I may be wrong).
  • In solution is not used \ztotpages anymore. It now serve only for show the number of documents' page.
Zarko
  • 296,517
  • May I know why you added \mbox{}? – Diaa Mar 01 '20 at 11:37
  • It actually start \DraftPag page. Without it you obtain only part of image as you shown in your question. – Zarko Mar 01 '20 at 11:45
  • Please try this one: `\AtEndDocument{ \ifodd\ztotpages \else \DraftPage \fi}

    \begin{document} \lipsum \lipsum %\lipsum[1-3] \textbf{Number of pages is \ztotpages.} \end{document}`. You will always get different output each time you compile. I only need to print a draft page only if the total number of pages is odd. I think it is a different question needed to be asked separately.

    – Diaa Mar 01 '20 at 11:47
  • Do you compile twice? I obtain correct result. – Zarko Mar 01 '20 at 11:53
  • I compiled a lot of times. Each compile gets different output than the previous run. – Diaa Mar 01 '20 at 11:55
  • The second compilation is needed due to tikzpicture which use options remember picture,overlay. After second compilation result is stable -- always the same. I use recent MikTeX with recent packages, if this matter. – Zarko Mar 01 '20 at 12:00
  • This document switches between two pages document and three pages with a wrongly rendered draft page every single run. – Diaa Mar 01 '20 at 12:03
  • @Diaa, indeed. I will delete my answer. – Zarko Mar 01 '20 at 12:06
1

I'd use \BeforeLastShipout.

\documentclass{exam}
\usepackage{lipsum,tikz,ulem,atveryend,hyperref}
\usepackage[totpages,user]{zref}

\BeforeClearDocument{\clearpage\DraftPage}
\newcommand{\DraftPage}{%
  \ifodd\value{page}\else
    \thispagestyle{empty}%
    \mbox{}%
    \begin{tikzpicture}[remember picture,overlay]
    \node at ([yshift=-2cm]current page.north) {\LARGE\textbf{\uline{Draft Page}}};
    \draw[line width=5pt]
    (current page.north west) -- (current page.south east);
    \draw[line width=5pt]
    (current page.south west) -- (current page.north east);
    \end{tikzpicture}%
    \addtocounter{page}{-1}%
    \hypersetup{pageanchor=false}%
    \renewcommand{\DraftPage}{}%
  \fi
}

\begin{document}

\lipsum[1-25]

\textbf{Total number of pages is \zpageref{LastPage}.}

\end{document}

enter image description here

If you change into \lipsum[1-30], the additional page is not shipped out.

egreg
  • 1,121,712
  • Many thanks for this stable solution. If you don't mind I would be happy to make sure I understand some stuff correctly: (1) does \ifodd\value{page}\else check the total number of pages is even so that it activates the command \DraftPage? It seems superficially to check for even number although I found it activates \DraftPage when the total number of pages is odd. (2) why does \BeforeClearDocument{\clearpage\DraftPage\clearpage\DraftPage} not work in case I want to append multiple instances of the daft page? – Diaa Mar 01 '20 at 17:52
  • @Diaa It's necessary to redefine \DraftPage to do nothing, otherwise it would add two pages. You wanted one, didn't you? – egreg Mar 01 '20 at 17:54
  • Kindly, check my previous comment after editing it. I don't quite understand how \ifodd\value{page}\else works/means. – Diaa Mar 01 '20 at 17:56
  • @Diaa I emit \clearpage so everything is (hopefully) shipped out. This operation steps the page counter. There are two cases: page is even means that the last shipped page was odd and you need \DraftPage; otherwise page is odd and the last shipped page was even. – egreg Mar 01 '20 at 17:59
  • I apologize for not having the enough knowledge to let me fully grasp your technical description. For your previous question of whether or not I need one page, my main target is to check the total number of pages, and it will fall in two cases: (1) odd number of pages print a draft page then use \includepdf{GraphPaper.pdf} then draft page then \includepdf{GraphPaper.pdf}. (2) even number of pages print a graph paper with \includepdf{GraphPaper.pdf} then draft page then \includepdf{GraphPaper.pdf}. So, I started my experimentation by trying to conditionally print the draft page. – Diaa Mar 01 '20 at 18:05
  • I found out that \AtEndDocument{ \ifodd\thepage\else \DraftPage \fi \includepdf{GraphPaper} \DraftPage \includepdf{GraphPaper} } does the job pretty well. I just found that \AtEndDocument behaves differently from \AtVeryEndDocument, which I don't know if it is worth asking a new question or not. – Diaa Mar 01 '20 at 18:49