11

Is there any way I can execute a command (\ifthenelse actually) at the start of every page?

I tried \AtBeginShipout (atbegshi) but that seem to get executed when the page is about to be rendered (shipped out). I want something different, as soon as the page is about to start.

lockstep
  • 250,273
Masroor
  • 17,842
  • What's the ultimate effect you're trying to achieve here? There are various mechanisms depending on what you have in mind: eg, something like adding a "DRAFT" watermark, or something like adding a header/footer, or something like putting "Theorem 2 (contd.)" if a previous section is overflowing onto this page. – Lev Bishop Nov 15 '13 at 17:30
  • @LevBishop Actually, I want to test if the page number has a certain value, if the answer is yes, I want to execute a predefined command at that point. And this command is not included the cases (adding .... onto this page) you enumerated. – Masroor Nov 15 '13 at 17:37
  • What kind of command would you execute? Note that you cannot act on what TeX has already typeset; when it breaks a page, it usually has already typeset a complete paragraph, so this cannot be modified any more. – egreg Nov 15 '13 at 17:41
  • @egreg I am trying to find a solution to this question. I find that I can find the last page number using the lastpage package. Now, if I can execute this at the start of every page, \ifthenelse{\thepage=\pageref{LastPage}}{change geometry}{\relax}, I can solve the problem. Definitely, the document will have to be compiled a number of times to stablize everything. – Masroor Nov 15 '13 at 17:46
  • 1
    @MMA You can't change the geometry mid page. And TeX is typicall mid page when it decides to make a page break. – egreg Nov 15 '13 at 17:50
  • @egreg So, perhaps, time to give up. – Masroor Nov 15 '13 at 17:52
  • 1
    @MMA you'd need something like this but determining at what line the page breaks is hard in general http://tex.stackexchange.com/a/142039/1090 – David Carlisle Nov 15 '13 at 19:44
  • @egreg limited forms of geometry change should be possible (eg, changing the top/bottom margins, or shifting the textblock left/right), just not ones that change the width of the textblock, since that has already been frozen. – Lev Bishop Nov 16 '13 at 14:02
  • @LevBishop If you're able to do it, you're welcome! ;-) – egreg Nov 16 '13 at 14:05
  • @MMA for this kind of typographic nicety I think it's ok to do it manually with a manual pagebreak and geometry change command. – Lev Bishop Nov 16 '13 at 14:06
  • @egreg "should be possible" \neq "I can do it" :-) – Lev Bishop Nov 16 '13 at 14:08
  • @LevBishop I can not agree more. Situations like this arise when we want to add some back cover, or a colophon or something similar. It is very likely that I know what and where it is going to happen. Moreover, there is only one last page in a document. My initial impression was to advise the OP of this question, to handle the situation manually. Then I thought that I am not at liberty to control the end user and should not advise on what he should do. Also, I felt like taking a challenge (and failed). ;-) – Masroor Nov 16 '13 at 14:13

1 Answers1

7

You'll need the package bophook to add commands at the beginning of each page:

Using the \AtBeginPage hook, you can add material in the background of a page. Think of it as a construct that does: For every page, create a picture environment with its origin at the top left corner of the paper (resp., output device). So for example, you can put an image that is three centimeters from the left border and five from the top border by saying

\AtBeginPage{%
   \setlength{\unitlength}{1cm}
   \put(3, -5){\makebox(0,0)[tl]{\includegraphics{image}}}
}
tricasse
  • 186