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.

Is it possible to make a box around two nodes in a tikzpicture? Specifically, I am making a 2 by 2 box, and I have some labels outside of the box. For example, I have a + and a 0 in the same row and underneath and box. Could I make some sort of circle (oval) around them? Similarly, for the 0 and the - on the right side of the box, outside of it? Can these circles both extend to include that plus sign on the bottom right outside the box?

For an approximate graphic of what the code produces, see: making a box in beamer -- adding labels diagonally

Thank you very much for your help!

\documentclass[professionalfont, fleqn]{beamer}
 \usepackage{tikz}
 \usetikzlibrary{arrows,shapes,positioning,fit,shapes.misc,matrix}

% commands for the cell colors
\def\coloroneone#1{\def\Coloroneone{#1}}
\def\coloronetwo#1{\def\Coloronetwo{#1}}
\def\colortwoone#1{\def\Colortwoone{#1}}
\def\colortwotwo#1{\def\Colortwotwo{#1}}

\begin{document}

\begin{frame}

% initial colors
\coloroneone{blue!60}
\coloronetwo{white}
 \colortwoone{blue!60}
\colortwotwo{white}

\only<1-4>{%
\centering
\begin{tikzpicture}

 % definitions of colors for the four initial slides
 \only<2>{% colors for second slide
  \coloroneone{white}
\coloronetwo{blue!60}
\colortwoone{white} 
\colortwotwo{blue!60}}
 \only<3>{% colors for third slide
 \coloroneone{red!30}
 \coloronetwo{red!30}
 \colortwoone{white}
 \colortwotwo{white}}
 \only<4>{% colors for fourth slide
\coloroneone{white}
 \colortwoone{red!30}
\colortwotwo{red!30}}

 % the matrix
\matrix 
[ 
matrix of nodes,
ampersand
 replacement=\&,
 nodes={draw,rectangle,minimum size=1.0cm,text width=2cm,text depth=1cm,text height=.8cm,align=center,inner sep=0pt},
row sep=0cm,
 column sep=0cm
] (mat)
{
|[fill=\Coloroneone]|  
\& 
|[fill=\Coloronetwo]| Smaller \\
|[fill=\Colortwoone]| Smaller
\& 
|[fill=\Colortwotwo]|  \\
};

 % the labels
\node[left=.5cm of mat,text width=1.5cm,align=center] {\textbf{actual \\ value}};
 \node[above=.5cm of mat] {\textbf{prediction}};

\node[anchor=south] at (mat-1-1.north) {$\mathbf{p}$};
\node[anchor=south] (n) at (mat-1-2.north) {$\mathbf{n}$};
\node[anchor=east] at (mat-1-1.west) {$\mathbf{p'}$};
\node[anchor=east] (np) at (mat-2-1.west) {$\mathbf{n'}$};

\only<1-4>{\node[anchor=north]  at (mat-2-1.south) [below = 2pt+7pt]{$\textbf{{+}}$};}
\only<2-4>{\node[anchor=north]  at (mat-2-2.south) [below = 2pt+7pt]{$\textbf{{0}}$};}

\only<3-4>{\node[anchor=west]  at (mat-1-2.east)  [right = 2pt+10pt]{$\textbf{{0}}$};}
\only<4-4>{\node[anchor=west]  at (mat-2-2.east)  [right = 2pt+10pt]{$\textbf{{--}}$};}

\only<1-4>\node[anchor=north] at ([yshift=-8.75mm, xshift=-2mm]np.south){\textbf{A}};
\only<3-4>\node[anchor=west] at ([xshift=8mm]n.east)  {\textbf{D}};

 \only<4-4>\node[font=\color{red}, anchor=north] at ([yshift=-9.5mm, xshift=51mm]np.south){\textbf{+}};

 \end{tikzpicture}
 }

 \only<5>{Some text}

 \end{frame}

 \end{document}
share|improve this question
add comment

1 Answer

up vote 9 down vote accepted

Just name your nodes and use the fit library. Also the class option is obsolete (see the warnings after compilations)

\documentclass{beamer}
\usefonttheme{professionalfonts}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes,positioning,fit,shapes,matrix}

% commands for the cell colors
\def\coloroneone#1{\def\Coloroneone{#1}}
\def\coloronetwo#1{\def\Coloronetwo{#1}}
\def\colortwoone#1{\def\Colortwoone{#1}}
\def\colortwotwo#1{\def\Colortwotwo{#1}}

\begin{document}

\begin{frame}

% initial colors
\coloroneone{blue!60}
\coloronetwo{white}
\colortwoone{blue!60}
\colortwotwo{white}

\only<1-5>{%
\centering
\begin{tikzpicture}

 % definitions of colors for the four initial slides
\only<2>{% colors for second slide
\coloroneone{white}
\coloronetwo{blue!60}
\colortwoone{white} 
\colortwotwo{blue!60}}
\only<3>{% colors for third slide
\coloroneone{red!30}
\coloronetwo{red!30}
\colortwoone{white}
\colortwotwo{white}}
\only<4>{% colors for fourth slide
\coloroneone{white}
\colortwoone{red!30}
\colortwotwo{red!30}}

 % the matrix
\matrix 
[ 
matrix of nodes,
ampersand
 replacement=\&,
 nodes={draw,rectangle,minimum size=1.0cm,text width=2cm,text depth=1cm,text height=.8cm,align=center,inner sep=0pt},
row sep=0cm,
 column sep=0cm
] (mat)
{
|[fill=\Coloroneone]|  
\& 
|[fill=\Coloronetwo]| Smaller \\
|[fill=\Colortwoone]| Smaller
\& 
|[fill=\Colortwotwo]|  \\
};

 % the labels
\node[left=.5cm of mat,text width=1.5cm,align=center] {\textbf{actual \\ value}};
 \node[above=.5cm of mat] {\textbf{prediction}};

\node[anchor=south] at (mat-1-1.north) {$\mathbf{p}$};
\node[anchor=south] (n) at (mat-1-2.north) {$\mathbf{n}$};
\node[anchor=east] at (mat-1-1.west) {$\mathbf{p'}$};
\node[anchor=east] (np) at (mat-2-1.west) {$\mathbf{n'}$};

\only<1->{\node[anchor=north]  (plusnode) at (mat-2-1.south) [below = 2pt+7pt]{$\textbf{{+}}$};}
\only<2->{\node[anchor=north]  (bottomzeronode) at (mat-2-2.south) [below = 2pt+7pt]{$\textbf{{0}}$};}

\only<3->{\node[anchor=west]  (rightzeronode) at (mat-1-2.east)  [right = 2pt+10pt]{$\textbf{{0}}$};}
\only<4->{\node[anchor=west]  (dashednode) at (mat-2-2.east)  [right = 2pt+10pt]{$\textbf{{--}}$};}

\only<1->\node[anchor=north] at ([yshift=-8.75mm, xshift=-2mm]np.south){\textbf{A}};
\only<3->\node[anchor=west] at ([xshift=8mm]n.east)  {\textbf{D}};

\only<4->\node[font=\color{red}, anchor=north] (diagplusnode)at ([yshift=-9.5mm, xshift=51mm]np.south){\textbf{+}};
\only<5->\node[ellipse,fit=(plusnode)(bottomzeronode),draw] {};
\only<5->\node[ellipse,fit=(dashednode)(rightzeronode)(diagplusnode),draw] {};
\end{tikzpicture}
}

\only<6>{Some text}

 \end{frame}

\end{document}

I really really recommend you to go through the manuals of beamer and TikZ. Simple examples would give you enough affinity to the concepts given here such as the libraries and other details.

enter image description here

share|improve this answer
    
I've upvoted your answer just for the last sentence ;-) –  Ignasi Jul 28 '12 at 6:38
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.