1

I want to display a warning message if the number of pages is out of some range. In particular I want to count only pages between \mainmatter and \backmatter. But I don't know how. Googling I found this useful answer but it must be adapted. Specifically I want to specify range as [10,40] (number of pages between 10 and 40 inclusive), [10,-1] (at least 10 pages) or [-1,40] (no more than 40 pages). I prefer ranges with inclusive bounds, but it is not important. Moreover I want to exclude blank pages. In the warning would be useful show current number of pages.

miticollo
  • 173

1 Answers1

1

You need to redefine \cleardoublepage and \backmatter. I also define a counter that takes care of the skipped pages. If there is no \backmatter, the task is performed “at very end”. In each case, we need to subtract 1 from the page number, because the page counter has been stepped.

\documentclass[a4paper]{book}
\usepackage{atveryend}

\usepackage{lipsum}

\makeatletter \newcommand{\mainpagerange}[2]{% \edef\lorenzo@minpages{#1}% \edef\lorenzo@maxpages{#2}% } \newif\iflorenzo@backmatter

\renewcommand{\backmatter}{% \lorenzo@backmattertrue \if@openright \cleardoublepage \else \clearpage \fi \lorenzo@check @mainmatterfalse } % in case there is no \backmatter \AtVeryEndDocument{% \iflorenzo@backmatter \else \lorenzo@check \fi }

\newcounter{lorenzo@skippage} \renewcommand{\cleardoublepage}{% \clearpage \if@twoside \ifodd\c@page \else \stepcounter{lorenzo@skippage}% \hbox{}\newpage \if@twocolumn \hbox{}\newpage \fi \fi \fi }

\newcommand{\lorenzo@check}{% \edef\lorenzo@pages{\the\numexpr\value{page}-\value{lorenzo@skippage}-1\relax}% \ifnum\lorenzo@pages<\lorenzo@minpages \lorenzo@warning{less}{\lorenzo@minpages}% \fi \ifnum\lorenzo@maxpages<0 \else \ifnum\lorenzo@pages>\lorenzo@maxpages \lorenzo@warning{more}{\lorenzo@maxpages}% \fi \fi }

\newcommand{\lorenzo@warning}[2]{% @latex@warning@no@line{you have #1 than #2 pages}% }

\makeatother

\mainpagerange{10}{40}

\begin{document}

\mainmatter

% this generates pages 1 and 2 (blank) \chapter{Test 1}\lipsum[1]

% this generates pages from 3 to 10 (no blank) \chapter{Test 2}\lipsum[1-40]

% this generates pages from 11 to 41 \chapter{Test 3}\lipsum[1-100]\lipsum[1-81]

% this generates page 43 (and blank 44 with \backmatter) \chapter{Test 4}\lipsum[1]

%\backmatter

\end{document}

Try the various combinations. In this case I get

LaTeX Warning: WARNING: you have more than 40 pages.

No warning if I remove the last chapter, notwithstanding that the total number of pages is 41 (the second page in the first chapter is blank).

You may want to do

\renewcommand{\cleardoublepage}{%
  \clearpage
  \if@twoside
    \ifodd\c@page
    \else
      \stepcounter{lorenzo@skippage}%
      \hbox{}%
      \thispagestyle{empty}%
      \newpage
      \if@twocolumn
        \hbox{}\newpage
      \fi
    \fi
  \fi
}

in order to have really empty pages.

egreg
  • 1,121,712