# How can I access the numbers of pages on which input material begins and ends?

Is there a way to access the numbers of pages on which material included using \input begins and ends? Crude approaches along the lines of

\def\myinput#1{%
\typeout{ Input starts on page \thepage}
\input{#1}
\typeout{ End ends on page \thepage}}


fail in exceptional cases (e.g. if a page break occurs just before, or just after, a \myinput).

-
It depends a bit what you mean by "begin" "end" and "fail" in your question. For example if the input file starts with \vfill (or any other space) that fills up the current page which is then followed by a page break, does the input "start" on the current page or the next. Would the answer be different for \vspace{0pt} ? –  David Carlisle Jun 6 '12 at 14:04
It's hard to compose rigourous definitions for this! Let's say that the input 'starts' on the first page that contains typeset material (including spaces) whose source is the input file, and likewise for the end (leaders are nasty here). Any other results count as 'fail'. –  Ian Thompson Jun 6 '12 at 15:04

\documentclass{article}
\usepackage{kantlipsum}
\usepackage{refcount}

\newcounter{SEinput}
\newcommand{\SEinput}[1]{%
\refstepcounter{SEinput}\label{SE@\arabic{SEinput}@#1-start}%
\typeout{Input starts on page \getpagerefnumber{SE@\arabic{SEinput}@#1-start}}%
\input{#1}%
\refstepcounter{SEinput}\label{SE@\arabic{SEinput}@#1-end}%
\typeout{Input ends on page
\number\numexpr\getpagerefnumber{SE@\arabic{SEinput}@#1-end}
\ifdim\pagegoal=\maxdimen -1\fi\relax}%
}

\begin{document}
\kant
\SEinput{se1}
\SEinput{se2}
\kant
\SEinput{se3}
\end{document}


The files se2.tex and se3.tex both contain only \kant, while se1.tex contains \kant\clearpage, so the end-of-input is computed when TeX is on a fresh page (and this can be tested by \ifdim\pagegoal=\maxdimen).

The output on the terminal is

[1][2]
Input starts on page 3
(./se1.tex [3] [4] [5])
Input ends on page 5
Input starts on page 6
(./se2.tex) [6] [7]
Input ends on page 8
[8] [9]
Input starts on page 10
(./se3.tex) [10] [11]
Input ends on page 12
[12]


One should note that the information, as you see, is already in the .log file.

Two runs are necessary for the information to stabilize, as we use the \label-\ref system. It may fail in various ways to give the correct number, but it should work in the commonest situations.

The start page is particularly problematic, rather than the end: when you \input a file, the first paragraph in it may cause TeX to put it on a new page and you can't know it without hooking to that first paragraph.

-