Transparency in tikz, preview package and xelatex

Is it possible to have transparency in a tikz picture while using the preview package and compiling with xelatex?

The following mwe compiles without warnings/errors under both xelatex and pdflatex but with xelatex there is no transparency. Everything is printed opaque. With pdflatex everything works as expected.

\documentclass[a4paper,12pt]{article}

\usepackage{tikz}

\usepackage[xetex,active,tightpage]{preview}
%\usepackage[pdftex,active,tightpage]{preview}

\PreviewEnvironment[]{tikzpicture}

\begin{document}

\begin{tikzpicture}[line width=1ex]
\draw (0,0) -- (3,1);
\filldraw [fill=red,draw opacity=0.5] (1,0) rectangle (2,1);
\end{tikzpicture}

\end{document}

-
Perhaps the problem is the same here : tex.stackexchange.com/questions/17080/… –  Alain Matthes Jun 17 '11 at 13:35
There is an issue with XeLaTeX, preview package and TikZ shadings, most likely also with patterns and transparency. I as standalone class author, which uses preview, got contacted by a user two month ago because of it. I tracked it down to the above combination. The user then wrote an email to the preview author, but I didn't heard anything about it ever since. –  Martin Scharrer Jun 17 '11 at 13:54
@Martin, I see. I'll send an email to the author too. I hope it is something easily fixed. –  pmav99 Jun 17 '11 at 14:24
it also might be an issue of PGF/TikZ instead. Anyway it seems only to appear with XeLaTeX+preview+some feature of PGF/TikZ (shadings, patterns, transparency). It works without preview or with it under pdflatex. –  Martin Scharrer Jun 17 '11 at 14:34
@Martin, Just removing the preview package outputs the expected result even with xelatex. So I don't think it is related with xelatex or/and pgf (of course I can be wrong). AFAIK Tikz's patterns problems are related with xelatex. –  pmav99 Jun 17 '11 at 14:38

You can avoid the issue by selecting the page size to be equal to the picture size. Therefore you don't need preview and it works fine with XeLaTeX. For this you need to save the picture in a box register. Because geometry only allows to select the page size in the preamble you need to move the picture there, which is OK as long it inside a savebox (like inside a lrbox environment).

I'm planning to build a feature like this into standalone as an alternative to preview.

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

%\usepackage[active,tightpage]{preview}
%\usepackage[pdftex,active,tightpage]{preview}
%\PreviewEnvironment[]{tikzpicture}

\pagestyle{empty}
\normalfont% required to select normal font already in the preamble
\begin{lrbox}{0}%
\begin{tikzpicture}[line width=1ex]
\draw (0,0) -- (3,1);
\filldraw [fill=red,draw opacity=0.5] (1,0) rectangle (2,1);
\end{tikzpicture}%
\end{lrbox}

\geometry{paperwidth=\wd0,paperheight=\ht0,margin=0cm}
\parindent=0pt
\begin{document}
\usebox{0}%
\end{document}


(The outer dark frame is the background of the PDF viewer)

The upcoming version 1.0 of standalone includes code similar to this and avoids the need of preview. It should work fine for XeLaTeX.

-
The fill color (red) is still opaque. I cannot see the black line inside the red rectangle. Herbert's answer can be used as a reference. –  kiss my armpit Dec 12 '11 at 22:29
@CounterTerrorist: That's simple because draw opacity was used (by the OP, so I did the same). If you replace it by fill opacity or opacity (which does both) the filled rectangle will be transparent. –  Martin Scharrer Dec 12 '11 at 22:36
I see.......... –  kiss my armpit Dec 12 '11 at 22:40

if I use PSTricks and XeLaTeX together with preview, it works. The reason why I suppose a problem with TikZ and not preview

-
I sent an email to preview's author (no answer so far) and filled a bug report for tikz sourceforge.net/tracker/… –  pmav99 Jun 19 '11 at 12:23
I cannot produce PSTricks transparency with XeLaTeX, see tex.stackexchange.com/q/38001/9467. –  kiss my armpit Dec 12 '11 at 11:14