# How can I align background image and notebook frame?

I'm trying to make this notebook style document but I can't align background image and notebook frame. I tried everything but I can't find the solution, and that slightly unaligment is driving me crazy.

Also, I would like to add different quotes on specific pages (even pages for example), but I dont know how to do that, but my main problem is aligment of backgorund image and notebook.

\documentclass{book}
\usepackage[%%
textwidth=\dimexpr4\paperwidth/5\relax,
inner=\dimexpr\paperwidth/9\relax,
textheight=\dimexpr2\paperheight/3\relax,
bottom=\dimexpr2\paperheight/7\relax,
a4paper
]{geometry}
\usepackage[angle=0,scale=20,opacity=1,color=black]{background}
\usepackage[utf8]{inputenc}

\graphicspath{ {images/} }

% I've create a boolean to control the presence of the diamond (and subsequently
% where the page numbers are placed.  If "true", the diamond will be rendered and
% the page numbers will appear in the diamond.  If "false", the diamond will not
% be rendered and the page numbers will appear on the bottom of the page.  See
% the code where I call the booleans to modify this behavior.  Overall, I've tried
% to divorce the formatting for rules and diamonds from the placement of the diamond.
\newif\ifusediamond
\usediamondfalse
\usediamondtrue

%% load the packages and libraries for creating the notebook pages
\usepackage{tikzpagenodes}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{calc}

%% create the styles for the nodes
\tikzset{my page number format/.style={font=\small\itshape},
dark black lines/.style={line width=0.5pt,black!20},
dark black lines/.style={line width=0.5pt,black!80},
dark black lines/.style={line width=0.5pt,black},
header using a diamond format/.style={diamond,draw,my page number format,inner sep=0pt,minimum size=1.3cm},
}

%% set up lengths to set up the environment.  Need these lengths because
%% everything has to be set up prior to the first page being shipped out.
%% That is, the ruled page is created before TikZ has any page nodes/coordinates
%% to work with.
\newlength{\myruledpagewidth}
\newlength{\myruledpageheight}
\newlength{\myruledpageheightl}
\setlength{\myruledpagewidth}{\textwidth}
\setlength{\myruledpageheight}{\textheight}
\setlength{\myruledpageheightl}{25cm}
%% Here's the primary macro which generates the actual ruled paper.
%% It takes two argument which are directions: {east}{west} or {west}{east}
\def\aeRuledPage#1#2{%%
\begin{tikzpicture}
\coordinate                  (my top west)     at (0,0);
\coordinate                  (my top east)    at (\myruledpagewidth,0);
\coordinate                  (my bottom west)  at (0,-\myruledpageheightl);
\coordinate                  (my bottom east) at (\myruledpagewidth,-\myruledpageheightl);

%% by setting the bounding box, it'll be easier to place the ruled page
%% later and not worry about things (like possibly the diamond) which
%% would change the dimensions of the bounding box.
\path[use as bounding box] (my top west) rectangle (my bottom east);

%% The following two lines control how the "dia" node is rendered.
\ifusediamond
\node[header using a diamond format] (dia)  at (my top #1) {};

\else
\node[header without diamond format] (dia)  at (my top #1) {};

\fi
\coordinate                  (tail) at (my top #2);

%% diamond is rendered (if at all)
\draw[dark black lines]    (dia.south)  -- (my bottom #1);
\draw[dark black lines]   (tail.south) -- (my bottom #2);
\draw[dark black lines] (dia.#2)     -- (tail);

\foreach \x in {0,1,...,26} {%%
\draw[dark black lines] ($(my bottom east)+(0,\x*0.9cm)$)
--
($(my bottom west)+(0,\x*0.9cm)$);

}
\end{tikzpicture}%%
}

%% Here I set up the boxes that will greatly increase the speed for
%% compiling this document.
\makeatletter
\def\ae@ruled@page#1#2{\aeRuledPage{#1}{#2}}

\newsavebox{\ruledpage@south@east}
\begin{lrbox}{\ruledpage@south@east}
\ae@ruled@page{west}{east}%%
\end{lrbox}

\newsavebox{\ruledpage@south@west}
\begin{lrbox}{\ruledpage@south@west}
\ae@ruled@page{east}{west}%%
\end{lrbox}

%% I've created separate macro for the ruled page and the page numbering
%% to make it a bit easier to change either without effecting the other.
\def\aePlaceRuledPage#1#2{%
\begin{tikzpicture}[remember picture,overlay]
\node[anchor=north #2,inner sep=0pt] at (current page header area.#1 #2) { \usebox{\csname ruledpage@#1@#2\endcsname}};

\end{tikzpicture}}

\def\aePlacePageNumber#1{%%
\begin{tikzpicture}[remember picture,overlay]
\ifusediamond
\node[my page number format] at (current page header area.#1) {\thepage};
\else
\node[my page number format] at (current page text area.#1) {\thepage};
\fi
\end{tikzpicture}}

\makeatother

%% Setting up the header and footers
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}

%% These next three lines control the position of the page number
\ifusediamond
%% page number on either the left or right hand side of the top
\else
%% page number in the bottom, centered
\fancyfoot[C]{\aePlacePageNumber{south}}
\fi

\fancyfoot[RE,LO]{} % other info in "inner" position of footer line

\usepackage[utf8]{inputenc}

\backgroundsetup{%
contents={%
\begin{tikzpicture}
\pgfmathsetmacro{\myopacity}{0.15}
\node[opacity=\myopacity] {\includegraphics[scale=0.025]{aaa.jpg}};
\end{tikzpicture}
}
}

\begin{document}

\foreach \y in {1, 2, ..., 50} { \makebox{}\clearpage}

\end{document}

I should point out that this is for personale use only, and that notebook style code I found at this link: This is the link where I found notebook code, but i slightly edited it

Here is the outpot, both left and right page:

• This document won't compile for me, even when I change the image name. ERROR: Package PGF Math Error: Unknown function right' (in 'right'). – Piet van Oostrum Dec 9 '16 at 22:13
• sorry about that, I fixed that now, it should compile now – DataJayden Dec 9 '16 at 22:49
• What is misaligned exactly? – cfr Dec 10 '16 at 3:07
• Well you can see that the frame is shifted a bit because of margine and printing, but background image stays at the center of the paper, so there is more space on one side between a vertical line and bottom corner of the picture. @cfr – DataJayden Dec 10 '16 at 10:57
• Does fancyhdr give you any warnings? That would be my first guess, anyway. – cfr Dec 10 '16 at 16:56

I guess the alignment problem is that the picture is placed differently with respect to the vertical notebook lines on even and odd pages.

You can change the position of the image with the hshift and vshift options in the \backgroundsetup macro. You have to be careful, because you specified scale=20, so you must divide all required shifts by 20. If you want the shift to be different on odd and even pages you can use the ifthen package to test the page number and use different backgroundsetup calls on even and odd pages. You have to put it in the preamble then inside an \AddEverypageHook macro, as described in the background package documentation.

Here is an example.

\documentclass{book}
\usepackage[%%
textwidth=\dimexpr4\paperwidth/5\relax,
inner=\dimexpr\paperwidth/9\relax,
textheight=\dimexpr2\paperheight/3\relax,
bottom=\dimexpr2\paperheight/7\relax,
a4paper
]{geometry}
\usepackage[angle=0,scale=20,opacity=1,color=black, contents={}]{background}
\usepackage{ifthen}

\ifthenelse{\isodd{\value{page}}}%
{\backgroundsetup{%
hshift=0.2mm,contents={%
\begin{tikzpicture}
\pgfmathsetmacro{\myopacity}{0.15}
\node[opacity=\myopacity] {\fbox{\includegraphics[scale=0.025]{example-image-a}}};
\end{tikzpicture}%
}%
}%
}
{\backgroundsetup{%
hshift=-0.2mm,contents={%
\begin{tikzpicture}
\pgfmathsetmacro{\myopacity}{0.15}
\node[opacity=\myopacity] {\fbox{\includegraphics[scale=0.025]{example-image-b}}};
\end{tikzpicture}%
}%
}%
}%
\BgMaterial}

\usepackage[utf8]{inputenc}

\graphicspath{ {images/} }

% I've create a boolean to control the presence of the diamond (and subsequently
% where the page numbers are placed.  If "true", the diamond will be rendered and
% the page numbers will appear in the diamond.  If "false", the diamond will not
% be rendered and the page numbers will appear on the bottom of the page.  See
% the code where I call the booleans to modify this behavior.  Overall, I've tried
% to divorce the formatting for rules and diamonds from the placement of the diamond.
\newif\ifusediamond
\usediamondfalse
\usediamondtrue

%% load the packages and libraries for creating the notebook pages
\usepackage{tikzpagenodes}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{calc}

%% create the styles for the nodes
\tikzset{my page number format/.style={font=\small\itshape},
dark black lines/.style={line width=0.5pt,black!20},
dark black lines/.style={line width=0.5pt,black!80},
dark black lines/.style={line width=0.5pt,black},
header using a diamond format/.style={diamond,draw,my page number format,inner sep=0pt,minimum size=1.3cm},
}

%% set up lengths to set up the environment.  Need these lengths because
%% everything has to be set up prior to the first page being shipped out.
%% That is, the ruled page is created before TikZ has any page nodes/coordinates
%% to work with.
\newlength{\myruledpagewidth}
\newlength{\myruledpageheight}
\newlength{\myruledpageheightl}
\setlength{\myruledpagewidth}{\textwidth}
\setlength{\myruledpageheight}{\textheight}
\setlength{\myruledpageheightl}{25cm}
%% Here's the primary macro which generates the actual ruled paper.
%% It takes two argument which are directions: {east}{west} or {west}{east}
\def\aeRuledPage#1#2{%%
\begin{tikzpicture}
\coordinate                  (my top west)     at (0,0);
\coordinate                  (my top east)    at (\myruledpagewidth,0);
\coordinate                  (my bottom west)  at (0,-\myruledpageheightl);
\coordinate                  (my bottom east) at (\myruledpagewidth,-\myruledpageheightl);

%% by setting the bounding box, it'll be easier to place the ruled page
%% later and not worry about things (like possibly the diamond) which
%% would change the dimensions of the bounding box.
\path[use as bounding box] (my top west) rectangle (my bottom east);

%% The following two lines control how the "dia" node is rendered.
\ifusediamond
\node[header using a diamond format] (dia)  at (my top #1) {};

\else
\node[header without diamond format] (dia)  at (my top #1) {};

\fi
\coordinate                  (tail) at (my top #2);

%% diamond is rendered (if at all)
\draw[dark black lines]    (dia.south)  -- (my bottom #1);
\draw[dark black lines]   (tail.south) -- (my bottom #2);
\draw[dark black lines] (dia.#2)     -- (tail);

\foreach \x in {0,1,...,26} {%%
\draw[dark black lines] ($(my bottom east)+(0,\x*0.9cm)$)
--
($(my bottom west)+(0,\x*0.9cm)$);

}
\end{tikzpicture}%%
}

%% Here I set up the boxes that will greatly increase the speed for
%% compiling this document.
\makeatletter
\def\ae@ruled@page#1#2{\aeRuledPage{#1}{#2}}

\newsavebox{\ruledpage@south@east}
\begin{lrbox}{\ruledpage@south@east}
\ae@ruled@page{west}{east}%%
\end{lrbox}

\newsavebox{\ruledpage@south@west}
\begin{lrbox}{\ruledpage@south@west}
\ae@ruled@page{east}{west}%%
\end{lrbox}

%% I've created separate macro for the ruled page and the page numbering
%% to make it a bit easier to change either without effecting the other.
\def\aePlaceRuledPage#1#2{%
\begin{tikzpicture}[remember picture,overlay]
\node[anchor=north #2,inner sep=0pt] at (current page header area.#1 #2) { \usebox{\csname ruledpage@#1@#2\endcsname}};

\end{tikzpicture}}

\def\aePlacePageNumber#1{%%
\begin{tikzpicture}[remember picture,overlay]
\ifusediamond
\node[my page number format] at (current page header area.#1) {\thepage};
\else
\node[my page number format] at (current page text area.#1) {\thepage};
\fi
\end{tikzpicture}}

\makeatother

%% Setting up the header and footers
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}

%% These next three lines control the position of the page number
\ifusediamond
%% page number on either the left or right hand side of the top
\else
%% page number in the bottom, centered
\fancyfoot[C]{\aePlacePageNumber{south}}
\fi

\fancyfoot[RE,LO]{} % other info in "inner" position of footer line

\usepackage[utf8]{inputenc}

\begin{document}

\foreach \y in {1, 2, ..., 3} { \makebox{}\clearpage}

\end{document}
• Thank you very much, that is exactly what i wanted, you sir are great. an thank you very much once again. @Piet van Oostrum – DataJayden Dec 11 '16 at 0:50