# Exact centering of full-page background image

Here's the situation:

1. I have to use memoir (v3.6j), with geometry (v5.6) and LuaTeX, version beta-0.70.1-2011061410 (rev 4277) in an A5 project which, in the final output have to be 3mm larger from any side, i.e., PDF dimensions should be 154x216mm. I use crop (v1.9) package for crop marks and setting the correct paper size.

2. The project has two background images, each for both left and right pages, which fit 154x216mm size. For this purpose, I have to use eso-pic (v2.0c) this time (though suggestions regarding background and wallpaper packages are most welcome).

3. Though I can manually tweak the position of those background images, I'd like to know if there is an automatic way to place them exactly centered regarding the page, if possible independent of changing paper dimensions with crop package (comment l.13, and uncomment l.14).

Here is a sample document code.

\documentclass[10pt,twoside]{memoir}

\DisemulatePackage{geometry}
\DisemulatePackage{crop}

\usepackage{geometry}
\geometry{%
papersize={148mm,210mm},%
hmargin={2cm,2cm},%
vmargin={2cm,2.2cm},
showframe}

\usepackage[width=15.4cm,height=21.6cm,center,cam]{crop}
%\usepackage[width=16.3cm,height=22.5cm,center,cam]{crop}

\usepackage{xcolor}

\newcommand{\bgleft}{{\color{yellow}\rule{154mm}{216mm}}}% Background for even pages

\newcommand{\bgright}{{\color{green}\rule{154mm}{216mm}}}% Background for odd pages

\usepackage{eso-pic}
\usepackage{ifthen}

\usepackage{lipsum}

\begin{document}

{\bgright}%
{\bgleft}
}

\lipsum[1-15]

\end{document}

-
+1 for a great MWE! –  doncherry Jan 28 '12 at 11:04
Thanks :-) So, just to make it clear, the point here is that background images should fit exactly 154x216mm page. Centering vertically and horizontally against the page dimensions set by geometry package would be ideal, since these dimensions are constant and do not change (with a note that background should be allowed to exceed margins and page boundaries), unlike paper dimensions set by crop package. –  Meho R. Jan 28 '12 at 13:17

Here's my shot at it without crop, geometry, or eso-pic.

I'm assuming you need the color to bleed over the trim, and therefore need the background underneath the crop marks (that's what the first, mystical line does).

Note the use of memoir dimensions. All you need to do to remove/add the trims is to update \setstocksize and \settrims

\RequirePackage{atbegshi}\AtBeginShipoutInit
\documentclass[10pt,twoside,showtrims]{memoir}
\setstocksize{216mm}{154mm}
\settrimmedsize{210mm}{148mm}{*}
\settrims{3mm}{3mm}
\setulmarginsandblock{20mm}{22mm}{*}
\setlrmarginsandblock{20mm}{20mm}{*}
\checkandfixthelayout
\trimLmarks
\usepackage{xcolor}
\newcommand{\bgleft}{{\color{yellow}\rule{\stockwidth}{\stockheight}}}% Background for even pages
\newcommand{\bgright}{{\color{green}\rule{\stockwidth}{\stockheight}}}% Background for odd pages
\usepackage{picture}
\usepackage{ifthen}
\usepackage{lipsum}
\begin{document}
\AtBeginShipout{%
\AtBeginShipoutUpperLeft{%
\ifthenelse{\isodd{\thepage}}%
{\put(0,-\stockheight){\bgright}}%
{\put(0,-\stockheight){\bgleft}}%
}%
}
\lipsum[1-15]
\end{document}


Following Meho's comments, I went back to the drawing-board, and here's the result. I haven't parameterised the image size, the trimmed size, nor yet the 3mm difference, but I believe this solution works (is correct?, even), for any arbitrary stock size and trimming geometry that ends up at A5 after trimming.

I have halved the size of the coloured backgrounds to help in testing. Their north-east corners correspond to the center point of Meho's images.

Uncomment the lines shown and tinker with the values there -- the coloured boxes should not move relative to the page (and text).

Here it is, then:

\RequirePackage{atbegshi}\AtBeginShipoutInit
\documentclass[10pt,twoside,draft,showtrims,a5paper]{memoir}
\settypeoutlayoutunit{mm}
\usepackage{lipsum}
\usepackage{ifthen}
\usepackage{picture}
\usepackage{xcolor}
\settrimmedsize{210mm}{148mm}{*}
% Un-comment to play with stock size and trimming arrangements
% \setstocksize{256mm}{184mm}
% \settrims{10mm}{10mm}
\setulmarginsandblock{20mm}{22mm}{*}
\setlrmarginsandblock{20mm}{30mm}{*}
\checkandfixthelayout
\trimLmarks
\newcommand{\bgleft}{{\color{yellow!50}\rule{77mm}{108mm}}}% Background for even pages
\newcommand{\bgright}{{\color{green!50}\rule{77mm}{108mm}}}% Background for odd pages
\newlength{\yboth}
\newlength{\xrecto}
\newlength{\xverso}
\yboth=\dimexpr\trimtop+\paperheight+3mm
\xrecto=\dimexpr\stockwidth-\trimedge-\paperwidth-3mm
\xverso=\dimexpr\trimedge-3mm
\AtBeginShipout{%
\AtBeginShipoutUpperLeft{%
\ifthenelse{\isodd{\thepage}}%
{\put(\xrecto,-\yboth){\bgright}}%
{\put(\xverso,-\yboth){\bgleft}}%
}%
}
\begin{document}
\lipsum[1-15]
\end{document}

-
Thanks a lot. This is very close to what I want to achieve. The only thing is that \bgleft and \bgright must not be resized: those will be images with fixed width and height, i.e., exactly 154x216mm, and should be centered always, regardless of changes in setstocksize. –  Meho R. Jan 29 '12 at 14:59
Not to forget, in case you decide to change your example, please don't remove the current code, just comment \bgleft, \bgright and \AtBeginShipout parts and add new ones: This is a great example as it is and it would be shame to remove any part of it. –  Meho R. Jan 29 '12 at 15:09
I'll accept it because I find both those examples excellent and only because of that ;-) Thanks a lot for your time, much appreciated. You gave me some material to study further, which is a nice bonus :-) –  Meho R. Jan 30 '12 at 10:56

I get exact positioning without crop:

\usepackage{geometry}
\geometry{%
papersize={154mm,216mm},
layoutsize={148mm,210mm},
hmargin={2cm,2cm},
vmargin={2cm,2.2cm}
}

\ifthenelse{\isodd{\thepage}}
{\kern-62mm\bgright}
{\bgleft}%
}


However, showcrop passed to \geometry does nothing, probably because \AddToShipoutPicture hooks after geometry.

There should be a computation that justifies the 62mm. :)

Wouldn't it be simpler to crop the two images right away from the start?

-
The point is, I have to deliver pure A5 format for ebook, but for printed version, it was requested of us that background must be +3mm on all sides (plus an even larger version including crop marks). I admit it's a strange request, but I shouldn't question our printer ;-) Of course, there is another approach: just set final dimensions in geometry, omit crop package completely, and crop PDF after it's compiled, but that isn't really an ideal long-term solution, since I encounter situations like the above quite often. –  Meho R. Jan 28 '12 at 13:10
BTW, those 62mm are caused for some reason by memoir class (try book class instead). So, the problem is partially related to using crop (larger paper size than page size set by geometry), and partially to memoir. –  Meho R. Jan 28 '12 at 13:22
I suspect you shouldn't need geometry, as memoir has its own, comprehensive layout facilities. Need a little time to play - if I get lucky, more later. –  Brent.Longborough Jan 28 '12 at 16:29
@egreg, thank you for your insights which (as always), if not solving a problem at once, lead to a better way of doing things. For non-memoir classes, pointing out to geometry's own features showcrop and layoutsize was crucial and the problem at hand is solved easily, even if done manually. So, again, a big thank you to both of you :-) –  Meho R. Jan 30 '12 at 12:08