所以我写了下面的代码:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{multirow}
\usepackage[export]{adjustbox}
\begin{document}
\begin{table}[H]
\centering
\begin{tabular}{ccccccc}
\multicolumn{2}{c}{} &\multicolumn{5}{c}{Feature Vector of Samples} \\
\headerrow & & 0 & 1 & 2 & 3 & 4 \\
\multirow{5}{*}{\rotatebox{90}{Samples}}
& 0 & \(9.87*e^{-01}\) & \(8.67*e^{-17} \)& \(4.13*e^{-24}\) & \(1.16*e^{-17}\) & \(4.58*e^{-17}\) \\
& 1 & \(1.16*e^{-16}\) & \(9.99*e^{-01}\) & \(9.14*e^{-25}\) & \(3.63*e^{-22}\) & \(2.69*e^{-11}\) \\
& 2 & \(4.35*e^{-33}\) & \(7.85*e^{-34}\) & \(9.93*e^{-01}\) & \(3.74*e^{-04}\) & \(4.55*e^{-34}\) \\
& 3 & \(5.51*e^{-26}\)& \(5.13*e^{-31}\)& \(5.02*e^{-04}\) & \(9.99*e^{-01}\) & \(1.71*e^{-29}\) \\
& 4 & \(9.60*e^{-18}\) & \(2.21*e^{-07}\)& \(2.52*e^{-25}\)& \(4.64*e^{-20}\)& \(9.99*e^{-01}\)
\end{tabular}
\label{tbl:predan}
\end{table}
\end{document}
所以我想在表格中的一些变量周围放置一些框。如果颜色也可以选择就好了。就像下面的照片一样。
提前致谢 :)
答案1
使用最新版本(2021/03/23 的 v 5.13),您可以使用键和nicematrix
命令来绘制单元格的边框。\Block
draw
borders
\documentclass{article}
\usepackage{nicematrix}
\begin{document}
\NiceMatrixOptions{cell-space-limits=2pt}
\begin{NiceTabular}{ccccccc}
& \Block{1-5}{Feature Vector of Samples} \\
& & 0 & 1 & 2 & 3 & 4 \\
\Block{5-1}{\rotate Samples}
& 0 & \(9.87*e^{-01}\) & \(8.67*e^{-17} \)& \(4.13*e^{-24}\) & \(1.16*e^{-17}\) & \(4.58*e^{-17}\) \\
& 1 &
\Block[borders={top,bottom,left}]{}{\(1.16*e^{-16}\)}
& \Block[draw=red]{}{\(9.99*e^{-01}\)}
&
\Block[borders={top,bottom,right}]{}{\(9.14*e^{-25}\)}
& \(3.63*e^{-22}\) & \(2.69*e^{-11}\) \\
& 2 & \(4.35*e^{-33}\) & \(7.85*e^{-34}\) & \(9.93*e^{-01}\) & \(3.74*e^{-04}\) & \(4.55*e^{-34}\) \\
& 3 & \(5.51*e^{-26}\) & \(5.13*e^{-31}\)& \(5.02*e^{-04}\)& \(9.99*e^{-01}\) & \(1.71*e^{-29}\) \\
& 4 & \(9.60*e^{-18}\) & \(2.21*e^{-07}\) & \(2.52*e^{-25}\) & \(4.64*e^{-20}\) & \(9.99*e^{-01}\)
\end{NiceTabular}
\end{document}
如果您希望在数组后有格式化指令,这是可以的。在 的环境中nicematrix
,您可以在 中\CodeAfter
使用 Tikz 绘制您想要的任何规则。
\documentclass{article}
\usepackage{nicematrix,tikz}
\begin{document}
\NiceMatrixOptions{cell-space-limits=2pt}
\begin{NiceTabular}{ccccccc}
& \Block{1-5}{Feature Vector of Samples} \\
& & 0 & 1 & 2 & 3 & 4 \\
\Block{5-1}{\rotate Samples}
& 0 & \(9.87*e^{-01}\) & \(8.67*e^{-17} \) & \(4.13*e^{-24}\) & \(1.16*e^{-17}\) & \(4.58*e^{-17}\) \\
& 1 & \(1.16*e^{-16}\) & \(9.99*e^{-01}\) & \(9.14*e^{-25}\) & \(3.63*e^{-22}\) & \(2.69*e^{-11}\) \\
& 2 & \(4.35*e^{-33}\) & \(7.85*e^{-34}\) & \(9.93*e^{-01}\) & \(3.74*e^{-04}\) & \(4.55*e^{-34}\) \\
& 3 & \(5.51*e^{-26}\) & \(5.13*e^{-31}\)& \(5.02*e^{-04}\)& \(9.99*e^{-01}\) & \(1.71*e^{-29}\) \\
& 4 & \(9.60*e^{-18}\) & \(2.21*e^{-07}\) & \(2.52*e^{-25}\) & \(4.64*e^{-20}\) & \(9.99*e^{-01}\)
\CodeAfter
\begin{tikzpicture}
\draw (4-|4) -- (4-|3) -- (5-|3) -- (5-|4) ;
\draw (4-|5) -- (4-|6) -- (5-|6) -- (5-|5) ;
\draw [red] (4-|4) rectangle (5-|5) ;
\end{tikzpicture}
\end{NiceTabular}
\end{document}
使用这两种语法,输出如下。
您需要几个编译(因为nicematrix
使用 PGF/Tikz 节点)。
答案2
我建议使用以下代码及其基本包:
\documentclass{article}
\usepackage{array, multirow, bigstrut, float}
\usepackage[table]{xcolor}
\usepackage{hhline}
\usepackage[export]{adjustbox}
\begin{document}
\begin{table}[H]
\setlength{\arrayrulewidth}{0.8pt}
\centering
\begin{tabular}{*{7}{c}}
\multicolumn{2}{c}{} &\multicolumn{5}{c}{Feature Vector of Samples} \\
headerrow & & 0 & 1 & 2 & 3 & 4 \\
\multirow{5}{*}{\rotatebox{90}{Samples}}
& 0 & \(9.87*e^{-01}\) & \(8.67*e^{-17} \)& \(4.13*e^{-24}\) & \(1.16*e^{-17}\) & \(4.58*e^{-17}\) \\
\hhline{~~- >{\arrayrulecolor{red}}->{\arrayrulecolor{black}}-}
& 1 & \multicolumn{1}{|c}{\(1.16*e^{-16}\) \bigstrut[t]}&\multicolumn{1}{!{\color{red}\vrule width 0.8pt}c!{\color{red}\vrule width 0.8pt}}{ \(9.99*e^{-01}\) }& \multicolumn{1}{c|}{\(9.14*e^{-25}\)} & \(3.63*e^{-22}\) & \(2.69*e^{-11}\) \\
\hhline{~~- >{\arrayrulecolor{red}}->{\arrayrulecolor{black}}-}
& 2 & \(4.35*e^{-33}\) & \(7.85*e^{-34}\) & \(9.93*e^{-01}\) & \(3.74*e^{-04}\) & \(4.55*e^{-34}\) \\
& 3 & \(5.51*e^{-26}\)& \(5.13*e^{-31}\)& \(5.02*e^{-04}\) & \(9.99*e^{-01}\) & \(1.71*e^{-29}\) \\
& 4 & \(9.60*e^{-18}\) & \(2.21*e^{-07}\)& \(2.52*e^{-25}\)& \(4.64*e^{-20}\)& \(9.99*e^{-01}\)
\end{tabular}
\label{tbl:predan}
\end{table}
\end{document}
答案3
对于任何对答案感兴趣的人,我使用 tikzmark 来绘制框。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{multirow}
\usepackage[export]{adjustbox}
\usepackage{tikz}
\usetikzlibrary{fit,tikzmark}
\usepackage{booktabs,cellspace}
\begin{document}
\begin{table}
\centering
\begin{tabular}{ccccccc}
\multicolumn{2}{c}{} &\multicolumn{5}{c}{Feature Vector of Samples} \\
\headerrow & & 0 & 1 & 2 & 3 & 4 \\
\multirow{5}{*}{\rotatebox{90}{Samples}}
& 0 & \(9.87*e^{-01}\) & \(8.67*e^{-17} \)& \(4.13*e^{-24}\) & \(1.16*e^{-17}\) & \(4.58*e^{-17}\) \\
& 1 & \tikzmarknode{a}{\(1.16*e^{-16}\)} & \tikzmarknode{c}{\(9.99*e^{-01}\)} & \tikzmarknode{b}{\(9.14*e^{-25}\)} & \(3.63*e^{-22}\) & \(2.69*e^{-11}\) \\
& 2 & \(4.35*e^{-33}\) & \(7.85*e^{-34}\) & \(9.93*e^{-01}\) & \(3.74*e^{-04}\) & \(4.55*e^{-34}\) \\
& 3 & \(5.51*e^{-26}\)& \(5.13*e^{-31}\)& \(5.02*e^{-04}\) & \(9.99*e^{-01}\) & \(1.71*e^{-29}\) \\
& 4 & \(9.60*e^{-18}\) & \(2.21*e^{-07}\)& \(2.52*e^{-25}\)& \(4.64*e^{-20}\)& \(9.99*e^{-01}\)
\end{tabular}
\label{tbl:predan}
\end{table}
\begin{tikzpicture}[overlay,remember picture]
\node[draw=red, thin, inner sep=1.5pt, fit=(a) (b)] {};
\node[draw=green, thin, inner sep=1.5pt,rounded corners, fit=(c) (c)] {};
\end{tikzpicture}
\end{document}