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.

I'm looking for a way to use transparency/opacity to highlight a particular area in a TikZ figure I generated. To be more specific, I have a TikZ figure that illustrates the relationship between several concepts. After I have shown this figure for the first time, I want to reuse it later in my document when I discuss specific concepts in more detail. To make clear which concept of the figure is discussed, I want to highlight the relevant part of the figure. It would be nice if there was a way in which I could leave the relevant part of the figure unchanged and make the rest of the figure somewhat faded using opacity. Is there a simple way to do this?

Update: Here's an MWE:

\documentclass[10pt]{article}

\usepackage{tikz}
\usepackage{fancyhdr}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}

\begin{document}

\thispagestyle{empty}

\begin{tikzpicture}[
  opacity=0.5,
  box/.style={rectangle,draw=black,
    minimum size=1cm,text width=2.2cm,text centered,font=\small}]

%%% coordinates
  \coordinate (CM) at ( 0.0, 3.0);
  \coordinate (DM) at ( 0.0, 6.0);
  \coordinate (MM) at ( 0.0, 9.0);
  \coordinate (PR) at ( 0.0,12.0);

%%% nodes  
  \node[box] (cm) at (CM) {box 1};
  \node[box] (dm) at (DM) {box 2};
  \node[box] (mm) at (MM) {box 3};   
  \node[box] (pr) at (PR) {box 4}; 

%%% arrows
  \draw[->] (dm.south) -- 
    node[fill=white,align=center,text width=3.5cm,font=\small] 
    {\textsl{proc 1}} (cm.north);  

  \begin{scope}[opacity=1.0]
    \draw[->] (mm.south) -- 
      node[fill=white,align=center,text width=3.3cm,font=\small] 
      {\textsl{proc 2}} (dm.north);
  \end{scope}

  \draw[->] (pr.south) -- 
    node[fill=white,align=center,text width=3.5cm,font=\small] 
    {\textsl{proc 3}} (mm.north);
\end{tikzpicture}

\end{document}

This does not quite do what I want because: (1) I would like to be able to specify a circle or rectangle that contain the highlighted portion of the figure, rather than having to explicitly select which commands should not be in the background. (2) Ideally, I would like to not have to modify the basic source for the figure so I can keep it in a separate file and simply input it and add the command to select the portion to be highlighted. (3) The text superimposed on the arrows works fine because of the white background when there is no opacity. But with opacity, the white background no longer covers the arrow. Is there an easier solution rather than drawing the arrows in two pieces?

share|improve this question
    
You can supply opacity=0.5 to the tikzpicture options to make everything slightly transparent, and set opacity=1 for those parts of your drawing that you want highlighted. –  Jake Aug 30 '12 at 8:18
    
Short answer: yes. Please add a minimal working example (MWE) that illustrates your problem. –  Paul Gaborit Aug 30 '12 at 8:20
2  
To expand on Paul's comment, if you want a more detailed than Jake's given, you will need to show us how you are drawing the diagram at the moment otherwise there could be lots of ways to achieve this but no way for anyone to know which will best help you. –  Andrew Stacey Aug 30 '12 at 8:21
add comment

1 Answer

up vote 7 down vote accepted

You can draw a semi-transparent mask on top of your existing tikzpicture by using remember picture to make the coordinates available. The pictures show the mask in red for demonstration, and in white for the actual application:

enter image description here

\documentclass[10pt]{article}

\usepackage{tikz}
\usepackage{fancyhdr}

\begin{document}

\thispagestyle{empty}

\begin{tikzpicture}[remember picture,
  box/.style={rectangle,draw=black,
    minimum size=1cm,text width=2.2cm,text centered,font=\small}]

%%% coordinates
  \coordinate (CM) at ( 0.0, 3.0);
  \coordinate (DM) at ( 0.0, 6.0);
  \coordinate (MM) at ( 0.0, 9.0);
  \coordinate (PR) at ( 0.0,12.0);

%%% nodes  
  \node[box] (cm) at (CM) {box 1};
  \node[box] (dm) at (DM) {box 2};
  \node[box] (mm) at (MM) {box 3};   
  \node[box] (pr) at (PR) {box 4}; 

%%% arrows
  \draw[->] (dm.south) -- 
    node[fill=white,align=center,text width=3.5cm,font=\small] (proc1) 
    {\textsl{proc 1}} (cm.north);  

    \draw[->] (mm.south) -- 
      node[fill=white,align=center,text width=3.3cm,font=\small] 
      {\textsl{proc 2}} (dm.north);


  \draw[->] (pr.south) -- 
    node[fill=white,align=center,text width=3.5cm,font=\small] 
    {\textsl{proc 3}} (mm.north);
    \coordinate (diagramNW) at (current bounding box.north west);
    \coordinate (diagramSE) at (current bounding box.south east);
\end{tikzpicture}


%% The mask
\begin{tikzpicture}[remember picture, overlay]
\fill [red, opacity=0.5,even odd rule]
    (diagramNW) rectangle (diagramSE) % Cover up everything
    (mm.north west) rectangle (dm.south east) % Cut out a rectangular area;
    (proc1) circle [radius=0.5cm];
\end{tikzpicture}

\end{document}
share|improve this answer
    
Thanks, this does what I need. –  user1362373 Aug 30 '12 at 10:34
    
I have a follow-up question: It would be nice not to have to take out the commands that crop the picture - is there a way to do that? Otherwise I will have to do it manually. –  user1362373 Aug 30 '12 at 10:35
    
@user1362373: I'm not sure what you mean by "the commands that crop the picture". Could you elaborate (or edit your question or open a new one)? –  Jake Aug 30 '12 at 10:46
    
In your MWE, you removed \usepackage[active,tightpage]{preview} and \PreviewEnvironment{tikzpicture}. When I tried your MWE and put these back in, the masked part is not shown; I only get the figure without the masking. –  user1362373 Aug 30 '12 at 13:07
1  
Just was able update and now get the same behavior as you. Thanks again for your help. –  user1362373 Aug 31 '12 at 8:56
show 3 more comments

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.