Page LayoutHow to alternate background picture with eso-pic ?

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

How to alternate background picture with eso-pic ?

Post by Cham »

I would like to use three different background textures, alternating on all the pages of the document. I'm currently using the eso-pic package, which works great, but I'm having only one background texture for all the pages.

How can we tell LaTeX to use texture1 on page1, texture2 on page2, texture3 on page 3, then texture1 on page 4, texture2 on page 5, and so on ?

The commands I'm currently using are these :

Code: Select all

\usepackage{eso-pic}

\begin{document}
\AddToShipoutPicture{\put(0,0){\includegraphics[width=\paperwidth,height=\paperheight]{texture1.jpg}}}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10324
Joined: Mon Mar 10, 2008 9:44 pm

How to alternate background picture with eso-pic ?

Post by Stefan Kottwitz »

You could first call \ClearShipoutPicture and then \AddToShipoutPicture with the next texture. Either manually, or make a macro for it. For automatizing, that macro could go to a header or footer command.

Well, that's an "outer" approach. You could make an if-then choice within \AddToShipoutPicture{...} itself. Instead of the \put, decide which picture will be put, depending on the page number (modulo 3).

However, bevor you make such work, I recommend to think about if it's really necessary. I imagine it would be strange for me, always changing patterns, instead of some homogeneity. Even more, if it's not left/right alternating but in triples.

Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

How to alternate background picture with eso-pic ?

Post by Cham »

Thanks Stefan,

the idea with 3 (or 4) textures, is to give some subtle variety. The textures are old paper with dirts and scratches, and the project is for a personal travel journal, with text and pictures. I want to give the impression that each page is unique.

However, I may revert to a simpler idea : two textures only, one for the left page, and the second texture for the right page.

How this should be done, with the eso-pic package ? The documentation isn't very helpfull to me.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10324
Joined: Mon Mar 10, 2008 9:44 pm

How to alternate background picture with eso-pic ?

Post by Stefan Kottwitz »

In pseudo code, not yet compilable, to show a possibility, just choose a right if-command and syntax:

Code: Select all

\AddToShipoutPicture{
  \ifisodd{page}
    \put(0,0){\includegraphics[width=\paperwidth,height=\paperheight]{texture1.jpg}}
  else
    \put(0,0){\includegraphics[width=\paperwidth,height=\paperheight]{texture2.jpg}}
}
Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

How to alternate background picture with eso-pic ?

Post by Cham »

Hmmm, it doesn't work well, I'm getting several "Undefined control sequence".

Something is missing in the preamble. In the output, I get an offset texture, and the word "pageelse" is printed at the bottom.

EDIT : Should I load another package, for the "if then else" command ?
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

How to alternate background picture with eso-pic ?

Post by Cham »

Here's a MWE (not working without textures) :

Code: Select all

\documentclass[12pt,letterpaper,twoside]{book}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{graphicx}
\usepackage{eso-pic}

\begin{document}

\AddToShipoutPicture{\put(0,0){\includegraphics[width=\paperwidth,height=\paperheight]{cover.jpg}}}
\thispagestyle{empty}
\cleardoublepage
\phantomsection
\ClearShipoutPicture

\AddToShipoutPicture{
	\ifisodd{page}
		\put(0,0){\includegraphics[width=\paperwidth,height=\paperheight]{texture1.jpg}}
	else
		\put(0,0){\includegraphics[width=\paperwidth,height=\paperheight]{texture2.jpg}
	}
}

\chapter{Title}
Bla bla bla
\end{document}

Last edited by Cham on Sat Jul 26, 2014 4:45 pm, edited 1 time in total.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10324
Joined: Mon Mar 10, 2008 9:44 pm

How to alternate background picture with eso-pic ?

Post by Stefan Kottwitz »

That's not an MWE. Please don't put together untested code, this is not compilable, there are errors, such as missing hyperref package and capitalization in \chapter. Please always make a test run before posting.

Sure, you need to find something for the "if". Either TeX' \isodd, or the ifthen package, or toolbox. Here's a way with the changepage package. I just used different sizes to see the changing picture without an actual image.

Code: Select all

\documentclass[12pt,letterpaper,twoside]{book}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[demo]{graphicx}
\usepackage{eso-pic}
\usepackage{ifthen}
\usepackage{hyperref}
\usepackage{changepage}
\strictpagecheck
\begin{document}

\AddToShipoutPicture{\put(0,0){\includegraphics[width=\paperwidth,height=\paperheight]{cover.jpg}}}
\thispagestyle{empty}
\cleardoublepage
\phantomsection
\ClearShipoutPicture
\AddToShipoutPicture{%
  \checkoddpage
  \ifoddpage
    \put(0,0){\includegraphics[width=.5\paperwidth,height=.5\paperheight]{texture1.jpg}}
  \else
    \put(0,0){\includegraphics[width=.9\paperwidth,height=.9\paperheight]{texture2.jpg}}
  \fi
}

\chapter{Title}
text
\newpage
text
\newpage
text
\newpage
text
\newpage
text
\newpage
\end{document}
Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

How to alternate background picture with eso-pic ?

Post by Cham »

Hmm, now it's working, but there's a slight horizontal offset of the textures ; there's a white vertical band on the left part of all the pages. Why ? I used this (the textures should cover all the page, including the margins) :

Code: Select all

\AddToShipoutPicture{
	\checkoddpage
	\ifoddpage
		\put(0,0){\includegraphics[width=\paperwidth,height=\paperheight]{texture1.jpg}}
	\else
		\put(0,0){\includegraphics[width=\paperwidth,height=\paperheight]{texture2.jpg}}
	\fi
	}

User avatar
Stefan Kottwitz
Site Admin
Posts: 10324
Joined: Mon Mar 10, 2008 9:44 pm

Re: How to alternate background picture with eso-pic ?

Post by Stefan Kottwitz »

In my code I used a percent sign % to comment out the following whitespace (line break, indentation in the code). Just do it the same, then you won't get that offset.

Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Re: How to alternate background picture with eso-pic ?

Post by Cham »

Hahaa ! Thanks a lot, Stefan. I'll never understand the logic behind that % after a command.

Now it's working perfectly.
Post Reply