11

I have a series of pdf files that I'm combining. I'd like to include as many of them as possible in the finished document, but for logistical reasons I cannot have a total of more than 50 pages.

Is there any way to make LaTeX stop compiling and throw an error message, should the page count exceed this total?

\documentclass{article}

\usepackage[a4paper]{geometry}
\usepackage{pdfpages}
\usepackage{tikz}
\renewcommand{\contentsname}{My combined file}
\setlength\parindent{0pt}

\begin{document}

\includepdf[pages=-]{cover.pdf}
\includepdf[pages=-]{doc1.pdf}
\includepdf[pages=-]{doc2.pdf}
\includepdf[pages=-]{doc3.pdf}
\includepdf[pages=-]{doc4.pdf}
\includepdf[pages=-]{doc5.pdf}
\includepdf[pages=-]{doc6.pdf}
\includepdf[pages=-]{doc7.pdf}
% More files included here

\end{document}
7
  • You can use everypage to test and \errmessage to produce the error. Dec 9, 2014 at 13:38
  • After some testing, \errsessage won't stop compilation. \errorstopmode will, but it also produces no output at all. Dec 9, 2014 at 14:19
  • I know I received help on here somewhere for this, but you can take a look at the minp and maxp keys for in the class I wrote for my thesis: github.com/vermiculus/smcm-tex/blob/master/smcm-tex/cosc/…, specifically line 134. The basic idea was to hook onto the end of the document to make sure that we were in the range. I'll post this up as an answer after work if you can be patient :) It won't just stop compiling, but it will throw out a message (can be made into a fatal error very easily). Dec 9, 2014 at 17:03
  • @SeanAllred That sounds like exactly what I need, thankyou.
    – JustinT
    Dec 9, 2014 at 18:42
  • @JustinT Done :) Dec 9, 2014 at 19:56

2 Answers 2

6

Use the (newly created) pagectrl package:

\documentclass{article}

\usepackage[range=2-3, report-as=error]{pagectrl}

\usepackage{mwe}

\begin{document}
\lipsum[1-40]
\end{document}

Note that issuing a fatal error will cause TeX to halt, but this error will not be thrown until \end{document}. I will be uploading this to CTAN later tonight. Until it's available in the distributions, simply download the sty file and put it in the same directory as your document (or follow any other manual install procedure).

4
  • That does exactly what I need, thankyou! However, for some reason the error message doesn't show up in the editor I'm using (latexeditor.org). The help text does appear if you type H after the error gets thrown though. Also, compilation does still seem to complete even if you set report-as=fatal, if you just press enter when the error gets thrown. Is this a result of the error not being thrown until \end{document} ?
    – JustinT
    Dec 10, 2014 at 9:36
  • @JustinT Yes – by the time the error is hit, all the pages have already been finalized (which is why we know what \thepage is), so failing compilation at this step won't really change that. There isn't a whole lot that can be done about that in particular, but I'll add an option to run this check on everypage as in the answer below. Dec 10, 2014 at 13:37
  • @JustinT This isn't really possible, actually – see this question. The problem – as I understand it – is that we can't halt output once it's started. \msg_fatal: cannot halt tex from inside a box (a basic unit of output). I will have to remove the fatal style (and might as well remove the critical style) from report-as to quell any confusion. Dec 10, 2014 at 15:06
  • Ah, fair enough. I can (probably) manage without fatal and critical styles, I'll just have to be more vigilant at compile time. Will this package be uploaded to CTAN anytime soon?
    – JustinT
    Dec 15, 2014 at 16:10
9

Here is as far as I got.

\documentclass{article}

\usepackage[a4paper]{geometry}
\usepackage{pdfpages}
%\usepackage{tikz}
\usepackage{everypage}

\AddEverypageHook{\ifnum\value{page}>50 \errmessage{Too many pages}\errorstopmode\fi}

\renewcommand{\contentsname}{My combined file}
\setlength\parindent{0pt}

\begin{document}
\includepdf[pages=-]{minitoc.pdf}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .