我有一个关于在 Latex 中表格的并排定位的问题。我找到了一个解决方案,但是,我还想在一个单元格中有一个对角线,这就是为什么我需要 tikz 环境,不过,拟合看起来不太好(居中、距离等)。有人能给我一些建议,告诉我如何以更漂亮的方式对齐表格吗?
\begin{document}
\usepackage{table}
\usepackage{tikz}
\usetikzlibrary{positioning, fit}
\begin{table}[]
\centering
\begin{tabular}{ll|c|c|l}
\cline{3-4}& & \multicolumn{2}{c|}{true categories} & \\ \cline{3-4}
& & 1 & 0 & \\ \cline{1-4}
\multicolumn{1}{|c|}{\parbox[t]{4mm}{\multirow{2}{*}{\rotatebox[]{90}{\centering predicted}}}}
& \multicolumn{1}{c|}{1} & True Positive & \begin{tabular}[c]{@{}c@{}}False Positive\\ (Type I error)\end{tabular} & \\ \cline{2-4}
\multicolumn{1}{|c|}{} & \multicolumn{1}{c|
{0} & \begin{tabular}[c]{@{}c@{}}False Negative\\ (Type II error)\end{tabular} & True Negative & \\ \cline{1-4}
\end{tabular}
\caption{The confusion matrix for predicted and true categories.}
\end{table}
\end{document}
第二个是:
\begin{table}[!htb] \centering
\begin{subtable}
\begin{tikzpicture}[x=1.5cm,y=0.5cm]
\draw (0,0) grid [step=1] (3,3);
\node at (1.5,2.5) {\textbf{1}};
\node at (2.5,2.5) {\textbf{0}};
\node at (0.5,1.5) {\textbf{1}};
\node at (0.5,0.5) {\textbf{0}};
\node at (1.5,1.5) {331};
\node at (2.5,1.5) {25};
\node at (1.5,0.5) {0};
\node at (2.5,0.5) {350};
\draw (0,3) -- (1,2);
\node at (1.0,3.0) [below left,inner sep=1pt] {\small$\textbf{t}$};
\node at (0.0,2.0) [above right,inner sep=1pt] {\small$\textbf{p}$};
\end{tikzpicture}
\end{subtable} \begin{subtable}
\begin{tikzpicture}[x=1.5cm,y=0.5cm]
\draw (0,0) grid [step=1] (3,3);
\node at (1.5,2.5) {\textbf{1}};
\node at (2.5,2.5) {\textbf{0}};
\node at (0.5,1.5) {\textbf{1}};
\node at (0.5,0.5) {\textbf{0}};
\node at (1.5,1.5) {331};
\node at (2.5,1.5) {25};
\node at (1.5,0.5) {0};
\node at (2.5,0.5) {350};
\draw (0,3) -- (1,2);
\node at (1.0,3.0) [below left,inner sep=1pt] {\small$\textbf{t}$};
\node at (0.0,2.0) [above right,inner sep=1pt] {\small$\textbf{p}$};
\end{tikzpicture}
\end{subtable}
\caption{The confusion matrices for the estimates are given. The left table shows the estimates applied to the training set with an accuracy of $0.964$. The table on the right hand side shows the estimates applied on the test set with an accuracy of $0.966$.}
\end{table}
答案1
我只是想与其他可能寻求类似方法的人分享我的解决方案。
\begin{table}[!htb]
\begin{minipage}{0.4\textwidth}
\centering
\begin{tabular}{|c| c | c |}
\hline
\diagbox{\textbf{\footnotesize{Pred.}}}{\textbf{\footnotesize{True}}}
& \mathbf{$1$} & \textbf{$0$} \\ \hline
\textbf{$1$} & $331$ & $25$ \\ \hline
\textbf{$0$} & $0$ & $350$ \\ \hline
\end{tabular}
\end{minipage} \begin{minipage}{0.4\textwidth}
\centering
\begin{tabular}{|c| c | c |}
\hline
\diagbox{\textbf{\footnotesize{Pred.}}}{\textbf{\footnotesize{True}}}
& $1$ & $0$ \\ \hline
$1$ & $153$ & $11$ \\ \hline
$0$ & $0$ & $150$ \\ \hline
\end{tabular}
\end{minipage}
\caption{}
\end{table}
答案2
对于第一个表格(颜色太多,但您可以更改或删除它们),您可以尝试这个。
\documentclass{article}
\usepackage{xcolor}
\usepackage{colortbl} % Für farbige Tabellen
\usepackage{ragged2e} % Für besseres links-, rechtsbündig
\usepackage{multirow}
\usepackage{hhline}
\definecolor{Cts1}{HTML}{5B86C9}
\definecolor{Cts2}{HTML}{4C84DC}
\definecolor{Cts3}{HTML}{0947A9}
\definecolor{Cts4}{HTML}{032357}
\definecolor{Cts5}{HTML}{626FCE}
\definecolor{Cts6}{HTML}{54A2C4}
\definecolor{Cts7}{HTML}{43ACD9}
\definecolor{Cts8}{HTML}{0472A1}
\definecolor{Cts9}{HTML}{02364C}
\definecolor{Cts10}{HTML}{013B53}
\definecolor{Cts11}{HTML}{040E5B}
\newcolumntype{C}[1]{>{\hspace{0pt}\Centering}p{#1}}
\newcommand{\mcc}[2]{\multicolumn{#1}{!{\vlineTS}c!{\vlineTS}}{#2}}
\newcommand{\mccc}[3]{\multicolumn{#1}{!{\vlineTS}>{\cellcolor{Cts#2}}c!{\vlineTS}}{#3}}
\begin{document}
\renewcommand{\arraystretch}{1.8}
\newcommand{\vlineTS}[1][1.2pt]{\color{gray!70}\vrule width #1}
\setlength\arrayrulewidth{1.2pt}\arrayrulecolor{gray!70}%
\begin{tabular}[t]{|C{4.4cm}|C{3.5cm}|C{4.15cm}|C{4.15cm}|}\hhline{*{4}{|-}}
\mcc{2}{\cellcolor{Cts1}} & \multicolumn{2}{!{}>{\cellcolor{Cts2}}c!{\vlineTS}}{\color{white}Zustand der Wirklichkeit} \\\hhline{*{2}{|>{\arrayrulecolor{Cts1}}-}>{\arrayrulecolor{gray!70}}*{2}{|-}}
\mccc{2}{1}{\multirow{-2}*{\color{white}Fehler beim Testen von Hypothesen}} & \cellcolor{Cts3}\color{white}$H_{0}$ ist wahr & \cellcolor{Cts6}\color{white}\color{white}$H_{0}$ ist falsch \\\hhline{*{4}{|-}}
\multicolumn{1}{!{\vlineTS}>{\cellcolor{Cts7}}c!{}}{\multirow{2}*{\cellcolor{Cts7}}} & \mccc{1}{8}{\color{white}abgelehnt} &
\multicolumn{1}{!{}>{\cellcolor{Cts11!70}}c!{\vlineTS}}{\color{white}Fehler 1. Art} & \multicolumn{1}{!{}>{\cellcolor{Cts11}}c!{\vlineTS}}{\color{white}Richtige Entscheidung} \\\hhline{*{1}{|>{\arrayrulecolor{Cts7}}-}>{\arrayrulecolor{gray!70}}*{3}{-}}
\multicolumn{1}{!{\vlineTS}>{\cellcolor{Cts7}}c!{}}{\raisebox{0.52cm}[0.52cm]{\color{white}Nullhypothese $H_{0}$ wird}} &
\mccc{1}{7}{\color{white}nicht abgelehnt} &
\multicolumn{1}{!{}>{\cellcolor{Cts9}}c!{\vlineTS}}{\color{white}Richtige Entscheidung} & \multicolumn{1}{!{}>{\cellcolor{Cts10!80}}c!{\vlineTS}}{\color{white}Fehler 2. Art} \\\hhline{*{4}{|-}}
\end{tabular}
\end{document}