3

In this example

\documentclass{article}

\usepackage[a6paper,landscape,margin=1cm,bottom=2cm]{geometry}
\usepackage[english]{babel}
\usepackage{blindtext}

\usepackage{afterpage,refcount,lastpage}
\newcommand{\testafterpage}{%
    \noindent{\Large\ttfamily NEW PAGE!\quad}%
    \afterpage{\testafterpage}%
}
\AtBeginDocument{\testafterpage}

\begin{document}
\blindtext[3]
\end{document}

the words NEW PAGE! should be printed at the beginning of every page using the afterpage package. For some reason, NEW PAGE! is not inserted at the second page of the document:

afterpage fails on 2nd page

Is this intended behaviour? Is it possible to fix this in general?

I am aware of the various packages like atbegshi that allow to add some content when the page is shipped out, but I don't know if this can also be used as a replacement for the strange behaviour of afterpage.

  • I can't see the problem you're mentioning and get “NEW PAGE” on top of page 2. – egreg Mar 11 '14 at 18:42
  • @egreg: Really? I obtain a 4-page output, with NEW PAGE! at the top op pages 1, 3 & 4. – Werner Mar 11 '14 at 19:18
  • 1
    @Werner Add an empty line before \end{document}. The problem is that only paragraph endings trigger the page builder. – egreg Mar 11 '14 at 20:50
  • @egreg: I see. Since I use lipsum more often and it inserts \par by default. – Werner Mar 11 '14 at 21:52
  • Ok, but adding a \par doesn't help if I use, say, \blindtext[5] since there is no paragraph ending for more than one page of the output. After LaTeX called \shipout, there should be a list of the remaining content to be typeset on subsequent pages. Is there any possibility to modify this content or at least prepending something to it? – porst17 Mar 12 '14 at 10:18
2
\newcommand{\testafterpage}{%
    \noindent{\Large\ttfamily NEW PAGE!\quad}%
    \afterpage{\testafterpage}%
}

Is definitely not guaranteed to add text on to every page. The recursive call will add \afterpage at the point the page breaker was exercised to ship out the previous page but if more than one page worth of text had been collected then this is the wrong place.

\afterpage is an incredibly invasive, fragile mechanism: it's not designed at all to be used consistently through a document in this way.

% This package implements a command, |\afterpage|, that causes the
% commands specified in its argument to be expanded after the current
% page is output.\footnote{This is really a pre-release, to see whether
% people like the idea of a command like this. This implementation is
% \emph{not} particularly robust. This implementation does not work in
% two column mode, and can get `confused' by \LaTeX's floating
% environments.}
  • Ok, I see. But then the question remains: How do I accomplish the above task if \afterpage is not the right choice? – porst17 Nov 24 '15 at 10:22
  • @porst17 you can't in general add text into the paragraph run at the top of the page (as shown in your NEW PAGE) with afterpage or anything else in tex, the lines have already been broken into full width boxes well before page breaking happens. afterpage can only insert something above the text that has broken over a page, and if you want to do that in every page that is just the page head, as specified directly or via fancyhdr etc – David Carlisle Nov 24 '15 at 11:06
  • My purpose was to inject text at the beginning of first line of a page. But this seems to be impossible in TeX. – porst17 Nov 24 '15 at 14:19
  • @porst17 of course nothing is impossible (you can position every letter in the document by coordinate if you have to) but you'd have to disable almost all of tex's line and page breaking and do it "by hand" as linebreaking is independent of page breaking so at the page break you just have a list of boxes of full width lines, you can not insert something into the text and re-flow the paragraph. – David Carlisle Nov 24 '15 at 14:32
  • Ok, not impossible, but also not really feasible for the regular LaTeX user ... – porst17 Nov 24 '15 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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