8

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 \vspaces 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.)

  • Why don't you include the picture with \includegraphics within a figure environment? And welcome to TeX.SX!. – mafp May 28 '13 at 17:07
  • @mafp I think this is essentially what I did in the section labeled third (the \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. – senshin May 28 '13 at 17:18
  • In your example, the image appears before the rubric (red mark). Is that what you meant? To have the picture possibly before or possibly after the rubric, depending on the location of the rubric on the page? – Steven B. Segletes May 28 '13 at 17:19
  • @StevenB.Segletes Yes, that is what I mean. Ideally, the picture would appear before the page if the mark was in the first half of the page and after the page if the mark was in the second half of the page, but I'd be okay with a solution that e.g. always puts the picture after the page irrespective of the location of the mark on the page. – senshin May 28 '13 at 17:25
8

My immediate thought here is to use \afterpage. This should result in a placement one page after it's mentioned:

enter image description here

\documentclass{article}
\usepackage{graphicx,lipsum,afterpage}% http://ctan.org/pkg/{graphicx,lipsum,afterpage}
\begin{document}
\lipsum[1-20]
\afterpage{
  \thispagestyle{empty}\addtocounter{page}{-1}%
  \noindent\includegraphics[width=\textwidth,height=\textheight]{example-image-a}%
}
\lipsum[21-40]
\end{document}

The above shows pages 3 and 4 (top), with the image inserted about two-thirds from the top on page 3. \afterpage then inserts a header/footer-less page (\thispagestyle{empty}) with the image stretched to the text block (width=\textwidth,height=\textheight) on a subsequent page. Adding \addtocounter{page}{-1} steps back the page counter so that the page seems to be literally inserted independently from the document flow (and may not be what you're after entirely, but something similar to \includepdf from pdfpages).

As far as I know, this use (with afterpage) should be free from problems with other unprocessed floats since it doesn't interface with the algorithm of float placement.

Of course, the image can also be made to fill the entire page. The answer is merely geared towards showcase the possibility.

  • This is a helpful answer, but could you elaborate on how the image could be made to fill the entire page? I tried putting \newgeometry{hmargin=0,vmargin=0} in the beginning of the \afterpage command, but that caused all sorts of new problems, and I don't really have any better ideas. – senshin May 29 '13 at 0:52
  • 1
    I managed to adapt your solution to my needs by replacing the line containing \includegraphics with \includepdf[width=\paperwidth]{my_image.pdf}, so I'm going to accept this answer. – senshin Jun 2 '13 at 21:02
  • 1
    @senshin: That's great. I was thinking about stretching the image internally, but it's really not necessary. So you're approach with \includepdf is perfect. – Werner Jun 2 '13 at 21:40
2

Is this what you mean? I'm not sure (it's just using a float). The figure will appear after the rubric, which I have indicated with a red capital R. UPDATE, when I do as the comment indicates, my float works out just as I expect, on the page following the rubric.

Thanks to barbare beeton for the pointer on getting rid of a page number on the float page. I used (I think) a variation of what was proposed there.

\documentclass{article}
\usepackage{graphicx}
\usepackage{color}
\usepackage{lipsum}
\usepackage{mwe}
\usepackage{afterpage}
\let\svthepage\thepage
\newcommand\rubric[1]{%
  \textcolor{red}{R}%
  \def\thepage{\svthepage\thispagestyle{empty}}%
  \begin{figure}[p]%
  \includegraphics{#1}%
  \end{figure}%
  \afterpage{\global\def\thepage{\svthepage\let\thepage\svthepage}}%
}
\begin{document}
\lipsum[1-20]
\rubric{example-image-a}
\lipsum[21-40]
\end{document}

enter image description here

  • To see how this was working, I replaced [1-3] with [1-20] and [4-10] with [21-40] (to get more lipsum text). When I compile this, I see the red "R" displayed in the middle of some text on page 4. The example-image-a is displayed on page 9. A solution to my problem would display the image either one page before or one page after what is currently rendered as page 4, so I don't think this answers my question. (Note: when I keep the lipsum settings as you have them, the "R" appears on page 1, and the image appears on page 3, so the same issue arises there.) – senshin May 28 '13 at 18:05
  • @senshin I cannot recreate your behavior cited above. I've edited to show updated result, using your parameters. – Steven B. Segletes May 28 '13 at 19:03
  • 1
  • Other floats that are unprocessed at the time of reaching \rubric may influence the placement of the figure. – Werner May 28 '13 at 19:24
  • @senshin FYI, I am using PDFTeX or PDFLaTeX to get this result. – Steven B. Segletes May 28 '13 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.