[Tex/LaTex] Draw a rectangle hiding an area of a picture

tikz-pgf

I have something drawn as a tikzpicture:

\begin{tikzpicture}
  \node[above] at (8,13) {$...$};
  \filldraw[fill=white!20] (2,0) rectangle (6,12);
  \draw (2,4) -- (2,12); \draw (2,12) -- (6,12); \draw (6,12) -- (6,4);
  \draw [dashed] (6,3) -- (1,3);
  ...
\end{tikzpicture}

Now I would like to draw a non-transparent white rectangle which hides a part of the picture, so that what is under the rectangle becomes invisible or less visible (e.g. gray). Does anyone know how to do it?

Best Answer

You can simply move the \filldraw command to be after what you want hidden, and control the opacity with fill opacity=<opacity>:

enter image description here

If you want what is underneath to be complely hidden, adding fill opacity=1.0 captures the specific intent (even though it is the default).

Note:

  • I changed the color to yellow!20 to make the effect clearer in the image here. Replacing this with fill=white will give you the desired result.

Code:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\foreach \OpacitySetting in {1.0, 0.95, 0.75}{%
\begin{tikzpicture}
\draw [blue, ultra thick] (0,2) -- (4,2);
\filldraw[fill=yellow!20, fill opacity=\OpacitySetting] (1,0) rectangle (3,4) 
    node [above left, text opacity=1.0]  {opacity=\OpacitySetting};
\end{tikzpicture}%
\hspace{0.25cm}%
}%
\end{document}