[Tex/LaTex] Exact centering of full-page background image

backgroundsgeometrymemoirpositioning

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}

\AddToShipoutPicture{\ifthenelse{\isodd{\thepage}}%
    {\bgright}%
    {\bgleft}
    }

\lipsum[1-15]

\end{document}

Best Answer

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}