I want to put a TikZ figure with a fading effect (which I reproduced in the body of the document as illustration) in the page header, which I set with the package titlesec. The following MWE seems to indicate that it doesn't work. Any workaround?

\documentclass[12pt]{article}
\usepackage{tikz}

\usepackage[pagestyles]{titlesec}

\newpagestyle{teststyle}{
\color{blue}%
\rule[-.3\baselineskip]{\linewidth}{1pt}}
%% HEADER - depends on option
{}%
{%
}%
}

\begin{tikzpicture}[baseline=(mabox.base)]
\node[rectangle,baseline=current bounding box.base,anchor=south east,inner ysep =0cm,inner xsep =0cm](mabox) at (0,0) {\sectiontitle};
\end{tikzpicture}
}

\begin{document}
\pagestyle{teststyle}
\section{ ABCD EFGH IJKLM NOPQR 1111 2222 3333 4444}

\end{document}

-
Please no formatting in the question title. It's not supported and isn't very readable in verbatim form. Also "doesn't work" doesn't help. State the issue in more detail (e.g. how the output is off exactly; any error messages, etc.). And adding an image if applicable always helps as well. – Martin Scharrer Apr 13 '12 at 17:17

This seems to be a bug in PGF/TikZ related to PDF specials like fading, patterns and transparency applied outside the main body of the page. I encountered the same issue with transparencies with some of my code as well, when I tried to add them inside the output routine. Apparently some PDF dictionary is not updated or connected properly. I think talked about this with Joseph Wright, which knows more about output drivers etc. than me.

This particular issue happens only with single page documents. It works with when the document has more than one page. I guess at \end{document} some PDF stuff is flushed to early. You can fix this by adding a \newpage directly before it, so that the current page is flushed before the end of the document is reached. There will be no trailing empty page because of this as long there is no material after the \newpage. BTW, adding \AtEndDocument{\newpage} is not the same, because it comes to late.

In the following code the fading in the section title works correctly:

\documentclass[12pt]{article}
\usepackage{tikz}

\usepackage[pagestyles]{titlesec}

\newpagestyle{teststyle}{
\color{blue}%
\rule[-.3\baselineskip]{\linewidth}{1pt}}%
%% HEADER - depends on option
{}%
{%
}%
}

\begin{tikzpicture}[baseline=(mabox.base)]
\node[rectangle,baseline=current bounding box.base,anchor=south east,inner ysep=0cm,inner xsep=0cm,text=blue](mabox) at (0,0) {\sectiontitle};
\end{tikzpicture}%
}

\pagestyle{teststyle}
\begin{document}
\section{ABCD EFGH IJKLM NOPQR 1111 2222 3333 4444}
\newpage% Required to make it work
\end{document}

-
Yes, I've also encountered this same problem with PGF/TikZ and one page documents in my background package. – Gonzalo Medina Apr 13 '12 at 18:51
@GonzaloMedina: I have to check if there is a TikZ bug report about this. – Martin Scharrer Apr 13 '12 at 18:54
Great! Please let me know if you find something. – Gonzalo Medina Apr 13 '12 at 18:55
@Martin : more precisely, this bug appears always at the "last page of the document", and not only "if the document has a single page". Thanks anyway for the hint! – nicolas roy Apr 14 '12 at 15:18
@nicolasroy: Ah, yes, that's also possible, but depends on how the last page is flushed I think. I tested it with some two-page document without \newpage and it worked fine; in that specific case I mean. – Martin Scharrer Apr 14 '12 at 15:31

I think I've found the bug. On line 300 of pgfsys-pdftex.def there is a reference to a pgfsmaks. All around it are references to pgfsmasks. When I made the obvious change, I no longer got complaints about the last page's fading being missing.

Here's the diff.

--- pgfsys-pdftex.def   2010-10-25 23:00:21.000000000 +0200
+++ pgfsys-pdftex.def   2012-04-22 22:06:25.000000000 +0200
@@ -297,7 +297,7 @@