4

Is there a way of ensuring that a given floating object (e.g., a figure) is always placed on a specific page in the output PDF? For instance, "this graphic should always be placed at the top of page 2."

I can usually achieve this behavior with \begin{figure}[t] and then moving around this environment until I find a place where it ends up on the desired output page, but that does not feel very principled, plus I might have to move the environment again every time I make any changes to the preceding text.

And yes, I am aware that this, to some extent, defeats the purpose of a floating object :-)

der_herr_g
  • 280
  • 2
  • 9

2 Answers2

5

enter image description here

You can hide the requested page in the width of the float (making it 4sp wider than the page for page 4, or to enable twocolumn support double the value, adding one for the 2nd column) and then adjust the code that looks for (the power of 2 representing) t in [htb]arguments to report t not present on such over-wide floats until the appropriate page is found.

\documentclass{article}

\usepackage{placeonpage}

\usepackage{lipsum} \begin{document}

\begin{figure}[t] \centering \rule{3cm}{3cm} \caption{A figure on page 4} \placeonpage{4} \end{figure}

\begin{figure}[t] \centering \rule{3cm}{3cm} \caption{A figure on page 6} \placeonpage{6} \end{figure} \lipsum\lipsum\lipsum\lipsum \end{document}

a two column example

enter image description here

\documentclass[twocolumn]{article}

\usepackage{placeonpage}

\usepackage{lipsum} \begin{document}

\begin{figure}[t] \centering \rule{3cm}{3cm} \caption{A figure on top page 4} \placeonpage{4} \end{figure}

\begin{figure}[b] \centering \rule{3cm}{3cm} \caption{A figure on bottom page 5} \placeonpage{5} \end{figure}

\begin{figure}[t] \centering \rule{3cm}{3cm} \caption{A figure on top right page 6} \placeonrightcolumn{6} \end{figure}

\begin{figure}[t] \centering \rule{7cm}{3cm} \caption{A spanning figure on top page 8} \placeonpage{8} \end{figure}

\lipsum\lipsum\lipsum\lipsum \lipsum\lipsum\lipsum\lipsum \end{document}


placeonpage.sty


\let\latex@getfpsbit\@getfpsbit

\def@getfpsbit#1{% \ifnum\numexpr2*\c@page\if@firstcolumn\else+1\fi\relax <\numexpr\wd@currbox-% \ifdim\f@depth=\z@\columnwidth\else\textwidth\fi\relax @tempcnta\z@ \else \latex@getfpsbit#1% \fi }

\def\placeonpage#1{% \hrule height 0pt depth 0pt width \dimexpr\hsize+\numexpr2(#1)\relax sp\relax} \let\placeoleftcolumn\placeonpage \def\placeonrightcolumn#1{% \hrule height 0pt depth 0pt width \dimexpr\hsize+\numexpr2(#1)+1\relax sp\relax}

David Carlisle
  • 757,742
  • +1! So, now it does. :) – mickep Feb 08 '23 at 05:54
  • This looks very promising! I noticed it does not seem to work if the twocolumn option is used for the document class (neither with \begin{figure} nor with \begin{figure*}). Do you have any ideas for how to fix this? – der_herr_g Feb 08 '23 at 21:18
  • 1
    @der_herr_g sigh you just need to do the same thing, in more places:-) I just did single column document t floats. may do the twocolumn topfloats later and add here:-) – David Carlisle Feb 08 '23 at 21:29
  • 1
    @der_herr_g try now, I deleted 90% of the code so now the cases you mention work, also b float. I may change it to use odd/even sp values to denote left or right col, currently only page, so it comes on first column – David Carlisle Feb 08 '23 at 22:15
  • 1
    @der_herr_g left/right column support added – David Carlisle Feb 08 '23 at 22:41
  • @David what is the reason for duplicate lines 1 and 3 in placeonpage.sty ? – Jhor Feb 08 '23 at 23:48
  • @Jhor apparently my inability to fully delete the original version before adding the update.... I'll fix, thanks – David Carlisle Feb 08 '23 at 23:57
  • @David Any chance you could also add support for \begin{figure*} environments? With the current implementation, it seems like those all get pushed to the end of the document as soon as I add \usepackage{placeonpage} to the preamble. Thank you so much in advance! – der_herr_g Feb 09 '23 at 00:23
  • @der_herr_g they were working unless I broke them with the column support,let me check...... – David Carlisle Feb 09 '23 at 00:29
  • @der_herr_g hmm try now – David Carlisle Feb 09 '23 at 00:44
  • @David It's working! Amazing, thank you so much! :-) – der_herr_g Feb 09 '23 at 21:07
1

In general, no. How do you think that in your code you could put the figure at the end of your 30 page document but expect that it will appear on the second page.

You could use a nonfloating graphic and use \captionof from the caption package to give it a caption.

Something like (after correcting typos and adding text)

 \documentclass...
    \usepackage{caption}
    \begin{document}
    Bunch of text
\includegraphics{...}
\captionof{Non floating illustration}

More text etc...

Peter Wilson
  • 28,066
  • This seems similar to placing a figure with [H]? I feel like that doesn't help with my problem because I will still have to move the figure block around every time I change the surrounding text? – der_herr_g Jan 31 '23 at 13:38