4

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}
\usetikzlibrary{patterns,fadings}
\begin{document}
  \tikzfading[name=monfading,left color=transparent!100,right color=transparent!0]
  \begin{tikzpicture}
    \node[anchor=south west] at (0,0) {HALLO} ;   
    \fill[draw=black,path fading=monfading,fill=white] (0,0) rectangle (1.5,1);
  \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} ;   
    \fill[draw=black,path fading={left color=transparent!100,right
    color=transparent!0},fill=white] (0,0) rectangle (1.5,1);
  \end{tikzpicture} 

How i can make this pseudo code a real code?

Loic Rosnay
  • 8,167
  • 1
    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 – Loic Rosnay 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. – Loic Rosnay Apr 13 '12 at 16:56
  • oh, but i see also, that it does not solve my problem : it seems that one can not have a fading in the header. I have to post a new question. – Loic Rosnay Apr 13 '12 at 16:58
  • 2
    \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
  • No need to ask a new question, just edit in an example with the picture in the header for others to test. – Andrew Stacey Apr 15 '12 at 07:35
  • @Jake: Nice idea. – Marco Daniel Apr 15 '12 at 11:52
  • Regarding defining a fading in the header, please try the fix I just posted at http://tex.stackexchange.com/a/52931/86 and see if it helps. – Andrew Stacey Apr 22 '12 at 21:32
  • @Jake, you might like to try that as well. – Andrew Stacey Apr 22 '12 at 21:33

1 Answers1

6

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}
\usetikzlibrary{fadings}
\usepackage[pagestyles]{titlesec}


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

\newcommand{\FadingSection}{%
    \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};
        \fill[path fading=west,fill=white] (-5,0) rectangle (0,1em);
    \end{tikzpicture}   
}

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

\newpage

\lipsum

\end{document}
Andrew Stacey
  • 153,724
  • 43
  • 389
  • 751