[Tex/LaTex] Special action for every new page

macros

I need to define a macro or do some special action every time a new page is encountered. In some sense I want to "number" tikzpictures but have all the tikzpictures on the same page contain the same number. (It's more complex than this but it is just a simple example)

Basically all I want to do is define a macro at the start of every new page. I'll use that macro to in my tikzpictures to do some special things and they may delete the macro depending on something(which is why I need to set it on each new page so it get's reset each page).

The macro needs to be defined at the start of every new page before any tex in my document is executed.

Basically I want to avoid having to add the macro in the document myself for each new page(which is hard to control and clutters up the thing).

My case: I have several tikspictures scattered throughout my document but some are on the same page.

I want the first one on the page to have special treatment(like put a caption on it). BUT I may add or remove text which may change the ordering and I don't want to have to update the code.

For example,

-New Page
Text
Img1 *
Img2 
-New Page
Img3 *
Text
Img4
Img5
-New Page
Img6 *

Because Img 1 3 and 6 are the first on the page I want to do something to them. BUT I do not want to hard code this because it may easily change(when I add text it may push put some images on a new page, i.e.,

-New Page
Text
Img1 *
-New Page
Img2 
Img3 *
Text
Img4
-New Page
Img5
Img6 *

and notice how * does not correspond to the first image on the page(like I want it). i.e., this is what will happen if you hard code the things I want to do.

Also, Because tex first has to calculate image bbox to determine if it is on the current page or not I need code that will work since my images will depend on if they are the first image on the page or not.

My "Special" action is only horizontally centering the image and so shouldn't effect layout.

The issue stems from

tikzpicture alignment and centering

and I'm trying to streamline and automate the process. I need to apply the master and slave pattern in the way I have describe. The first image on the page should get the master style and the remaining should get the slave BUT not hard coded since it causes the problem I gave above.

(Note how Jake's answer has hard coded master and slave. This causes major problems if you add text which pushes some images on a new page. (since the first image on the page should be master but by hard coded it you could get a slave as the first image)

Best Answer

You still need to do it in two passes.

Consider a single long paragraph that takes a page or two. all macros in the paragraph are fully expanded before line breaking is considered, and only after line breaks are decided the output routine is triggered to consider page breaking.

The page breaking never affects the line breaking decisions, and the line breaker never affects the macro expansion. So no part of the macro expansion can be affected by the page breaking.

What you can do is get each picture to do a \label{uniquelabel} then it can (on a later run of latex) inspect \pageref{uniquelable} and \pageref{the-label-of-previous-figure} to see if you are the first on the page.

so in the example below, the first example on every page gets a modified formatting saying "first on page"

enter image description here

\documentclass{article}
\textheight15\baselineskip
\newcounter{pcount}
\makeatletter
\newenvironment{test}{%
\refstepcounter{pcount}%
\par\bigskip
\edef\this{\expandafter\expandafter\expandafter\@secondoftwo\csname r@x:\thepcount\endcsname\@empty}%
\ifx\this\@empty
\typeout{first pass}%
\def\format{}%
\else
\advance\c@pcount\m@ne
\edef\prev{\expandafter\expandafter\expandafter\@secondoftwo\csname r@x:\thepcount\endcsname\@empty}%
\advance\c@pcount\@ne
\ifx\prev\this
\def\format{}%
\else
\def\format{first on page! }%
\fi
\fi
\begin{tabular}{|l|}\hline \format test:\label{x:\thepcount} \thepcount\\\hline}
{\\\hline\end{tabular}
\bigskip}
\makeatother

\begin{document}

test

\begin{test}one\\two\end{test}
\begin{test}aaa\\bbb\\ccc\end{test}
\begin{test}aaa\\bbb\\ccc\end{test}

more text

\begin{test}red\\green\\blue\end{test}
\begin{test}1\\2\\2\\4\end{test}

more text

\begin{test}black\\white\\\end{test}
\begin{test}a\\b\\c\\d\end{test}

\end{document}
Related Question