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 <https://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}
\vspace{-\voffset} \vspace{-1in} \vspace{-\headsep} \vspace{-\topmargin} \vspace{-\headheight}
\centerimg[width=\paperwidth,height=\paperheight]{my_image.jpg}
\end{figure}
\blindtext[10]
\end{document}
This seemed to get the job done, but then I stuck it back into my original document, which has headers and footers. Add the following header/footer code to the preamble:
\makepagestyle{nstyle}
\makeevenhead {nstyle}{HEADER TEST LEFT}{MID}{RIGHT}
\makeoddhead {nstyle}{HEADER TEST LEFT}{MID}{RIGHT}
\makeevenfoot {nstyle}{FOOTER TEST LEFT}{MID}{RIGHT}
\makeoddfoot {nstyle}{FOOTER TEST LEFT}{MID}{RIGHT}
\pagestyle{nstyle}
This gives me the strange result that the headers are hidden (presumably behind the image?), but the footers display over the image. I don't understand why this would happen, but either way, it's a deal-breaker for me. This entire setup (what with the long series of \vspace
and so forth) is extraordinarily inelegant anyway, so I assume this must not be a good way of approaching the problem anyway.
So at this point, I'm kind of at a loss for how to approach this problem. (I'm a total novice at latex, though, so I wouldn't be surprised if there's a straightforward solution I just don't know about.)
Note: I'm not sure if this matters, but I'm using xetex
to compile (I need foreign script support).
EDIT: It seems likely to me that the best approach to solving this problem is to use \includegraphics
inside a figure
environment (i.e. some variant of the third approach I attempted above). The issues I'm facing are:
I can't figure out how to remove headers/footers from the page that the
figure
ends up on. Or, alternatively, I can't figure out how to make the image appear "above" the footer (in the z-direction).I don't have a clean/elegant way of getting the graphic to occupy the entire page. If there isn't a clean/elegant way, so be it, though I do hope there's something better than throwing a series of 5
\vspace
s at the problem.
(Note: I think my example above with the picture of the tree might have been misleading - the images I'm actually working with don't have a transparent or white background, and so it is important that there be no whitespace surrounding them. Ordinarily I'd deal with this using pdfpages
, but I haven't found a way to get pdf pages to float properly.)
\includegraphics
within afigure
environment? And welcome to TeX.SX!.\centerimg
command is just a wrapper around\includegraphics
). Anyway, I tried doing this with just\includegraphics
instead of the\centerimg
macro; this has the same issues with the footer appearing over the image, plus it requires me to futz with horizontal spacing so that there isn't whitespace in the left margin.