# Different output xelatex and lualatex (tikzpagenodes, tcolorbox and background packages involved)

I am positioning an image on the top of the page of chapter openings. The image should fill all the page width. The code below is working fine with LuaLaTeX, but when I run it with XeLaTeX (it should be run 3 times to get proper positioning) the image is moved to the horizontal centre of the page. For other reasons, I have to run the document with XeLaTeX. What should I change in the code to run with XeLaTeX and obtain the output shown with LuaLaTeX?

EDIT Changed to use an example image. The header will change to respect the images show; but the effect of useing XeLaTeX is the same.

\documentclass{book}

%%% Include image in chapter opening
\RequirePackage{tikzpagenodes}
%%% fill rectangle with image uisng tcolorbox
%%% https://tex.stackexchange.com/a/219424/2483
\RequirePackage{tcolorbox}
\tcbuselibrary{skins}
\usetikzlibrary{calc}
%%% For the background
%%% https://tex.stackexchange.com/a/86702/2483
\usepackage[pages=some]{background}
\newlength{\bleendlength}
\setlength{\bleendlength}{5mm}
\newcommand{\chapimage}[1]{%
\backgroundsetup{scale=1,placement=top,contents={
\tikz[remember picture,overlay] {%
($(current page.north west)+(-\bleendlength, \bleendlength)$)
rectangle
($(current page.north east)+(\bleendlength, -6cm)$);
}
}
}
}

\begin{document}
\chapter{My first chapter}
\label{cha:my-first-chapter}
\chapimage{}\BgThispage

\end{document}


Correct output with LuaLaTeX:

Incorrect output with XeLaTeX:

• please make your example so that it can be tested on other computers. – Ulrike Fischer Aug 17 '20 at 14:40
• Sorry I forgot to attach the image file! – TeXtnik Aug 17 '20 at 14:45
• There is certainly no need for an external picture. Use one of the example picture e.g. from the mwe package. – Ulrike Fischer Aug 17 '20 at 14:57
• I added the use of an example image. – TeXtnik Aug 17 '20 at 14:59
• I wouldn't use the background package for tikz content, and certainly not for content involving page nodes. As muzimuzhi Z writes, the background material is inside a node in a tikzpicture and this very difficult to control. Use e.g. the eso-pic package instead, or the new shipout hooks of latex-dev. – Ulrike Fischer Aug 17 '20 at 15:48

The value passed to background's option contents (stored in \Background@Contents) is already typeset inside a tikzpicture. And nesting tikzpicture is an undefined behavior. See the implementation of \bg@material in background v2.1, 2014/03/04:

\newcommand\bg@material{%
\begin{tikzpicture}[remember picture,overlay,scale=\Background@Scale]
\node[
rotate=\Background@Angle,
scale=\Background@Scale,
opacity=\Background@Opacity,
anchor=\Background@NodeAnchor,
xshift=\Background@HShift,
yshift=\Background@VShift,
color=\Background@Color,
inner sep=0pt
]
at (\Background@Position) [\Background@Anchor]
{\Background@Contents};
\end{tikzpicture}}%


A brute attempt is to redefine \bg@material.

% this example produces the same output with pdftex, xetex, and luatex
\documentclass{book}
\usepackage{tcolorbox}
\tcbuselibrary{skins}
\usepackage[pages=some]{background}

\newlength{\bleendlength}
\setlength{\bleendlength}{5mm}

\makeatletter
\renewcommand\bg@material{%
\tikz[remember picture,overlay] {%
($(current page.north west)+(-\bleendlength, \bleendlength)$)
rectangle
($(current page.north east)+(\bleendlength, -6cm)$);
}%
}%
\makeatother

\begin{document}
\chapter{My first chapter}\label{cha:my-first-chapter}

\BgThispage
\end{document}


Nicer solutions range from better redefinition, using another background pacakge, to providing a special page style set by \chapter (suppose you want to have similar background effect per chapter title page).

• As you suggested I another background package (eso-pic), but I still have a similar error when using the crop package. See my question – TeXtnik Aug 18 '20 at 14:47