我在用Qrrbrbirlbel 的解决方案构建如图所示的混淆矩阵。问题是,当将行和列扩展到 10 时(如代码所示),它给出了标题中所述的错误。我必须绘制 14*14 矩阵。请帮忙
\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}
答案1
问题似乎来自于\w
(可能相关)。
最简单的解决方法是删除该定义并手动写出\multicolumn
第一行的命令:
\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}
输出: