# Using opacity in tikz and the background package

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

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:

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!)

-
What do you mean "is hidden behind the rectangle"? Do you mean that the rectangle is no longer transparent? –  Loop Space Aug 26 '11 at 13:19
I've just tried it and I get what I would expect to get. Specifically, if I \SetBgColor{black}\SetBgOpacity{1}\SetBgAngle{0}\SetBgScale{1} then the backgrounded version is exactly the same as the non-backgrounded version (to my eyes). –  Loop Space Aug 26 '11 at 13:22
@Andrew, @Yossi: It works fine for me without setting any of those options, with transparency. I did have to use \SetBgContents (instead of \SetBgContent, which isn't defined for me). –  Jake Aug 26 '11 at 13:29
@Jake: To be clear: I only set those options so that I could compare exactly the backgrounded rectangle with the non-backgrounded one. Ditto on the \SetBgContent(s). @Yossi: Maybe you could post the full document and the image? –  Loop Space Aug 26 '11 at 13:35
Rewrote most of the question. Please reconsider your answer(s). –  Yossi Farjoun Aug 26 '11 at 15:21

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.

-
afterpage is only used if the \NoBgThispage command is invoked, which is not the case here. I still don't have really clear what's causing the problem (though I have some suspected culprit), but a simple \newpage (without the following text) is enough to solve it, as you've correctly found out. –  Gonzalo Medina Aug 26 '11 at 21:22
hmmm. I'm trying to generate a one-page document...this solution is just as much of a hack as my \phantoms! Good detective work though! Thanks for the pointer on removing the first \phantom...I was sure there's a better thing to do...I'm a little rusty. –  Yossi Farjoun Aug 26 '11 at 21:47
@Yossi Farjoun: as I explained in my comment, you only need \newpage without any additional contents so you still can obtain a one-page document. –  Gonzalo Medina Aug 26 '11 at 21:55
Oh, ok. Thanks!! –  Yossi Farjoun Aug 26 '11 at 22:25