[Tex/LaTex] Confusion matrix in LaTeX

commentsmatricespatternplottables

I used the proposed example in the "confusion matrix using only LaTeX code"

I increased the number of classes and until 9×9 confusion matrix, everything worked very well, but for 10 or higher numbers it got some errors. I upload the example with 10 columns:

\documentclass{article}
\usepackage{graphicx}

\newcommand\MyBox[1]{%
  \fbox{\parbox[c][.7cm][c]{.7cm}{\centering #1}}%
}
\newcommand\MyVBox[1]{%
  \parbox[c][.7cm][c]{1cm}{\centering\bfseries #1}%
}  
\newcommand\MyHBox[2][\dimexpr.7cm+2\fboxsep\relax]{%
  \parbox[c][1cm][c]{#1}{\centering\bfseries #2}%
}  
\newcommand\MyTBox[10]{%
  \MyVBox{#1}\MyBox{#2}\hspace*{-\fboxrule}\MyBox{#3}\hspace*{-\fboxrule}%
  \MyBox{#4}\hspace*{-\fboxrule}%
  \MyBox{#5}\hspace*{-\fboxrule}%
  \MyBox{#6}\hspace*{-\fboxrule}%
  \MyBox{#7}\hspace*{-\fboxrule}%
  \MyBox{#8}\hspace*{-\fboxrule}%
  \MyBox{#9}\hspace*{-\fboxrule}%
  \MyBox{#10}\par\vspace{-\fboxrule}
}  

\begin{document}

{
\offinterlineskip
\raisebox{-5cm}[0pt][0pt]{\rotatebox[origin=c]{90}{\parbox[c][0pt][c]{1cm}{\textbf{Source2}\\[20pt]}}}\par
\hspace*{1cm}\MyHBox[\dimexpr5.1cm+6\fboxsep\relax]{Source1}\par
\hspace*{1cm}\MyHBox{A}\MyHBox{B}\MyHBox{C}
\MyHBox{D}\MyHBox{E}\MyHBox{F}\par

\MyTBox{A}{1}{2}{3}{4}{5}{6}{7}{8}{9}
\MyTBox{B}{2}{3}{4}{5}{6}{7}{8}{9}{10}
\MyTBox{C}{3}{4}{5}{6}{7}{8}{9}{10}{11}
\MyTBox{D}{4}{5}{6}{7}{8}{9}{10}{11}{12}
\MyTBox{E}{5}{6}{7}{8}{9}{10}{11}{12}{13}
\MyTBox{F}{6}{7}{8}{9}{10}{11}{12}{13}{14}
}

\end{document}

Best Answer

Rather than using 10 arguments for your macro you're better off using a comma separated list and then processing it using something like docsvlist from the etoolbox package. With this small modification your code produces:

enter image description here

Here's the full code:

\documentclass{article}
\usepackage{graphicx}

\newcommand\MyBox[1]{%
  \fbox{\parbox[c][.7cm][c]{.7cm}{\centering #1}}%
}
\newcommand\MyVBox[1]{%
  \parbox[c][.7cm][c]{1cm}{\centering\bfseries #1}%
}
\newcommand\MyHBox[2][\dimexpr.7cm+2\fboxsep\relax]{%
  \parbox[c][1cm][c]{#1}{\centering\bfseries #2}%
}
\usepackage{etoolbox}
\newcommand\MyTBox[3]{%
  \MyVBox{#1}
  \renewcommand*\do[1]{\MyBox{##1}\hspace*{-\fboxrule}}
  \docsvlist{#2}
  \MyBox{#3}\par\vspace{-\fboxrule}
}

\begin{document}

{
\offinterlineskip
\raisebox{-5cm}[0pt][0pt]{\rotatebox[origin=c]{90}{\parbox[c][0pt][c]{1cm}{\textbf{Source2}\\[20pt]}}}\par
\hspace*{1cm}\MyHBox[\dimexpr5.1cm+6\fboxsep\relax]{Source1}\par
\hspace*{1cm}\MyHBox{A}\MyHBox{B}\MyHBox{C}
\MyHBox{D}\MyHBox{E}\MyHBox{F}\par

\MyTBox{A}{1, 2, 3, 4, 5, 6, 7, 8}{9}
\MyTBox{B}{2, 3, 4, 5, 6, 7, 8, 9}{10}
\MyTBox{C}{3, 4, 5, 6, 7, 8, 9, 10}{11}
\MyTBox{D}{4, 5, 6, 7, 8, 9, 10, 11}{12}
\MyTBox{E}{5, 6, 7, 8, 9, 10, 11, 12}{13}
\MyTBox{F}{6, 7, 8, 9, 10, 11, 12, 13}{14}
}

\end{document}

I made \MyTBox have three arguments as there is "special" processing for the header, body and footer respectively. This said, I'd probably do this differently using something like tikz, although this really depends on what you are really putting inside these macros:) -- but I like your macros too!