3

I have a very similar problem mentioned here. But I like to put a tikz logo with transparency in the heading of the document using fancyhdr.

If I compile the example below by enabling any of the two options I get what I want. My question are:

  • Why do I need such a "workaround"?
  • If I need a workaround can I put it somehow in the tikz picture, or in the command that inserts the logo?

Thank You!

\documentclass[a4paper,12pt]{article}

\usepackage{fancyhdr}
\usepackage{tikz}

\setlength\headheight{ 1.5  cm}

\pagestyle{fancy}

\fancyhf{}
\rhead{\insertlogo}

\newcommand{\insertlogo}{%
  \begin{tikzpicture}[y = 0.1cm, x = 0.1cm]%
    \fill[fill=green, rounded corners=1] (1,2) rectangle +(20,7);
    \fill[fill=red, rounded corners=1, opacity=0.3] (-1,2) rectangle +(24,7);
  \end{tikzpicture}%
}%

\begin{document}

A
% Option 1: the logo is inserted also in the text -> it is OK 
%\insertlogo

% Option 2: the document has two or more pages -> it is OK again
%\newpage

\end{document}

EDIT:

@percusse: Thank You for your solution. It works nice but to tell you the whole story. I like to make the colors of the logo configurable depending on the second argument (b - black and white, c - color, s - shaded) of the "insertlogo" command further more I like to make it scale-able (first argument), I provided only a minimum working example but in this case I like to extend it. Can I use your solution in this case?

Thanks again!

\newcommand{\insertlogo}[2]{%
% check if black and white selected

\definecolor{logowhite}{RGB}{255,255,255}
\definecolor{logoblack}{RGB}{  0,  0,  0}

\newif\ifblackandwhite
\newif\ifshaded

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\if b#2
  % define colors for black and white
  \definecolor{logogrey} {RGB}{255,255,255}
  \definecolor{logoyellow} {RGB}{  0,  0,  0}
  \blackandwhitetrue
\else
  \if s#2
    \shadedtrue
  \fi
  % define colors
  \definecolor{logogrey} {RGB}{178,180,182}
  \definecolor{logoyellow} {RGB}{210,210, 40}
\fi

% draw scale box
\scalebox{#1}{%
\begin{tikzpicture}[y = 1cm, x = 1cm]%
\ifshaded
 %clip bounding box in case of shading because it needs more space
 \clip[rounded corners=15](1.0,-2.7) rectangle +(23.0,6.5);
\fi

\ifblackandwhite

  % draw outer border
  \draw[draw=black, line width=0.1, rounded corners=15] (1.0,-2.7) rectangle +(23.0,6.5);

\fi

% draw outer backround
\fill[fill=logogrey, rounded corners=15] (1.0,-2.7) rectangle +(23.0,6.5);

\ifshaded
  % draw inner backround
  \fill[fill=logoyellow, opacity=0.3, rounded corners=15] (1.0,-1.4) -- +(0,5.2) -- +(23.0,5.2) -- +(23.0,0) -- cycle;
\else
  % draw inner backround
  \fill[fill=logoyellow, rounded corners=15] (1.0,-1.4) -- +(0,5.2) -- +(23.0,5.2) -- +(23.0,0) -- cycle;
\fi

% draw text
\draw (15.1,1.0) node {\Huge\color{white}AAA};

\end{tikzpicture}%
}%
}%

Note: I use scalebox because i have text on the logo and it should be scaled too. If you have a better idea I'm open to anything. I'm not a TeXpert. :)

Bence
  • 33
  • I think you can separate the header TikZ picture and the customized logo macro. That would both solve the issue and make the distinction clearer. In other words, I would use two \insertheaderlogo (with the box) and \insertlogo (no box , direct TikZ picture with arguments). And please avoid dimensionless numbers if you don't want funny surprises :) – percusse Sep 12 '13 at 10:25
  • After some more research I have found the "root cause". It seems to be a problem in tikz-pgf. More info with explanation can be found in another question, here. – Bence Sep 16 '13 at 08:21

1 Answers1

2

I don't know why it is happening (probably the page layers are not ready when headers are prepared so the transparent path disappears) but if you have a logo or a recurring content just box it and use it later. In writeLaTeX viewer it shows no issues but it's not a guarantee.

\documentclass[a4paper,12pt]{article}
\usepackage{tikz}
\usepackage{fancyhdr}

\newsavebox\myLogoBox
\savebox\myLogoBox{%
  \begin{tikzpicture}
    \fill[fill=green, rounded corners=1] (1mm,2mm) rectangle +(2cm,7mm);
    \fill[fill=red, rounded corners=1, opacity=0.3] (-1mm,2mm) rectangle +(2.4cm,7mm);
  \end{tikzpicture}%
}
\newcommand{\insertlogo}{\usebox{\myLogoBox}}%
\setlength\headheight{ 1.5  cm}

\pagestyle{fancy}
\fancyhf{}
\rhead{\insertlogo}

\begin{document}
A
\end{document}
percusse
  • 157,807