[Tex/LaTex] Using opacity in tikz and the background package

backgroundstikz-pgftransparency

I'm trying to take a given figure, make it a little whiter and put the result in the middle of the page under the text, using the package background.

\documentclass[12pt]{article}
\usepackage{graphicx}
\usepackage{background}
\begin{document}

\SetBgContents{
\begin{tikzpicture}
    \node(0,0) {\includegraphics[scale=.1]{logo}};
    \node(0,0)[fill opacity=.5,fill=black]
     {\phantom{\includegraphics[scale=.1]{logo}}};
 \end{tikzpicture}
 }
\SetBgOpacity{1}\SetBgAngle{0}\SetBgScale{1}\SetBgPosition{current page.north}\SetBgAnchor{below}

text
\phantom{
\begin{tikzpicture}
    \node(0,0)[fill opacity=.5,fill=black]{a};
 \end{tikzpicture}}

\end{document}

Works for me and produces

enter image description here

What's all that stuff with \phantom? I hear you. But that's MY question! (the first phantom is to make my life easy so that the box will cover the image) If I remove the second phantom (and its argument of course) the transparency no longer works. I get the following:

enter image description here

That's right, no longer transparent! I have no idea why this is…Any help??

(As if things are not strange enough, the fill opacity have to be the same for the this to work, i.e. even with the \phantom, if the fill opacity is different, the image is hidden!)

Best Answer

I think it would need Gonzalo to explain why this is the case, but I can solve your problem with a \newpage command. It may be to do with the fact that the background package uses the afterpage one internally and maybe that doesn't work perfectly if there is only one page.

I spotted this because my initial example used the lipsum package to generate the text and the default goes over on to two pages. In slowly modifying your example to mine, then the strange behaviour came when I removed the lipsum, which seemed absolutely daft! How can lipsum fix this? Then I figured that it was the number of pages that was important. So here's a working example:

\documentclass[12pt]{article}
\usepackage{graphicx}
\usepackage{background}

\SetBgOpacity{1}
\SetBgAngle{0}
\SetBgScale{1}
\SetBgPosition{current page.north}
\SetBgAnchor{below}

\SetBgContents{
\begin{tikzpicture}
    \node (a) at (0,0) {\includegraphics[scale=.1]{Htree}};
    \filldraw[fill opacity=.5,fill=black] (a.north west) rectangle (a.south east);
 \end{tikzpicture}
 }
\begin{document}

text
\newpage
text

\end{document}

Note that this renders correctly on both pages.

Incidentally, I got rid of the first \phantom by labelling the node containing the graphic and drawing the rectangle over the top using the node anchors.

Without that \newpage, I get the filled-in rectangle. I also get the following errors from xpdf:

Error: ExtGState 'pgf@CA1' is unknown
Error: ExtGState 'pgf@ca1' is unknown
Error: ExtGState 'pgf@CA1' is unknown
Error: ExtGState 'pgf@ca1' is unknown
Error: ExtGState 'pgf@ca.5' is unknown
Error: ExtGState 'pgf@CA1' is unknown
Error: ExtGState 'pgf@ca1' is unknown
Error: ExtGState 'pgf@CA1' is unknown
Error: ExtGState 'pgf@ca1' is unknown
Error: ExtGState 'pgf@ca.5' is unknown

But when the document is at least 2 pages, I get everything as it should be and no errors.