TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

\typeout{ Input starts on page \thepage}
\typeout{ End ends on page \thepage}}

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

share|improve this question
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
up vote 6 down vote accepted

  \typeout{Input starts on page \getpagerefnumber{SE@\arabic{SEinput}@#1-start}}%
  \typeout{Input ends on page
      \ifdim\pagegoal=\maxdimen -1\fi\relax}%


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

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

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.