Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
    
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
add comment

2 Answers

up vote 9 down vote accepted

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

share|improve this answer
    
So if eofill is specified, then we cannot have a hatched filling? –  Fifa Earth Cup 2014 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
add comment

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.

enter image description here

or a more complicated one

enter image description here

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.

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.