
我是 LaTex 的新手,我對這個表有一點問題......我必須做很多這種類型的表,我想找到一種方法來自動解決這個問題。有人能幫助我嗎?下面你可以找到我的程式碼和表格。
多謝!
\documentclass{article}
\usepackage{multirow}
\usepackage{xcolor}
\usepackage{colortbl}
\usepackage{rotating}
\begin{document}
\begin{table}[!ht]
\begin{center}
\caption{Matrice de confusion pour les classes texturales concernant notre campagne d’échantillonnage.}
\begin{tabular}{|
>{\columncolor[HTML]{C0C0C0}}c |
>{\columncolor[HTML]{EFEFEF}}c |c|c|c|c|}
\hline
\multicolumn{1}{|l|}{\cellcolor[HTML]{C0C0C0}} & \multicolumn{5}{c|}{\cellcolor[HTML]{C0C0C0}\textbf{\begin{tabular}[c]{@{}c@{}}Texture\\ observée sur le terrain\end{tabular}}} \\ \hline
\cellcolor[HTML]{C0C0C0} & & \cellcolor[HTML]{EFEFEF}A & \cellcolor[HTML]{EFEFEF}L & \cellcolor[HTML]{EFEFEF}Z & \cellcolor[HTML]{EFEFEF}\textbf{Total} \\ \cline{2-6}
\cellcolor[HTML]{C0C0C0} & A & 3 & 2 & 0 & 5 \\ \cline{2-6}
\cellcolor[HTML]{C0C0C0} & L & 4 & 7 & 0 & 11 \\ \cline{2-6}
\cellcolor[HTML]{C0C0C0} & Z & 0 & 1 & 3 & 4 \\ \cline{2-6}
\multirow{-5}{*}{\cellcolor[HTML]{C0C0C0}\textbf{\rotatebox{90}{Référence CNSW}}} & \textbf{Total} & 7 & 10 & 3 & \textbf{20} \\ \hline
\end{tabular}
\end{center}
\end{table}
\end{document}
答案1
我總是盡量避免表格的過度著色。保持乾淨整潔,最後更具可讀性!
也就是說,這是一個可能的解決方案。但請注意,您必須手動微調\parbox
位置。
\documentclass{article}
\usepackage{multirow}
\usepackage{makecell}
\usepackage{booktabs}
\usepackage{rotating}
\renewcommand\theadalign{bc}
\renewcommand\theadfont{\bfseries}
\begin{document}
\begin{table}[!ht]
\caption{Matrice de confusion pour les classes texturales concernant notre campagne d’échantillonnage.}
\begin{center}
\begin{tabular}{*{6}{c}}
\toprule
& \multicolumn{5}{c}{\thead{Texture\\ observée sur le terrain}}\\
\midrule
\multirow{5}{*}{\rotatebox{90}{\parbox[c]{2.2cm}{\centering\textbf{Référence CNSW}}}}
&
& A
& L
& Z
& \textbf{Total}\\
& A
& 3
& 2
& 0
& 5\\
& L
& 4
& 7
& 0
& 11\\
& Z
& 0
& 1
& 3
& 4\\\addlinespace
& \textbf{Total}
& 7
& 10
& 3
& \textbf{20} \\
\bottomrule
\end{tabular}
\end{center}
\end{table}
\end{document}
答案2
使用nicematrix
,您將在所有縮放等級的所有 PDF 檢視器中獲得完美的輸出(灰色面板中不會出現細白線,並且規則在某些縮放等級似乎不會消失)。
\documentclass{article}
\usepackage[svgnames]{xcolor}
\usepackage{caption}
\usepackage{nicematrix}
\NiceMatrixOptions{cell-space-top-limit=2pt}
\begin{document}
\begin{table}[!ht]
\centering
\captionsetup{width=9cm, format=hang}
\setlength{\extrarowheight}{2pt}
\caption{Matrice de confusion pour les classes texturales concernant notre campagne d’échantillonnage.}
\begin{NiceTabular}{cccccc}[hvlines,corners=NW]
\CodeBefore
\rectanglecolor{LightGrey!50}{2-2}{2-6}
\Body
& \Block[fill=LightGrey]{1-5}<\bfseries>{Texture\\ observée sur le terrain} \\
\Block[fill=LightGrey]{5-1}<\bfseries\rotate>{Référence\\CNSW} &
\Block[fill=LightGrey!50]{5-1}{}
& A & L & Z & \textbf{Total} \\
& A & 3 & 2 & 0 & 5 \\
& L & 4 & 7 & 0 & 11 \\
& Z & 0 & 1 & 3 & 4 \\
& \textbf{Total} & 7 & 10 & 3 & \textbf{20} \\
\end{NiceTabular}
\end{table}
\end{document}
nicematrix
因為使用 PGF/Tikz 節點,所以需要多次編譯。
答案3
這是一個解決方案,具有各種改進和程式碼簡化。對於您的特定問題,主要工具是包makecell
,它允許在標準單元格中換行,但它不能很好地處理表格中的顏色,因為場景後面有表格。\Centerstack
from 的指令不stackengine
存在這個問題。另外,您不必加載xcolor
and colortbl
:table
xcolor 選項可以為您完成此操作,並且它為表格添加了更多顏色命令。最後,\cline
在彩色表格中留下了一些細白線,所以我將它們替換為\hhline
s ,使部分線條具有背景顏色,從而使它們不可見。
\documentclass{article}
\usepackage{multirow}
\usepackage[table, svgnames]{xcolor}
\colorlet{grey2}{LightGrey!25}
\usepackage{rotating}
\usepackage{caption, makecell, hhline}
\newcommand{\myhhline}{\hhline{|>{\arrayrulecolor{LightGrey}}- > {\arrayrulecolor{black}}|>{\arrayrulecolor{grey2}}->{\arrayrulecolor{black}}|----|}}
\usepackage[usestackEOL]{stackengine}
\begin{document}
\begin{table}[!ht]
\centering
\captionsetup{width=9cm, format=hang}
\setlength{\extrarowheight}{2pt}
\caption{Matrice de confusion pour les classes texturales concernant notre campagne d’échantillonnage.}
\begin{tabular}{|
>{\columncolor{LightGrey}}c |
>{\columncolor{grey2}}c|c|c|c|c|}
\hline
\rowcolor{LightGrey} & \multicolumn{5}{c|}{\bfseries\Centerstack{Texture\\ observée sur le terrain}} \\ \hline
\rowcolor{grey2}\cellcolor{LightGrey} & & A & L & Z & \textbf{Total} \\[-0.2pt]
\myhhline
& A & 3 & 2 & 0 & 5 \\[-0.1pt]
\myhhline%
& L & 4 & 7 & 0 & 11 \\[-0.1pt]
\myhhline
& Z & 0 & 1 & 3 & 4 \\[-0.1pt]
\myhhline
\multirow{-5}{*}{\textbf{\rotatebox{90}{\Centerstack{Référence\\ CNSW}}}} & \textbf{Total} & 7 & 10 & 3 & \textbf{20} \\ \hline
\end{tabular}
\end{table}
\end{document}