# macro who argument is proxy of next page break or beginning of a new slide

QUESTION: How do I write macros that run a control sequence \C (for example print a figure on the page or slide, a) right after the next page break (or new slide), or b) right before the next page break (or new slide)?

ANSWER: I am looking for general methods illustrated with a simple example each, to learn from. If several answers are useful, several increasing bounties. (Of course the users most likely answer the question already have enormous rep I suppose .. )

If only possible for presentations, beamer or powerdot, seeing how a new frame is a user typed control sequence in that case, that would answer the question too. Particularly interested in article class however.

Perhaps use page numbers output as proxy for pagebreak: define the macro to take the page number counter as #1, and redefine the page number printing algorithm to contain \c at the end. Another control sequence then hides or unhides, if needed, the page numbers themselves by printing say a white square at their position?

If \futurelet must be used, please suggest how to use it in a nonfragile manner! That would be an answer. (It reads the next line inside the group it's in, does nothing if new frame control sequence is not present, else expands into \c if that is present, and so on.)

What's the point? Why not manually do it as usual? The question ultimately concerns something possibly of wide general usage, this being to construct evolving technical figure animation keyframes, that contain independent tikz figures, with automatic context specific markup and descriptions in panels as necessary. That is, instead of doing it one by one, which is tedious, at several frames per every couple seconds. Such a document can be batch extracted to .png sequences.

Consider a document whose content are series of figures generated by a foreach, such that some are, if some parameters are in sum above a threshold, displayed over not one per page but one per several frames or pages, with all permutations of variations in one particular parameter, and the certain images or text are automatically added in that case as markup.

I know: articles or various presentation classes are not intended for this: but so what? It can be done, and is rather useful I suggest. (Record audio, assemble keyframes from several such documents into video, and you've generated a particularly slick lecture!)

The reason for the question is that while the general document filling macro is straightforward, depending on the figure a series of stacked \foreachs, it complicates rapidly with if-then sequences for corresponding floating text markup and text panels that is same across several frames, if the feature requested in this question is absent. I am not sufficiently knowledgable in core macros (but learning!) to construct this particular feature, no idea where to begin with it. Perhaps it is very easy, and I am silly to be unaware of it.

• Well, I suspect, that your macro(s) should query first, whether they are applied in a beamer document or in an article document, then react on this conditional branches. For the article branch needspace could be a help perhaps – user31729 Jun 11 '14 at 4:52
• For the details of page breaking you may also have a look on the book by Victor Eijkhout Tex by Topic, especially chapter 27, available e.g. here (older version, however):mirrors.ctan.org/info/texbytopic/TeXbyTopic.pdf – user31729 Jun 11 '14 at 5:05
• Dirty approach: use tikzmark and retrieve the coordinates....... – Symbol 1 May 9 '15 at 14:52
• dirty is fine. i'm loading tikz anyway in all documents. can you expand on your comment? – Guido Jorg May 9 '15 at 22:07
• Why not use the allowframebreaks thing? Admittedly, it is evil. But any solution will be equally evil since your desiderata are precisely the reason the beamer author considers that option evil in the first place. – cfr May 10 '15 at 1:10

Here is one example using the \AddThispageHook feature of the everypage package, which as you will see upon compilation, converts to red font between pages 1 and 2, which is after the actual invocation point.

\documentclass{article}
\usepackage{everypage,lipsum,xcolor}
\begin{document}
\lipsum[1]
\lipsum[2-8]
\end{document}


The above approach works if you are only setting parameters at the page boundary (like a color change). It does not work for setting actual text, as the page margins are not properly set at the moment of a page-hook execution. However, there are other ways using a similar approach.

For something more complex, this will place a figure after page 3 (at the top of page 4). The key here is that using a page hook to set a \gdef will occur at a page boundary, and allows that parameter to be tested within the document. I do the test with an \everypar.

\documentclass{article}
\usepackage{everypage,lipsum,graphicx}
\def\Now{}
\begin{document}
\everypar{%
\if T\Now
\begin{figure}[t]
\centering\includegraphics[width=1in]{example-image-A}
\caption{Here is my page 4 figure}
\end{figure}
\gdef\Now{F}\fi
}
\lipsum[1-35]
\end{document}


And finally, a comparable beamer example (not requiring everypage since \frame occurs but once a frame), in which I tell it in frame 2 to insert a figure at the beginning of the next frame. Here, instead of using \everypar to do the test, as I did in the prior example, I use a redefined \frame to perform the \Action.

\documentclass{beamer}
\usepackage{graphicx}
\def\Action{\relax}
\let\svframe\frame
\def\frame{\svframe\Action\par\gdef\Action{\relax}}
\begin{document}
\begin{frame}
Frame 1
\end{frame}
\begin{frame}
\gdef\Action{\includegraphics[width=1in]{example-image}}%
Frame 2
\end{frame}
\begin{frame}
Frame 3
\end{frame}
\end{document}


• Impressive. This is working robustly on my end. As part of more complicated macros – Guido Jorg May 12 '15 at 12:06

FWIW, in ConTeXt you can use \startpostponing to print content on a particular page. For example, to position text (or float) on the next page, use:

\starttext
....
\startpostponing[+1]
...
\stoppostponing
...
\stoptext


To position text or float on a particular page (say 20), use

\startpostponing[20]
...
\stoppostponing

\starttext
....
% Content must have at least 20 pages
....
\stoptext


The implementation is in page-pst.mkiv. The ConTeXt output routine is different from that of LaTeX, so adapting the same implementation to LaTeX would require an understanding of the LaTeX output routine.

• This is very nice. Not familiar with ConTeXt. Now motivated to learn. I am reading the manual. This is seems powerful. Just asking in advance, would ConTeXt clash with any particular tikz usage (such as \def\gp#1#2{\node at (#1) {\includegraphics{\csname #2\endcsname}};}, using other documents as nodes in tikz) and if-then package usage output, all output to pdf? If it does clash with this, please suggest or link to the ConTeXt version. – Guido Jorg May 12 '15 at 4:38
• ConTeXt should work with Tikz. Best is to use context standalone (which can be installed in parallel with texlive or miktex). – Aditya May 12 '15 at 12:52

There is a cheap answer in the standard LaTeX document classes: When there are no other floats (figures or tables) around, you can use a float to achieve what you want:

1) At begin of the next page:

\begin{figure}[t] \C \end{figure}

2) At end of the current page (i.e. before pagebreak)

\begin{figure}[b] \C \end{figure}

When you have to deal with interacting floats, things become much more difficult, I'm afraid.