3

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}

1 Answer 1

6

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

5
  • Thank you for this response! Where exactly do I place the \vspace and \hspace commands? Before the \begin{tikzpicture}?
    – J G
    Jul 26, 2012 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, 2012 at 2:19
  • @JG To your first question; yes. For the second question, please see my updated answer. Jul 26, 2012 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, 2012 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. Jul 26, 2012 at 12:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .