我正在尝试绘制下图:
我找到了一个创建表格的示例confusion matrix using only LaTeX code
并尝试将解决方案调整到我的表格
\documentclass{article}
%\usepackage{ifthen}
%\usetikzlibrary{matrix,calc}
\usepackage{graphicx}
\newcommand\MyBox[1]{%
\fbox{\parbox[c][1.7cm][c]{1.7cm}{\centering #1}}%
}
\newcommand\MyVBox[1]{%
\parbox[c][1.7cm][c]{1cm}{\centering\bfseries #1}%
}
\newcommand\MyHBox[2][\dimexpr1.7cm+2\fboxsep\relax]{%
\parbox[c][1cm][c]{#1}{\centering\bfseries #2}%
}
\newcommand\MyTBox[4]{%
\MyVBox{#1}\MyBox{#2}\hspace*{-\fboxrule}%
\MyBox{#3}\hspace*{-\fboxrule}%
\MyBox{#4}\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{LH}\MyHBox{RH}\MyHBox{Other contact}\par
\MyTBox{Type 0}{25}{0}{0}
\MyTBox{Type 1}{1}{43}{1}
\MyTBox{Undetected{0}{5}{-}
}
\end{document}
但它无法编译。它显示Missing \endcsname inserted.
错误。
我想生成上面的图形,如果可能的话,使用 tikz 和独立的文档类。
答案1
\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{matrix, positioning}
\begin{document}
\begin{tikzpicture}[font=\sffamily]
\matrix[matrix of nodes, nodes in empty cells, nodes={anchor=center, minimum width=3cm, minimum height=2cm, align=center, draw, text width=1.7cm}, column sep=-\pgflinewidth, row sep=-\pgflinewidth,
row 1/.style={nodes={draw=none}},
column 1/.style={nodes={draw=none}}] (A)
{
& LH & RH & {Other contact} & {Other\\ No contact}\\
Type 0 & 25 & 0 & 0 & 3\\
Type 1 & 1 & 43 & 1 & 2\\
Undetected & 0 & 5 & -- & --\\
};
\draw[green!80!black, line width=1mm] (A-2-2.north west) rectangle (A-3-3.south east);
\draw (A-2-2.north west)-- node[above right]{Real} node[below left]{AI model} (A-1-1.north west);
\end{tikzpicture}
\end{document}
答案2
以下是使用 来实现这一{NiceTabular}
目的的一种方法nicematrix
。
\documentclass{article}
\usepackage{xcolor}
\usepackage{nicematrix}
\begin{document}
\begin{center}
\setlength{\tabcolsep}{10pt}
\renewcommand{\arraystretch}{1.5}
\begin{NiceTabular}{cccc}[hvlines,first-col,first-row,columns-width=auto]
\diagbox{Al model}{Real}
& LH & RH & \Block{}{Other\\ contact} & \Block{}{Other\\ No contact} \\
Type 0 \quad & \Block[draw=green,line-width=1pt,transparent]{2-2}{}
25 & 0 & 0 & 3 \\
Type 1 \quad & 1 & 43 & 1 & 2 \\
Undetected \quad & 0 & 5 & -- & --
\end{NiceTabular}
\end{center}
\end{document}
您需要多次编译(因为nicematrix
在后台使用 PGF/Tikz 节点)。