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 am trying to make a table similar to: How to construct a confusion matrix in LaTeX?

I prefer to do it in tikz. I would also like to highlight 2 cells in the same row or 2 cells in the same column as in: Highlighting table cells If the 2 cells cannot be circled, highlighting them in another way would also be awesome.

I would also like to do this in beamer.

I am also not fully sure how to center this figure--or generally--how to adjust its position within the beamer slide.

I would be very grateful for any help! Thank you in advance.

I have modified the tikz code in the earlier solution on just setting up the box (without any highlighting), but when add \begin{frame} and \end{frame} it no longer compiles:

\documentclass[professionalfont, fleqn]{beamer}

\usepackage{tikz}
\usetikzlibrary{arrows,shapes,positioning,fit,shapes.misc}

\begin{document}

\begin{tikzpicture}[
box/.style={draw,rectangle,minimum size=1.0cm,text width=1cm,align=center}]
\matrix (conmat) [row sep=0cm,column sep=0cm] {
\node (tpos) [box,
label=left:\( \mathbf{p'} \),
label=above:\( \mathbf{p} \),
] {};
&
\node (fneg) [box,
label=above:\textbf{n},] {$\checkmark$};
\\
\node (fpos) [box,
label=left:\( \mathbf{n'} \),] {$\checkmark$};
&
\node (tneg) [box] {$\checkmark$};
\\
};
\node [left=.05cm of conmat,text width=1.5cm,align=center] {\textbf{actual \\ value}};
\node [above=.05cm of conmat] {\textbf{prediction outcome}};
\end{tikzpicture}


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

1 Answer

up vote 6 down vote accepted

Use the fragile option for the frame:

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

\begin{document}

\begin{frame}[fragile]

\centering
\begin{tikzpicture}[
  box/.style = {draw,rectangle,minimum size=1.0cm,text width=1cm,align=center}
]
\matrix (conmat) [row sep=0cm,column sep=0cm] {
\node (tpos) [box,label=left:\( \mathbf{p'} \),label=above:\( \mathbf{p} \),] {};
  & \node (fneg) [box,label=above:\textbf{n},] {$\checkmark$}; \\
\node (fpos) [box,label=left:\( \mathbf{n'} \),] {$\checkmark$};
  & \node (tneg) [box] {$\checkmark$}; \\
};

\node [left=.05cm of conmat,text width=1.5cm,align=center] {\textbf{actual \\ value}};
\node [above=.05cm of conmat] {\textbf{prediction outcome}};
\end{tikzpicture}

\end{frame}

\end{document}

enter image description here

To control the positioning, you can use some standard commands such as \centering, \vfill, \vspace or \hspace.

An example showing two alternatives to fill columns or rows; the first one using an argumant for the box style (each \node can then be filled with an independent color) and the second one using column <number>/.style, and/or row <number>/.style to apply the color to a whole column or row:

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

\begin{document}

\begin{frame}[fragile]

\vspace*{-1cm}\hspace*{2cm}
\begin{tikzpicture}[
  box/.style = {draw,rectangle,minimum size=1.0cm,text width=1cm,align=center,fill=#1}
]
\matrix (conmat) [row sep=0cm,column sep=0cm] {
\node (tpos) [box=white,label=left:\( \mathbf{p'} \),label=above:\( \mathbf{p} \),] {};
  & \node (fneg) [box=blue!40,label=above:\textbf{n},] {$\checkmark$}; \\
\node (fpos) [box=white,label=left:\( \mathbf{n'} \),] {$\checkmark$};
  & \node (tneg) [box=blue!40] {$\checkmark$}; \\
};

\node [left=.05cm of conmat,text width=1.5cm,align=center] {\textbf{actual \\ value}};
\node [above=.05cm of conmat] {\textbf{prediction outcome}};
\end{tikzpicture}

\begin{tikzpicture}[column 1/.style={every node/.style={fill=red!30}},
  box/.style = {draw,rectangle,minimum size=1.0cm,text width=1cm,align=center}
]
\matrix (conmat) [row sep=0cm,column sep=0cm] {
\node (tpos) [box,label=left:\( \mathbf{p'} \),label=above:\( \mathbf{p} \),] {};
  & \node (fneg) [box,label=above:\textbf{n},] {$\checkmark$}; \\
\node (fpos) [box,label=left:\( \mathbf{n'} \),] {$\checkmark$};
  & \node (tneg) [box] {$\checkmark$}; \\
};

\node [left=.05cm of conmat,text width=1.5cm,align=center] {\textbf{actual \\ value}};
\node [above=.05cm of conmat] {\textbf{prediction outcome}};
\end{tikzpicture}
\begin{tikzpicture}[row 2/.style={every node/.style={fill=olive!30}},
  box/.style = {draw,rectangle,minimum size=1.0cm,text width=1cm,align=center}
]
\matrix (conmat) [row sep=0cm,column sep=0cm] {
\node (tpos) [box,label=left:\( \mathbf{p'} \),label=above:\( \mathbf{p} \),] {};
  & \node (fneg) [box,label=above:\textbf{n},] {$\checkmark$}; \\
\node (fpos) [box,label=left:\( \mathbf{n'} \),] {$\checkmark$};
  & \node (tneg) [box] {$\checkmark$}; \\
};

\node [left=.05cm of conmat,text width=1.5cm,align=center] {\textbf{actual \\ value}};
\node [above=.05cm of conmat] {\textbf{prediction outcome}};
\end{tikzpicture}

\end{frame}

\end{document}

enter image description here

share|improve this answer
    
Thank you for this response! Where exactly do I place the \vspace and \hspace commands? Before the \begin{tikzpicture}? –  J G Jul 26 '12 at 2:16
    
Also, how can these cells be highlighted? I would like to highlight ether one of the rows or one of the columns. Either circling it or possibly just highlighting the 2 cells. –  J G Jul 26 '12 at 2:19
    
@JG To your first question; yes. For the second question, please see my updated answer. –  Gonzalo Medina Jul 26 '12 at 2:35
    
How can this work with the \only<1>{ } command? I'm getting compiling issues when i try that –  J G Jul 26 '12 at 12:39
    
@JG that depends on what exactly you are trying to do; please elaborate; even better, since apparently your question isn't directly related to the issue of this one, but it's related, I would suggest you to accept the answer to this one and to open a follow-up question. –  Gonzalo Medina Jul 26 '12 at 12:43
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.