# defining a fading-style directly in the fill command

In the following MWE i define a fading monfading with the command \tikzfading which is then used in the command \fill.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node[anchor=south west] at (0,0) {HALLO} ;
\end{tikzpicture}
\end{document}


Now, i encounter some difficulties, because my tikz picture is actually in the header of the page (i use \RequirePackage[pagestyles]{titlesec}), and there, it seems not to remember the definition of \tikzfading. Therefore, i would like to put the definition of monfading directly in the \fill command. Therefore something like :

%Pseudocode :
\begin{tikzpicture}
\node[anchor=south west] at (0,0) {HALLO} ;
color=transparent!0},fill=white] (0,0) rectangle (1.5,1);
\end{tikzpicture}


How i can make this pseudo code a real code?

-
Couldn't you just put the \tikzfading command into your preamble? –  Jake Apr 13 '12 at 16:29
it doesn't work either. The definition of monfading seems to be forgotten in the header. I could make a MWE for this, but before i was looking for a quick solution, like my pseudo-code –  nicolas roy Apr 13 '12 at 16:30
For this concrete example, you can just use \fill[draw=black, path fading=west, fill=white] (0,0) rectangle (1.5,1);. Is your real fading more complicated? –  Jake Apr 13 '12 at 16:42
Oh, i see, actually i redefined a completely standard fading, which already exists under the name ẁest. This is enough for me, i just needs this fading (for the moment). Thanks. But I am still interested in the general answer to the question. –  nicolas roy Apr 13 '12 at 16:56
\tikzset{ define fading/.code={ \tikzfading[#1] } } allows you to define a new fading within your drawing command. Doesn't solve the header problem though, so I'm not sure if there's any utility for this. –  Jake Apr 13 '12 at 17:10

The problem here is that when TikZ/PGF uses a fading it has to put the code necessary for that fading into the PDF near the start. So it stores all of this in a token register that is processed at the end of the document using \AtEndDocument. But this hook is executed before the last \clearpage. This is a problem if the fadings are in headers or footers because these are only processed at the \clearpage. So the necessary code for doing the fadings is processed too late to make it into the PDF. The solution is to ensure that the fading stuff is done after the last page has been fully processed (ie shipped). There are a number of ways to achieve this:

1. File a bug report and get TikZ/PGF changed.
2. Edit pgfutil-latex.def and change \AtEndDocument{\the\pgfutil@everybye} to \AtEndDocument{\clearpage\the\pgfutil@everybye}.
3. Issue a \clearpage before the end of the document, either manually or automatically. If automatically, ie via \AtEndDocument, then put this before loading tikz.
4. Use the package atveryend and put:

\usepackage{atveryend}
\let\OrigAtEndDocument=\AtEndDocument
\let\AtEndDocument=\AfterLastShipout
\usepackage{tikz}
\let\AtEndDocument=\OrigAtEndDocument


at the start of the document. Note that in the main TikZ/PGF code then this is the only use of \AtEndDocument. A couple of the libraries also use it, notably the external library, and I haven't experimented with whether or not they are okay being after the \clearpage or not. Experiment.

5. Use the atveryend package and edit pgfutil-latex.def and change \AtEndDocument to \AfterLastShipout (probably best to add \RequirePackage{atveryend} to this file as well).
6. Use one of the other solutions at Can I hook into the end of the document but *after* the \clearpage?

Here's some working code without modifying any of the TikZ code:

\documentclass[12pt]{article}
\usepackage{atveryend}
\let\OrigAtEndDocument=\AtEndDocument
\let\AtEndDocument=\AfterLastShipout
\usepackage{tikz}
\let\AtEndDocument=\OrigAtEndDocument
\usepackage{lipsum}
\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}