0

I have a sequence of images, img1.png, img2.png, img3.png, ... I would like img1.png to appear in the background of page 1 of my LaTeX document, and img2.png to appear in the background of page 2 of my document, and similarly for img3.png and so on.

I have more images than pages. There is also a cover page and table of contents, which I am ignoring for the sake of argument. Note that these images are all different, and in particular they do not repeat e.g. with every odd or every even page.

Any ideas? Thank you.

2
  • 1
    The eso-pic package combined with a test on say the page counter (assuming it is never reset)
    – daleif
    Jul 18, 2019 at 16:30
  • Thank you. In principle of course I understand but I'm not familiar with the relevant concrete syntax and wasn't able to find any examples online. It would be helpful if you could add a concrete example.
    – Jim
    Jul 18, 2019 at 18:04

1 Answer 1

3

Welcome to TeX.SE! With the following code, you can define the image sequence wherever you want (even change it mid-document) like so:

\setimages{example-image-a, example-image-b, example-image-c}

It has wrap-around behavior if you have more pages than images in the image sequence (I know you didn't ask for it, but that was a rather cheap addition). You probably won't care, but \setimages respects TeX grouping too.

\documentclass{article}
\usepackage{eso-pic}
\usepackage{graphicx}
\usepackage{xparse}
\usepackage{lipsum} % provides dummy text for demonstration purposes

\ExplSyntaxOn

\seq_new:N \g_jim_images_seq

\cs_new_protected:Npn \jim_use_as_bg_picture:n #1
  {
    \AtPageLowerLeft
      { \includegraphics[width=\paperwidth, height=\paperheight] {#1} }
  }

\cs_generate_variant:Nn \jim_use_as_bg_picture:n { x }

\NewDocumentCommand \setimages { m }
  { \seq_set_from_clist:Nn \g_jim_images_seq {#1} }

\NewDocumentCommand \setbgpicforcurrentpage { }
  {
    % Compute wrapped-around 0-based index in \g_jim_images_seq
    \int_set:Nn \l_tmpa_int
      { \int_mod:nn { \the\value{page} - 1 } { \seq_count:N \g_jim_images_seq } }
    % Set it as the current page background
    \jim_use_as_bg_picture:x
      { \seq_item:Nn \g_jim_images_seq { \l_tmpa_int + 1 } }
  }

\ExplSyntaxOff

\AddToShipoutPictureBG{\setbgpicforcurrentpage}

% Use example-image-a for page 1, example-image-b for page 2, example-image-c
% for page 3, etc. This respects TeX grouping and can be reset mid-document.
\setimages{example-image-a, example-image-b, example-image-c}

\begin{document}
% Current image sequence: a, b, c, a, b, c, a ...
\lipsum
\lipsum
\lipsum
\lipsum
\lipsum
% Change the image sequence to: a, b, a, b, a, b, a...
\setimages{example-image-a, example-image-b}%
\lipsum
\lipsum
\lipsum
\end{document}

All pages

2
  • Thank you. This is very helpful.
    – Jim
    Jul 19, 2019 at 7:37
  • My pleasure. :-)
    – frougon
    Jul 19, 2019 at 7:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .