Execute a command at the start of every page

1,721

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}}}
}
Share:
1,721

Related videos on Youtube

Masroor
Author by

Masroor

Updated on August 01, 2022

Comments

  • Masroor
    Masroor over 1 year

    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.

    • Lev Bishop
      Lev Bishop almost 10 years
      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.
    • Masroor
      Masroor almost 10 years
      @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.
    • egreg
      egreg almost 10 years
      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.
    • Masroor
      Masroor almost 10 years
      @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.
    • egreg
      egreg almost 10 years
      @MMA You can't change the geometry mid page. And TeX is typicall mid page when it decides to make a page break.
    • Masroor
      Masroor almost 10 years
      @egreg So, perhaps, time to give up.
    • David Carlisle
      David Carlisle almost 10 years
      @MMA you'd need something like this but determining at what line the page breaks is hard in general tex.stackexchange.com/a/142039/1090
    • Lev Bishop
      Lev Bishop almost 10 years
      @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.
    • egreg
      egreg almost 10 years
      @LevBishop If you're able to do it, you're welcome! ;-)
    • Lev Bishop
      Lev Bishop almost 10 years
      @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
      Lev Bishop almost 10 years
      @egreg "should be possible" \neq "I can do it" :-)
    • Masroor
      Masroor almost 10 years
      @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). ;-)