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.

My question is similar to Transparent node with opaque text? . The difference is that i want the red circle to be above the green rectangle and below the word "Node". And also I do not want to draw the node twice.

(the image is from the other question, what i want to achieve is similar to the first image) example

Here is a MWE:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
    % desired effect:
    \node [right,fill=green] {Node};
    \node [fill=red,circle,minimum size=6mm,inner sep=0mm] {};
    \node [right] {Node};

    % undesired effect:
    \node at(2,0) [right,fill=green] {Node};
    \node at(2,0) [fill=red,circle,minimum size=6mm,inner sep=0mm] {};
\end{tikzpicture}
\end{document}

I think I am looking for a layer between a node and its text.

(Sorry if it should have not been posted as a separate question, I was uncertain).

Here is a longer MWE showing what i want to achieve finally:

\documentclass{beamer}


\usepackage{tikz,xparse}
\usetikzlibrary{matrix,backgrounds}


\definecolor{color3}{rgb}{0,1,0}
\definecolor{color2}{rgb}{1,0,0}
\definecolor{color1}{HTML}{999999}

\pgfdeclarelayer{myback}
\pgfsetlayers{myback,background,main}

\tikzset{myfillcolor/.style = {draw=#1,fill=#1!50,rounded corners,line width=2pt}}

\begin{document}

\NewDocumentCommand{\fhighlight}{O{color2} m m}{
\draw[myfillcolor=#1] ([shift={(.1cm,-.1cm)}]#2.north west)rectangle ([shift={(-.1cm,.1cm)}]#3.south east);
}


\begin{frame}{Test frame}
\begin{tikzpicture}
\matrix [
    ampersand replacement=\&,
    matrix of nodes,
    nodes={inner sep=0,text centered,opacity=.5,text opacity=1},
    every even row/.style={nodes={
                rectangle,
                draw,
                minimum size=0.8cm,
                fill=color1,
                font=\Large,
                text height=2ex,
                text depth=.25ex,
                text centered,
                }},
        column 10/.style={nodes={text width=1cm}},
        row 1/.style={minimum height=0.5cm},
        row 5/.style={minimum height=0.5cm}
    ]
    (m)
{
    \& x\& x\&\&\\
    |[fill=none,fill=color1!50, dashed]|~\&x\&x\&x\&x\\
    |[text height=0.2cm]|~\\
    |[draw=none,fill=none]|$x:$\&x\&x\&x\&x\\
    \& x\&\&\&\\
};
\begin{pgfonlayer}{myback}
\only<2>{\fhighlight[color2]{m-2-2}{m-4-5}}
\only<3>{\fhighlight[color3]{m-2-1}{m-4-4}}
\end{pgfonlayer}

\end{tikzpicture}
\end{frame}

\end{document}

In the example, i want the red and green rectangles NOT to be transparent or opaque, but instead bright, and to be placed over the gray boxes but under the text

share|improve this question
    
It would be helpful if you composed a fully compilable MWE including \documentclass and the appropriate packages that sets up the problem. While solving problems is fun, setting them up is not. Then those trying to help can simply cut and paste your MWE and get started on solving the problem. –  Peter Grill Dec 15 '12 at 19:42
    
Thanks for the hint –  antosecret Dec 15 '12 at 20:58
3  
That's not going to be easy... Could you specify what exactly your requirement of "not drawing the node twice" entails? Would it be okay to draw the rectangle and the text separately? Or do you want a single command that creates both the rectangle and the text? Also, I think it might be a good idea if you could explain why you need this functionality. Maybe there's a more straightforward way to handle the situation. –  Jake Dec 15 '12 at 21:11
2  
In addition to @Jake 's comment, how far should the transparent fill go in the background? For example, if all is drawn over a blue background, should that blue rectangle be at the same layer with red circle or behind the node's transparent filling? –  percusse Dec 15 '12 at 21:23
1  
Have a look at this answer : tex.stackexchange.com/questions/65096/… –  percusse Dec 15 '12 at 21:50
show 4 more comments

2 Answers 2

up vote 8 down vote accepted

@Qrrbrbirlbel answer is very nice, but I think one could also proceed without saving the text. The trick is very simple: use at least three layers, myback, highlight and main; in the main layer there will be the text of the matrix, in the myback layer the matrix fill and in the highlight layer... the highlighting. What is important here, is that the highlight layer should be declared behind the main (text) but before the matrix fill (myback). Thus, with this approach, one should declare a posteriori how the matrix is filled and this is the only disadvantage I see.

The code:

\documentclass{beamer}
\usepackage{lmodern}
\usepackage{tikz,xparse}
\usetikzlibrary{matrix,backgrounds}

\definecolor{color3}{rgb}{0,1,0}
\definecolor{color2}{rgb}{1,0,0}
\definecolor{color1}{HTML}{999999}

\pgfdeclarelayer{myback} % layer to fill the matrix
\pgfdeclarelayer{highlight} % layer to highlight the matrix
\pgfsetlayers{myback,highlight,main}

\tikzset{my fill color/.style = {draw=#1,fill=#1!50,rounded corners,line width=2pt}}

\begin{document}

% For both commands the second argument could be used to select the layer: here
% is not really necessary since the default is required, but having this option may help
% for other purposes (I still have to think about them..)

\NewDocumentCommand{\fhighlight}{O{color2} O{highlight} m m}{
\begin{pgfonlayer}{#2}
\draw[my fill color=#1] ([shift={(.1cm,-.1cm)}]#3.north west)rectangle ([shift={(-.1cm,.1cm)}]#4.south east);
\end{pgfonlayer}
}

\NewDocumentCommand{\bhighlight}{O{black} O{myback} m m}{
\begin{pgfonlayer}{#2}
\draw[#1] (#3.north west)rectangle (#4.south east);
\end{pgfonlayer}
}

\begin{frame}{Test frame}
\begin{tikzpicture}
\matrix [
    ampersand replacement=\&,
    matrix of nodes,
    nodes={inner sep=0,text centered},
    every even row/.style={nodes={
                minimum size=0.8cm,
                %fill=color1,
                font=\Large,
                text height=2ex,
                text depth=.25ex,
                text centered,
                }},
        column 10/.style={nodes={text width=1cm}},
        row 1/.style={minimum height=0.5cm},
        row 5/.style={minimum height=0.5cm}
    ] (m) {
    \& x\& x\&\&\\
    ~\&x\&x\&x\&x\\
    |[text height=0.2cm]|~\\
   $x:$\&x\&x\&x\&x\\
    \& x\&\&\&\\
    };

% the matrix fill always visible: put in last layer, myback

\bhighlight[fill=color1!50, dashed, opacity=0.5]{m-2-1}{m-2-1}

\foreach \i in {2,...,5}{
\pgfmathtruncatemacro\x{\i}
\bhighlight[draw,rectangle, opacity=0.5]{m-2-\x}{m-2-\x}
}

\foreach \i in {2,...,5}{
\pgfmathtruncatemacro\x{\i}
\bhighlight[draw,rectangle, opacity=0.5]{m-4-\x}{m-4-\x}
}

% matrix highlighting

\only<2>{\fhighlight[color2]{m-2-2}{m-4-5}}
\only<3>{\fhighlight[color3]{m-2-1}{m-4-4}}
\end{tikzpicture}
\end{frame}

\end{document}

The result:

enter image description here

share|improve this answer
    
Not that big of a disadvantages: I like the clarity as one can specify precisely which nodes are using what style. The fact that cells inherit styles of their row/column cost me a lot of time in finding a solution myself … –  Qrrbrbirlbel Dec 16 '12 at 11:26
    
Yes, I agree that setting the styles after having drawn the matrix is much more clear (also because I don't like too much redefine styles locally inside the matrix: I prefer having just matrix of text, or at maximum with some \tikzmarks). It would be great, but I think is not so simple, define some background column/row style that apply the styles on the background layer directly: that will do the trick almost straightforward. –  Claudio Fiandrino Dec 16 '12 at 11:58
add comment

The old and  too localized solution where I defined an extra shape can be found in the history of this answer.


I can’t see a solution without re-using parts of the matrix.

Ideas:

  1. Drawing and filling the rectangle shape on a background layer, similar to Cut one side of a rectangle node in TikZ, didn’t work: Apparently \tikzlastnode (or \tikzmatrixname-\tikzmatrixcurrentrow-\tikzmatriccurrentcolumn) and pgfonlayer don’t play nice and mess within append after command.

    But, similar to solution 3, saving the draw and fill options for later use does work. Unfortunately, this deals differently with options that are given to every even row and the local |[<options>]| which is the reason why opacity=.5 and draw have to be given to the m-2-1 again, but this can be made easier with a style that is given to every even row and as well to the local options.

    The back style re-uses its argument (#1) with the option opacity=0; this serves the purpose of not changing the actual dimension of the nodes.

  2. Re-drawing the whole matrix where only the text is being typeset, realized be redefining PGF’s \pgfusepath or overwriting the fill and the draw style, so that one can just re-use the same \matrix call.

    This is the easiest solution, but re-draws the node’s content, therefore not usable when text opacity is applied.

  3. Saving the node’s text and typeset it again.

Code 1

\documentclass{beamer}

\usepackage{tikz,xparse,etoolbox,xstring}
\usetikzlibrary{matrix,backgrounds}

\definecolor{color3}{rgb}{0,1,0}\definecolor{color2}{rgb}{1,0,0}\definecolor{color1}{HTML}{999999}
\pgfdeclarelayer{myback}\pgfsetlayers{myback,background,main}

\tikzset{myfillcolor/.style = {draw=#1,fill=#1!50,rounded corners,line width=2pt}}
\NewDocumentCommand{\fhighlight}{O{color2} m m}{
    \draw[myfillcolor=#1] ([shift={(.1cm,-.1cm)}]#2.north west)rectangle ([shift={(-.1cm,.1cm)}]#3.south east);
}

\def\backgroundtodrawlater{}
\newcommand*{\saveMe}[1]{%
    \ifcsname iph\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn\endcsname
        \StrCount{\backgroundtodrawlater}{,}[\kommaCount]%
        \StrBefore[\kommaCount]{\backgroundtodrawlater}{,}[\backgroundtodrawlater]
    \fi
    \ifx\backgroundtodrawlater\empty%
        \xappto\backgroundtodrawlater{{#1}/\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn}%
    \else%
        \xappto\backgroundtodrawlater{,{#1}/\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn}%
    \fi%
    \expandafter\xdef\csname iph\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn\endcsname{}%
}
\newcommand*{\drawMyBackBoxes}{%
    \begin{pgfonlayer}{myback}
    \foreach \opt/\name in \backgroundtodrawlater {
        \expandafter\ifx\csname iph\name\endcsname\empty
%           \typeout{opt="\opt", name="\name"}% debug
            \expandafter\path\expandafter[\opt] (\name.south west) rectangle (\name.north east);
            \expandafter\xdef\csname iph\name\endcsname{ }
        \else
            \expandafter\xdef\csname iph\name\endcsname{}
        \fi
    }
    \end{pgfonlayer}
}
\tikzset{
    back/.style={
        #1,
        opacity=0,
        save path={#1},
    },
    save path/.code={\saveMe{#1}}
}
\begin{document}
\begin{frame}{Test frame}
\begin{tikzpicture}
\matrix [
    ampersand replacement=\&,
    matrix of nodes,
    nodes={inner sep=0pt,text centered,opacity=.5,text opacity=1},
    every even row/.style={nodes={
            rectangle,
            back={
                draw,
                fill=color1,
                opacity=.5
            },
            minimum size=0.8cm,
            font=\Large,
            text height=2ex,
            text depth=.25ex,
            text centered,
            text opacity=1,
    }},
    column 10/.style={nodes={text width=1cm}},
    row 1/.style={minimum height=0.5cm},
    row 5/.style={minimum height=0.5cm}
] (m) {
                                                  \& x \& x \&   \&   \\
        |[back={fill=color1!50, draw, dashed}]| ~ \& x \& x \& x \& x \\
        |[text height=0.2cm]|      ~                                  \\
        |[back={draw=none,fill=none}]| $x:$       \& x \& x \& x \& x \\
                                                  \& x \&   \&   \&   \\
    };

\begin{pgfonlayer}{background}
    \only<2>{\fhighlight[color2]{m-2-2}{m-4-5}}
    \only<3>{\fhighlight[color3]{m-2-1}{m-4-4}}
\end{pgfonlayer}

\drawMyBackBoxes
\end{tikzpicture}
\end{frame}
\end{document}

Code 2

\documentclass{beamer}
\usepackage{tikz,xparse}\usetikzlibrary{matrix,backgrounds}
\definecolor{color3}{rgb}{0,1,0}\definecolor{color2}{rgb}{1,0,0}\definecolor{color1}{HTML}{999999}
\tikzset{myfillcolor/.style = {draw=#1,fill=#1!50,rounded corners,line width=2pt}}
\NewDocumentCommand{\fhighlight}{O{color2} m m}{\draw[myfillcolor=#1] ([shift={(.1cm,-.1cm)}]#2.north west)rectangle ([shift={(-.1cm,.1cm)}]#3.south east);}
\newcommand{\repeatMeText}[1]{
    #1%
    \def\repeatMe{{% note the extra pair of braces, keeps changes local!
%       \renewcommand{\pgfusepath}[1]{}% either this
        \tikzset{
            fill/.style={},% or this and
            draw/.style={},% this
            column sep=\pgflinewidth,% compensates for missing "draw"
            row sep=.5\pgflinewidth% compensates for missing "draw"
            }
        #1
    }}
}
\begin{document}

\begin{frame}{Test frame}
\begin{tikzpicture}
\repeatMeText{
\matrix [
    ampersand replacement=\&,
    matrix of nodes,
    nodes={inner sep=0,text centered,opacity=.5,text opacity=1},
    every even row/.style={nodes={
            rectangle,
            draw,
            minimum size=0.8cm,
            fill=color1,
            font=\Large,
            text height=2ex,
            text depth=.25ex,
            text centered,
    }},
    column 10/.style={nodes={text width=1cm}},
    row 1/.style={minimum height=0.5cm},
    row 5/.style={minimum height=0.5cm}
] (m) {
                                     \& x \& x \&   \&   \\
        |[fill=color1!50, dashed]|~  \& x \& x \& x \& x \\
        |[text height=0.2cm]| ~                          \\
        |[draw=none,fill=none]| $x:$ \& x \& x \& x \& x \\
                                     \& x \&   \&   \&   \\
    };
}
\only<2>{\fhighlight[color2]{m-2-2}{m-4-5}}
\only<3>{\fhighlight[color3]{m-2-1}{m-4-4}}
\only<2,3>{\repeatMe}
\end{tikzpicture}
\end{frame}
\end{document}

Code 3

\documentclass{beamer}

\usepackage{tikz,xparse,etoolbox,xstring}
\usetikzlibrary{matrix,backgrounds}

\definecolor{color3}{rgb}{0,1,0}\definecolor{color2}{rgb}{1,0,0}\definecolor{color1}{HTML}{999999}
\pgfdeclarelayer{myback}\pgfsetlayers{myback,background,main}

\tikzset{myfillcolor/.style = {draw=#1,fill=#1!50,rounded corners,line width=2pt}}
\NewDocumentCommand{\fhighlight}{O{color2} m m}{
    \draw[myfillcolor=#1] ([shift={(.1cm,-.1cm)}]#2.north west)rectangle ([shift={(-.1cm,.1cm)}]#3.south east);
}

\def\backgroundtodrawlater{}
\newcommand*{\saveMe}[1]{%
    \ifcsname iph\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn\endcsname
        \StrCount{\backgroundtodrawlater}{,}[\kommaCount]%
        \StrBefore[\kommaCount]{\backgroundtodrawlater}{,}[\backgroundtodrawlater]
    \fi
    \ifx\backgroundtodrawlater\empty%
        \xappto\backgroundtodrawlater{{#1}/\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn}%
    \else%
        \xappto\backgroundtodrawlater{,{#1}/\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn}%
    \fi%
    \expandafter\xdef\csname iph\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn\endcsname{}%
}
\newcommand*{\drawMyBackBoxes}{%
    \begin{pgfonlayer}{myback}
    \foreach \opt/\name in \backgroundtodrawlater {
        \expandafter\ifx\csname iph\name\endcsname\empty
%           \typeout{opt="\opt", name="\name"}% debug
            \expandafter\path\expandafter[\opt] (\name.south west) rectangle (\name.north east);
            \expandafter\xdef\csname iph\name\endcsname{ }
        \else
            \expandafter\xdef\csname iph\name\endcsname{}
        \fi
    }
    \end{pgfonlayer}
}
\tikzset{
    back/.style={
        #1,
        opacity=0,
        save path={#1},
    },
    save path/.code={\saveMe{#1}}
}
\begin{document}
\begin{frame}{Test frame}
\begin{tikzpicture}
\matrix [
    ampersand replacement=\&,
    matrix of nodes,
    nodes={inner sep=0pt,text centered,opacity=.5,text opacity=1},
    every even row/.style={nodes={
            rectangle,
            back={
                draw,
                fill=color1,
                opacity=.5
            },
            minimum size=0.8cm,
            font=\Large,
            text height=2ex,
            text depth=.25ex,
            text centered,
            text opacity=1,
    }},
    column 10/.style={nodes={text width=1cm}},
    row 1/.style={minimum height=0.5cm},
    row 5/.style={minimum height=0.5cm}
] (m) {
                                                              \& x \& x \&   \&   \\
        |[back={fill=color1!50, draw, opacity=.5, dashed}]| ~ \& x \& x \& x \& x \\
        |[text height=0.2cm]|      ~                                              \\
        |[back={draw=none,fill=none}]| $x:$                   \& x \& x \& x \& x \\
                                                              \& x \&   \&   \&   \\
    };

\begin{pgfonlayer}{background}
    \only<2>{\fhighlight[color2]{m-2-2}{m-4-5}}
    \only<3>{\fhighlight[color3]{m-2-1}{m-4-4}}
\end{pgfonlayer}

\drawMyBackBoxes
\end{tikzpicture}
\end{frame}
\end{document}

Output 1

enter image description here

Output 2

enter image description here

Output 3

enter image description here

share|improve this answer
    
Thank you! This is a good solution for my example, but i need to do it with several shapes (also with some shapes intersecting more than one node) and think that declaring a new shape each time is too much work. Sorry if this was not clear enough, it is definitely mi fault. –  antosecret Dec 15 '12 at 23:44
    
@antosecret Yes, I added the solution before I could read the comments and added the beamer alternative after reading them. Can you add a full beamer MWE that shows some examples and how you would achieve them with a “re-drawing node” approach. I think that you will need either re-drawing (could be well done or hidden inside a custom macro) or PGF layers. –  Qrrbrbirlbel Dec 16 '12 at 0:08
    
@antosecret I’ve updated my answer with ideas of re-drawing solutions. Though, I’m still waiting for a solution without re-drawing. –  Qrrbrbirlbel Dec 16 '12 at 7:06
    
@Qrrbrbirlbel: here it is one :). BTW: +1 for your nice solution! –  Claudio Fiandrino Dec 16 '12 at 9:07
    
@ClaudioFiandrino I should have said “re-use” instead of “re-draw”: Your solution is similar to my attemp of solution 1, I wanted to do the drawing of the rectangles inside an back/append after command that uses the drawing and filling options given to my custom back style. But sadly, doing this inside an pgfonlayer environment disturbs the localisation of nodes named as m-2-3. It would have had the advantage of not having any “a posteriori” (= re-use) commands. –  Qrrbrbirlbel Dec 16 '12 at 11:11
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.