Tuesday, 28 May 2013

Dynamically insert full-page image at nearest page break

Dynamically insert full-page image at nearest page break

The situation: I have a document that essentially consists of many pages of plain text. I would like to include a full-page image at the page break nearest to a given line. It is important that the image be inserted at a pre-existing page break (i.e. that there not be excess trailing whitespace on the page preceding the inserted image).
In other words, I want to start with a document looking like this:

I would then like to be able to insert some sort of command in the text at the red mark that causes the image to be inserted at the nearest page break, thus giving me a result that looks like this:

The page containing the image should contain no other content (i.e. no header/footer/etc), and the image should be resized so that it fills the entire page (the images will already be of the correct aspect ratio).



Here's what I've tried so far. In all of these cases, my_image.* is an image that has been sized to the same aspect ratio as my document (in this case, ebook, which is 2:3, so my images are mostly 800x1200 pixels).
First, the naive approach:
\documentclass[ebook]{memoir}
\usepackage{pdfpages} % to include the image-pdf
\usepackage{blindtext}

\begin{document}
    \blindtext[10] % some plain text
    \includepdf{my_image.pdf} % the image I want to be positioned somewhere around here
    \blindtext[10] % some more plain text
\end{document}
This obviously fails, because it inserts the image exactly at the position I requested, which may not be at a page break. I can of course manually futz with the positioning of the \includepdf so that it falls immediately after the end of the last paragraph that is immediately before a page break, but this is unpleasant and also not a practical solution since I have a somewhat large number of these images (at various positions throughout my document) to deal with.
Second, an approach that attempts to use floats:
\documentclass[ebook]{memoir}
\usepackage{pdfpages} % to include the image-pdf
\usepackage{blindtext}

\begin{document}
    \blindtext[10] % some plain text
    \begin{figure}
        \includepdf{my_image.pdf} % the image I want to be positioned somewhere around here
    \end{figure}
    \blindtext[10] % some more plain text
\end{document}
What this ends up doing is it makes my_image.pdf the "background" of one of the pages rather than placing my_image.pdf on a separate page.
I then tried adding \pagebreak before \begin{figure}, before \includepdf{my_image.pdf}, before \end{figure}, and after \end{figure}; none of these did anything useful either.
Third, I tried using some code from this tex.SE answer, and futzing with vertical margins (based on looking at the output of the layout package):
\documentclass[ebook,oneside]{memoir}
\usepackage{blindtext}
\usepackage{graphicx}
% \adjustimg and \centerimg from http://tex.stackexchange.com/a/39148/31395
\newcommand{\adjustimg}{% Horizontal adjustment of image
  \ifodd\value{page}\hspace*{\dimexpr\evensidemargin-\oddsidemargin}\else\hspace*{-\dimexpr\evensidemargin-\oddsidemargin}\fi%
}
\newcommand{\centerimg}[2][width=\textwidth]{% Center an image
  \makebox[\textwidth]{\adjustimg\includegraphics[#1]{#2}}%
}

\begin{document}
    \blindtext[10]
    \begin{figure}
     

No comments:

Post a Comment