# Compile just stops

I have this document where I make money for a board game with my own design. Now the issue: The compiler stops at page 251 (of 384 pages). No error message, nothing!

\documentclass{article}

\usepackage[paperheight   = 60mm,  paperwidth  = 130mm,
layoutheight  = 60mm, layoutwidth  = 130mm,
layoutvoffset = 0mm,  layouthoffset = 0mm,
showframe = false, showcrop = false]{geometry}

\usepackage[T1]{fontenc}
\newcommand{\fontfam}{augie}

\usepackage{pdfpages}
\usepackage{forloop}

\usepackage{fmtcount}% http://ctan.org/pkg/fmtcount
\usepackage[first=1707, last=98765432, seed=1708, counter=serialnumber]{lcg}

\usepackage[printwatermark=true]{xwatermark}

\newcommand{\shiftx}{-44mm}
\newcommand{\wm}[1]{\newwatermark*[allpages, fontfamily=\fontfam, color=black!90!blue, angle=0, fontsize=3.2mm, scale=1, ypos=.8mm, xpos=\shiftx, textalign=center]{\padzeroes[8]{\decimal{#1}}}}

\newcommand{\img}[1]{\includepdf[width=\textwidth, height=\paperheight]{#1}}

\newcounter{pagenumber}

\begin{document}

% % % % %  THIS DOES NOT WORK!
% % % % %  IT ALWAYS STOPPS AT SOME POINT AFTER THE 20's OR 50's...
% % % % %  Total of 384 pages

% % % 1
\forloop{pagenumber}{1}{\value{pagenumber} < 121}{ % 121
\rand
\wm{serialnumber}
\img{example-image-a.png}
}
% % % 5
\forloop{pagenumber}{1}{\value{pagenumber} < 25}{ % 25
\rand
\wm{serialnumber}
\img{example-image-b.png}
}
% % % 10
\forloop{pagenumber}{1}{\value{pagenumber} < 49}{ % 49
\rand
\wm{serialnumber}
\img{example-image-c.png}
}
% % % 20
\forloop{pagenumber}{1}{\value{pagenumber} < 25}{ % 25
\rand
\wm{serialnumber}
\img{example-image-a.png}
}
% % % 50
\forloop{pagenumber}{1}{\value{pagenumber} < 25}{ % 25
\rand
\wm{serialnumber}
\img{example-image-b.png}
}
% % % 100
\forloop{pagenumber}{1}{\value{pagenumber} < 97}{ % 97
\rand
\wm{serialnumber}
\img{example-image-c.png}
}
% % % 500
\forloop{pagenumber}{1}{\value{pagenumber} < 49}{ % 49
\rand
\wm{serialnumber}
\img{example-image-a.png}
}

\end{document}


These are the last few lines of the log:

[251]
File: example-image-c.png Graphic file (type png)
<use example-image-c.png>
Package pdftex.def Info: example-image-c.png  used on input line 69. on input l
ine 69.
Package pdftex.def Info: Requested size: 401.49901pt x 301.12425pt on input lin
e 69.


Any clues are appreciated.

• pdflatex: /usr/local/texlive/2017/texmf-dist/tex/latex/mwe/example-image-b.png: Too many open files It doesn't stop with LuaLaTeX. – egreg Jul 26 '17 at 8:42
• By the way, why \includepdf and not \includegraphics? – egreg Jul 26 '17 at 8:46
• @egreg: why \includepdf and not \includegraphics: because \includepdf does not stretch my image! TOO MANY OPEN FILES! Damn! How did you find that? TeXstudio did not return any errors? – gilu Jul 26 '17 at 9:07
• LuaLaTeX works! I still have to compile it twice to make the serial number work - but that's acceptable. Oh, and \includepdf does not add margins. – gilu Jul 26 '17 at 9:19
• To avoid having to run the document more than once, I needed a file called <texfilename>.xwm with this content: \relax \xwmnewlabel{xwmlastpage}{{}{ <number of pages in document> }{\relax }{Doc-Start}{}}. Since I know how many pages I'll end up with, this is an easy fix! Now all the serial number (or random numbers) show up after the first build. – gilu Aug 8 '17 at 13:23