Using opacity in tikz and the background package

3,751

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 '[email protected]' 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 '[email protected]' is unknown

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

Share:
3,751

Related videos on Youtube

Yossi Farjoun
Author by

Yossi Farjoun

Updated on August 27, 2020

Comments

  • Yossi Farjoun
    Yossi Farjoun about 3 years

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

    • Andrew Stacey
      Andrew Stacey about 12 years
      What do you mean "is hidden behind the rectangle"? Do you mean that the rectangle is no longer transparent?
    • Andrew Stacey
      Andrew Stacey about 12 years
      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).
    • Jake
      Jake about 12 years
      @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).
    • Andrew Stacey
      Andrew Stacey about 12 years
      @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?
    • Yossi Farjoun
      Yossi Farjoun about 12 years
      Rewrote most of the question. Please reconsider your answer(s).
  • Gonzalo Medina
    Gonzalo Medina about 12 years
    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.
  • Yossi Farjoun
    Yossi Farjoun about 12 years
    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.
  • Gonzalo Medina
    Gonzalo Medina about 12 years
    @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.