0

I am using Qrrbrbirlbel's solution to build the confusion matrix as given in the figure. The problem is when extend the rows and column to 10 as given in the code it gives me the error as stated in the title. I have to draw 14*14 matrix. Kindly help

      \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{~*{10}{|-}|}}
\makeatother

\begin{document}
    \begin{tabular}{l*{10}{|>{\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}\& \w{cl} & \w{rn}\& \w{cl} & \w{rn}\jline
        ving & \ccol{1} & 0 & 0 & 0 & 0 & 0& 0 & 0& 0 & 0\jline
        xing & 0 & \ccol{0.92} & \ccol{0.08} & 0 & 0 & 0 & 0 & 0& 0 & 0\jline
        king & 0 & \ccol{0.03} & \ccol{0.97} & 0 & 0 & 0& 0 & 0& 0 & 0\jline
        ging & 0 & 0 & 0 & \ccol{1} & 0 & 0& 0 & 0& 0 & 0\jline
        ping & 0 & 0 & 0 & 0 & \ccol{1} & 0& 0 & 0& 0 & 0\jline
        ning & 0 & 0 & 0 & 0 & 0 & \ccol{1}& 0 & 0& 0 & 0\jline
        ning & 0 & 0 & 0 & 0 & 0 & \ccol{1}& 0 & 0& 0 & 0\jline
        ning & 0 & 0 & 0 & 0 & 0 & \ccol{1}& 0 & 0& 0 & 0\jline
        ning & 0 & 0 & 0 & 0 & 0 & \ccol{1}& 0 & 0& 0 & 0\jline
        ning & 0 & 0 & 0 & 0 & 0 & \ccol{1}& 0 & 0& 0 & 0\jline
        \noalign{\global\let\w\undefined}
    \end{tabular}
\end{document}

enter image description here

  • 2
    Start with *{10} not *10. – daleif Feb 23 '19 at 7:27
  • Thank you Daleif, i have replaced it and now its giving this error: Misplaced \omit. ...\w{wk} & \w{jg} & \w{cl} & \w{rn}\& \w{cl} – umar Feb 23 '19 at 7:36
  • 1
    Then update your question. I wouldn't define \w at that location. – daleif Feb 23 '19 at 7:38
  • 4
    I'm voting to close this question as off-topic because the issue is due to a typo. – egreg Feb 23 '19 at 9:43
  • 3
    What's the purpose of defining \w inside \noalign and to make it globally undefined at the end? This will overwrite any standing definition of \w. Thus it's much better to do \newcommand{\w}{...} in the preamble. – egreg Feb 23 '19 at 10:11
2

The problem appears to be comint from the definition of \w (possibly related).

The simplest fix would be to remove that definition and just manually write out the \multicolumn command for the first row:

    \begin{tabular}{l*{10}{|>{\centering\arraybackslash}m{\cellwidth}}|}
        % \noalign{\gdef\w#1{\multicolumn{1}{c}{#1}}}
        \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} & \multicolumn{1}{c}{cl} & \multicolumn{1}{c}{rn} & \multicolumn{1}{c}{cl} & \multicolumn{1}{c}{rn}\jline
        ving & \ccol{1} & 0 & 0 & 0 & 0 & 0& 0 & 0& 0 & 0\jline
        xing & 0 & \ccol{0.92} & \ccol{0.08} & 0 & 0 & 0 & 0 & 0& 0 & 0\jline
        king & 0 & \ccol{0.03} & \ccol{0.97} & 0 & 0 & 0& 0 & 0& 0 & 0\jline
        ging & 0 & 0 & 0 & \ccol{1} & 0 & 0& 0 & 0& 0 & 0\jline
        ping & 0 & 0 & 0 & 0 & \ccol{1} & 0& 0 & 0& 0 & 0\jline
        ning & 0 & 0 & 0 & 0 & 0 & \ccol{1}& 0 & 0& 0 & 0\jline
        ning & 0 & 0 & 0 & 0 & 0 & \ccol{1}& 0 & 0& 0 & 0\jline
        ning & 0 & 0 & 0 & 0 & 0 & \ccol{1}& 0 & 0& 0 & 0\jline
        ning & 0 & 0 & 0 & 0 & 0 & \ccol{1}& 0 & 0& 0 & 0\jline
        ning & 0 & 0 & 0 & 0 & 0 & \ccol{1}& 0 & 0& 0 & 0\jline
        % \noalign{\global\let\w\undefined}
    \end{tabular}

Output:

output

|improve this answer|||||
  • Thank you very much. There is only one problem left that this in textstudio editor there are error appearing on & (highlighted with red color) and when i bring the mouse cursor over it gives(more cols then specified). Should i add the snapshot of it ? – umar Feb 23 '19 at 9:09
  • Does the message go if you change l*{10} to l*{11}? – Pippip19 Feb 23 '19 at 9:10
  • It remains the same even by replacing with 1*{11}. – umar Feb 23 '19 at 9:43
  • It is difficult for me to tell because I don't have texstudio. Perhaps the program does not recognise the \jline command. Does it still compile? If so, I wouldn't worry about it. – Pippip19 Feb 23 '19 at 9:56
  • Yes, It compile perfectly.. I was only worried about the warnings that are appearing on the & by highlighting with red color. – umar Feb 23 '19 at 10:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.