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 making a confusion matrix for a document, and I was wondering if LaTeX has the capabilities to handle this. It is essentially a table, but with background shading corresponding to the value in the table (the shading could be hand-coded).

How can this be done in LaTeX? Is it possible? An example ishttp://i.stack.imgur.com/mLxlO.png

share|improve this question
    
Welcome to TeX.sx! –  Kurt Nov 1 '12 at 16:12
2  
Possible duplicate: tex.stackexchange.com/questions/42444/… –  mythealias Nov 1 '12 at 16:31
    
@mythealias That looks like what I want, but when I include pgfplotstable into my document, I get a "command already defined" error, due to a conference paper's style file. –  shoes Nov 1 '12 at 17:09
add comment

3 Answers

up vote 5 down vote accepted

Building on Qrrbrbirlbel's answer, the collcell package can be used to automatically apply the color to each cell without the need to explicitly use a macro; a little example (notice particularly the second matrix):

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{collcell}
\usepackage{hhline}
\usepackage{pgf}

\newcommand\gray{gray}

\newcommand\ColCell[1]{%
  \pgfmathparse{#1<.8?1:0}%
    \ifnum\pgfmathresult=0\relax\color{white}\fi
  \pgfmathparse{1-#1}%
  \expandafter\cellcolor\expandafter[%
    \expandafter\gray\expandafter]\expandafter{\pgfmathresult}#1}

\newcolumntype{E}{>{\collectcell\ColCell}c<{\endcollectcell}}

\begin{document}

\noindent\begin{tabular}{c*{6}{|E}|}
 \multicolumn{1}{c}{} & \multicolumn{1}{c}{hw} & \multicolumn{1}{c}{bx} 
  & \multicolumn{1}{c}{wk} & \multicolumn{1}{c}{jg} & \multicolumn{1}{c}{cl} 
  & \multicolumn{1}{c}{rn} \\ \hhline{~*6{|-}|}
 ving & 1 & 0 & 0 & 0 & 0 & 0 \\ \hhline{~*6{|-}|}
 xing & 0 & 0.92 & 0.08 & 0 & 0 & 0 \\ \hhline{~*6{|-}|} 
 king & 0 & 0.03 & 0.97 & 0 & 0 & 0 \\ \hhline{~*6{|-}|}
 ging & 0 & 0 & 0 & 1 & 0 & 0 \\ \hhline{~*6{|-}|}
 ping & 0 & 0 & 0 & 0 & 1 & 0 \\ \hhline{~*6{|-}|}
 ning & 0 & 0 & 0 & 0 & 0 & 1 \\ \hhline{~*6{|-}|}
\end{tabular}\par\bigskip

\noindent\begin{tabular}{ *{6}{E}}
 0.2 & 0.3 & 0.2 & 0.86 & 0 & 1 \\
 0.87 & 0.65 & 0.43 & 0.6 & 1 & 0.3 \\
 0.17 & 0.25 & 0.83 & 0.23 & 0.7 & 0.5 \\
 0.72 & 0.45 & 1 & 0.5 & 0.43 & 0.85 \\
 0.1 & 0.15 & 0 & 0.65 & 0.83 & 0.25 \\
 0 & 1 & 0.4 & 0.51 & 0.63 & 0.75 \\
\end{tabular}

\end{document}

enter image description here

share|improve this answer
    
What does #1<.8?1:0 mean? Why the ?? –  azetina Nov 2 '12 at 3:17
    
@azetina In pgf, x ?y :z is a shorthand for if x then y else z. In this concrete example, if the argument is less than 0.8, then color black (1) will be used; otherwise, color white (0) will be used. –  Gonzalo Medina Nov 2 '12 at 3:29
1  
@azetina It is a widely used ternary operator. –  Qrrbrbirlbel Nov 2 '12 at 3:50
add comment

xcolor is used for the colors.
hhline is used for horizontal lines that don't get covered by \cellcolor.
pgf is used for the calculations.

The command \gray is only defined to reduce the amount of \expandafters.
The length \tabwidth is used so that all columns have the same width.

Code

\RequirePackage[table]{xcolor}
\documentclass{article}
\usepackage{array,hhline}
\usepackage{pgf}

\newcommand*{\gray}{gray}
\newcommand*{\woB}[1]{\multicolumn{1}{c}{#1}}

\newcommand*{\colorme}[1]{%
    \pgfmathparse{#1<.5?1:0}%
    \ifnum\pgfmathresult=0\relax\color{white}\fi
    \pgfmathparse{1-#1}
    \expandafter\cellcolor\expandafter[\expandafter\gray\expandafter]\expandafter{\pgfmathresult}%
    #1%
}
\newlength{\tabwidth}
\settowidth{\tabwidth}{0.00}
\begin{document}
\begin{tabular}{l*6{|>{\centering\arraybackslash}m{\tabwidth}}|}
    \woB{} & \woB{hw}      & \woB{bx}       & \woB{wk}       & \woB{jg}    & \woB{cl}    & \woB{rn}    \\ \hhline{~*6{|-}|}
     ving  & \colorme{1}   & 0              & 0              & 0           & 0           & 0           \\ \hhline{~*6{|-}|}
     xing  & 0             & \colorme{0.92} & \colorme{0.08} & 0           & 0           & 0           \\ \hhline{~*6{|-}|}
     king  & 0             & \colorme{0.03} & \colorme{0.97} & 0           & 0           & 0           \\ \hhline{~*6{|-}|}
     ging  & 0             & 0              & 0              & \colorme{1} & 0           & 0           \\ \hhline{~*6{|-}|}
     ping  & 0             & 0              & 0              & 0           & \colorme{1} & 0           \\ \hhline{~*6{|-}|}
     ning  & 0             & 0              & 0              & 0           & 0           & \colorme{1} \\ \hhline{~*6{|-}|}
\end{tabular}
\end{document}

Output

enter image description here

share|improve this answer
add comment

Here is Qrrbrbirlbel's solution without the mighty pgf.

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{array,hhline}
\makeatletter
\newcommand*{\ccol}[1]{%
  \ifdim#1pt<.5pt\relax\else\color{white}\fi
  \edef\x{\noexpand\cellcolor[gray]{\strip@pt\dimexpr1pt-#1pt}}\x
  #1%
}
\newlength{\cellwidth}
\settowidth{\cellwidth}{0.00}
\def\jline{\\\hhline{~*6{|-}|}}
\makeatother

\begin{document}
\begin{tabular}{l*6{|>{\centering\arraybackslash}m{\cellwidth}}|}
\noalign{\gdef\w#1{\multicolumn{1}{c}{#1}}}
\w{} & \w{hw} & \w{bx} & \w{wk} & \w{jg} & \w{cl} & \w{rn}\jline
ving & \ccol{1} & 0 & 0 & 0 & 0 & 0\jline
xing & 0 & \ccol{0.92} & \ccol{0.08} & 0 & 0 & 0 \jline
king & 0 & \ccol{0.03} & \ccol{0.97} & 0 & 0 & 0\jline
ging & 0 & 0 & 0 & \ccol{1} & 0 & 0\jline
ping & 0 & 0 & 0 & 0 & \ccol{1} & 0\jline
ning & 0 & 0 & 0 & 0 & 0 & \ccol{1}\jline
\noalign{\global\let\w\undefined}
\end{tabular}
\end{document}
share|improve this answer
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.