How to make inner and outer regions transparent?

The white regions are required to be true transparent (preserved in both PDF and PNG). How do you do this in TikZ or PSTricks?

The following diagram is just an example for illustration.

-
why don't we see the grid? So you want the red zone to become the transparent zone? – pluton Jul 26 '12 at 3:32

2 Answers

With PDF and PostScript (and so, with PSTricks and TikZ/pgf), you can use two rules to determine if a point is inside a path: 'nonzero rule' or 'even odd rule'.

The following code (TikZ) shows the difference:

\documentclass[tikz]{standalone}
\usepackage{mwe}
\begin{document}
\begin{tikzpicture}
\begin{scope}
\fill[red] (-1.2,-.6) rectangle (1.2,.6);
\fill[blue,draw=black,nonzero rule]
circle[radius=1.1cm]
(0:1cm) -- (120:1cm) -- (240:1cm) -- cycle;
\end{scope}
\begin{scope}[yshift=1*-2.3cm]
\fill[red] (-1.2,-.6) rectangle (1.2,.6);
\fill[blue,draw=black,nonzero rule]
circle[radius=1.1cm]
(0:1cm) -- (240:1cm) -- (120:1cm) -- cycle;
\end{scope}
\begin{scope}[yshift=2*-2.3cm]
\fill[red] (-1.2,-.6) rectangle (1.2,.6);
\fill[blue,draw=black,even odd rule]
circle[radius=1.1cm]
(0:1cm) -- (120:1cm) -- (240:1cm) -- cycle;
\end{scope}
\begin{scope}[yshift=3*-2.3cm]
\fill[red] (-1.2,-.6) rectangle (1.2,.6);
\fill[blue,draw=black,even odd rule]
circle[radius=1.1cm]
(0:1cm) -- (240:1cm) -- (120:1cm) -- cycle;
\end{scope}
\end{tikzpicture}
\end{document}


The PDF file is transparent. To get a correct PNG file (with transparencies), use convert from ImageMagick (pdftopnm seems to add a white background).

The following PSTricks code shows the difference (using or not using eofill fillstyle - eofill means even odd filling):

\documentclass[border=12pt]{standalone}
\usepackage{pstricks}
\begin{document}
\begin{pspicture}[showgrid](6,6)
\pscustom[fillstyle=solid,fillcolor=green]
{
\pscircle(3,3){3}
\psline[liftpen=2](1,2)(5,2)(3,5)(1,2)
}
\end{pspicture}
\begin{pspicture}[showgrid](6,6)
\pscustom[fillstyle=solid,fillcolor=green]
{
\pscircle(3,3){3}
\psline[liftpen=2](3,5)(5,2)(1,2)(3,5)
}
\end{pspicture}
\begin{pspicture}[showgrid](6,6)
\pscustom[fillstyle=eofill,fillcolor=green]
{
\pscircle(3,3){3}
\psline[liftpen=2](1,2)(5,2)(3,5)(1,2)
}
\end{pspicture}
\begin{pspicture}[showgrid](6,6)
\pscustom[fillstyle=eofill,fillcolor=green]
{
\pscircle(3,3){3}
\psline[liftpen=2](3,5)(5,2)(1,2)(3,5)
}
\end{pspicture}
\end{document}


With PSTricks, eofill can't be used with some other fill styles like hlines (may be a bug). You can always use nonzero rule (the default rule used by PSTricks) and correct direction for your hole:

\documentclass[border=12pt]{standalone}
\usepackage{pstricks}
\begin{document}
\begin{pspicture}[showgrid](6,6)
\pscustom[fillstyle=hlines,hatchcolor=green]
{
\pscircle(3,3){3}
\psline[liftpen=2](1,2)(5,2)(3,5)(1,2)
}
\end{pspicture}
\begin{pspicture}[showgrid](6,6)
\pscustom[fillstyle=hlines,hatchcolor=green]
{
\pscircle(3,3){3}
\psline[liftpen=2](3,5)(5,2)(1,2)(3,5)
}
\end{pspicture}
\end{document}


-
So if eofill is specified, then we cannot have a hatched filling? – kiss my armpit Jul 27 '12 at 18:44
@HiggsBoson Yes... May be a bug. But you can always use nonzero rule (the default rule used by PSTricks) and correct direction for your hole (like in the second pspicture in my PSTricks example). Look at my edited answer... – Paul Gaborit Jul 27 '12 at 18:50

I am not aware of very sophisticated clipping possibilities with pstricks where the clipping shape contains holes but you can (almost always) mimic complicated configurations by superimposing layers:

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pstricks}
\begin{document}
\begin{pspicture}[showgrid](6,6)
\psframe[linestyle=none,fillstyle=solid,fillcolor=blue](0,0)(3,3)
\pscircle[linestyle=none,dimen=middle,fillstyle=solid,fillcolor=red](3,3){3}
\begin{psclip}{\pspolygon[linestyle=none,fillstyle=solid,fillcolor=white](2,2)(3,4)(4,2)}
\psframe[linestyle=none,fillstyle=solid,fillcolor=blue](0,0)(3,3)
\end{psclip}
\end{pspicture}
\end{document}


Just notice that the triangle is not transparent as is the square in your example.

or a more complicated one

Edit: I think having true internal transparent zones in the final pdf file as suggested in your new example should be possible since this feature exists in Illustrator and Inkscape but as far as I know, it is not implemented in pstricks or TikZ but I may be wrong.

-
no but it is not a problem. You just have to clip what is inside the triangle (it could be a picture for instance) – pluton Jul 26 '12 at 4:04