[Tex/LaTex] How to make inner and outer regions transparent

pstrickstikz-pgf

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.

enter image description here

Best Answer

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}

enter image description here

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}

enter image description here

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}

enter image description here