我想知道如何构建像此代码中的表一样的表,但是我觉得应该有更好的方法来做到这一点,因为:
1.- 我想删除左上角的单元格。2.- 由于左列应为深色背景,因此必须有一种方法可以一次性完成所有操作,而不是逐个单元格地完成(就像在列的情况下使用行 \rowcolor{darkgray})
提前谢谢!欢迎任何帮助
\begin{table}[H]
\centering
\begin{tabular}{|c|c|c|}
\hline
\rowcolor{darkgray}
\cellcolor{white} & \color{white}{\textbf{A}} & \color{white}{\textbf{B}}\\
\hline
\cellcolor{darkgray}\color{white}{\textbf{C}} & 1 & 2 \\
\hline
\cellcolor{darkgray}\color{white}{\textbf{D}} & 3 & 4 \\
\hline
\end{tabular}
\caption{Some text}
\end{table}
答案1
由于颜色按行/列使用,因此我省略了垂直/水平线
\documentclass{article}
\usepackage{nicematrix}
\begin{table}
\setlength{\belowcaptionskip}{1ex}
\centering
\caption{\texttt{Table}}
\label{label1}
\begin{NiceTabular}{lcc}[code-before = \rowcolor{gray}{1} \columncolor{gray}
{1}\cellcolor{white}{1-1}]
& \color{white}{\textbf{A}} &
\color{white}\textbf{B}\\
\color{white}{\textbf{A}} & 1 & 2\\
\color{white}{\textbf{B}} & 3 & 4\\
\end{NiceTabular}
\end{table}
\end{document}
答案2
附带标准tabular
及xcolor
包装:
\documentclass{article}
\usepackage[table]{xcolor}
\begin{document}
\begin{table}[ht]
\centering
\begin{tabular}{|>{\columncolor{darkgray}}c|c|c|}
\cline{2-3}
\rowcolor{darkgray}
\multicolumn{1}{c}{\cellcolor{white}}
& \color{white}{\textbf{A}}
& \color{white}{\textbf{B}}\\
\hline
\color{white}{\textbf{C}} & 1 & 2 \\
\hline
\color{white}{\textbf{D}} & 3 & 4 \\
\hline
\end{tabular}
\caption{Some text}
\end{table}
\end{document}
答案3
标准表格的稍微简化的代码:
\documentclass[11pt]{article}%
\usepackage[table, svgnames]{xcolor}
\usepackage{array}
\makeatletter
\g@addto@macro{\endtabular}{\rowfont{}}% Clear row font
\makeatother
\newcommand{\rowfonttype}{}% Current row font
\newcommand{\rowfont}[1]{% Set current row font
\gdef\rowfonttype{#1}#1\ignorespaces%
}
\makeatother
\begin{document}
\setlength{\extrarowheight}{2pt}
\begin{table}
\centering
\begin{tabular}{| >{\columncolor{darkgray}\bfseries\color{white}}c|c|c|}
\hline
\rowcolor{darkgray}
\cellcolor{white} & \color{white}\bfseries A & \color{white}\bfseries B \\
\hline
C & 1 & 2 \\
\hline
D & 3 & 4 \\
\hline
\end{tabular}
\caption{Some text}
\end{table}
\end{document}