Using a tikzpicture disables opacity for first \BgThisPage. How to fix this

tikz-pgftransparency

In a book PDF, I am using various images as the background on several pages. On some of these background-image pages, I want the image to be semi-transparent; therefore I am using an opacity setting in the background page setup options. Each background image page was rendering fine … until I decided to do a cover art for the book on the first page. I simply wanted a gradient background, behind an image that has an alpha channel. I used the following tikzpicture code to do this:


\begin{tikzpicture}[remember picture, overlay]
\path [left color = black,middle color = black!80, right color = gray!20] (current page.south west)rectangle (current page.north east);  
\node[inner sep=0pt] (russell) at (10,-13)
    {\includegraphics[width=\paperwidth, height=\paperheight]{some_image_with_transparent_background_here.png}}; 
\end{tikzpicture}%

This code works fine for the first cover page – but seems to mess up the first image-background page (which happens several pages over from the cover page). On the first background-image page, it does not honor the 0.10% opacity setting; instead the page renders fully opaque. When I remove the code above, the opacity setting works for the background-image page, renders semi-transparent as I want … When I place the code above back into the document, the first background-image page renders fully opaque again, not honoring the desired 0.10% opacity setting. What is weird, the code above ONLY affects the fist background-image page. The second, third, forth, etc, render properly according to settings given.

If it matters, I am using XeLaTeX, and I think I am locked into that with all the packages that I am using.

What am I doing wrong ? I know just enough tikz to get me into trouble. I cobbled this together from examples on the web.

A MWE, with both the cover art first page, and the background-image page is giving in the code below:



\documentclass[a4paper,12pt,twoside,]{book}
\usepackage[pages=some]{background}


\begin{document}

\newpage

% the PDF cover page

\begin{tikzpicture}[remember picture, overlay]
\path [left color = black,middle color = black!80, right color = gray!20] (current page.south west)rectangle (current page.north east);  
\node[inner sep=0pt] (russell) at (10,-13)
    {\includegraphics[width=\paperwidth, height=\paperheight]{some_image_with_transparent_background_here.png}}; 
\end{tikzpicture}%




\newpage
\thispagestyle{empty} 

% first page that uses a background image after the cover page 

\backgroundsetup{
scale=1,
placement=center,
color=black,
opacity=0.10,
angle=0,
contents={%
\includegraphics[width=\paperwidth]{some_image_here.png}}%
}



\BgThispage 

Why is background image on this page not honoring the 0.10 percent opacity setting when the tikzpicture code is used on the first page?  Yet it does honor the opacity setting when the tikzpicture code is removed on the first page ?   

Also, I note that ONLY this first page is the page that does not honor the opacity setting.  Any backgrounded page after this one returns to proper rendering with opacity settings honored. If you repeat this page for the next page, the desired opacity settings will render properly !  

\end{document}

Best Answer

Perhaps not an elegant solution ... but a work solution nonetheless:

On the cover page, add a dummy background setup, with a dummy call to \BgThisPage and follow that dummy call with nothing at all.

\BgThisPage will not really render anything on this page since SOMETHING needs to be on the page for the background to render.

With this dummy call in play, then the NEXT TIME you really do want something backgrounded, it's a second call to \BgThisPage ... and the opacity settings are honored.

Perhaps not a proper fix, but this does work. I'm still waiting for something better to come along, but thought I'd share that just incase nothing is added to this post.

Here's the code that worked for me:



\documentclass[a4paper,12pt,twoside,]{book}
\usepackage[pages=some]{background}


\begin{document}

\newpage

% the PDF cover page

\begin{tikzpicture}[remember picture, overlay]
\path [left color = black,middle color = black!80, right color = gray!20] (current page.south west)rectangle (current page.north east);  
\node[inner sep=0pt] (russell) at (10,-13)
    {\includegraphics[width=\paperwidth, height=\paperheight]{some_image_with_transparent_background_here.png}}; 
\end{tikzpicture}%


%%
% Make a dummy call to \BgThisPage
%%%%%

\backgroundsetup{
scale=1,
placement=center,
color=black,
opacity=0.10,
angle=0,
contents={%
\includegraphics[width=\paperwidth]{some_image_here.png}}%
}


\BgThispage 


\newpage
\thispagestyle{empty} 

% first page that uses a background image after the cover page 
% but now, this is the second call to \BgThisPage ... which 
% seems to have flushed whatever was messing up opacity setting
% not being honored.  This call will render at 0.10 transparent

\backgroundsetup{
scale=1,
placement=center,
color=black,
opacity=0.10,
angle=0,
contents={%
\includegraphics[width=\paperwidth]{some_image_here.png}}%
}



\BgThispage 

The image now renders as desired with semi-transparent settings honored. 


\end{document}