如何使表格内容居中并且为特定的框着色?

如何使表格内容居中并且为特定的框着色?

我想要有如下表格: 在此处输入图片描述

我曾尝试使用格式表:

在此处输入图片描述

我的完整 MWE:

\documentclass[12pt,oneside]{book} 

\usepackage{ragged2e}
\usepackage{makecell, multirow, tabularx}
\usepackage{amsmath}
\usepackage{siunitx}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usetikzlibrary{arrows.meta,
                backgrounds,
                calc, chains,
                decorations.pathreplacing,
                fit,
                matrix,
                positioning,
                }

\usepackage{tabularx,seqsplit, caption} %for table spacing to second row
    \usepackage{booktabs, ragged2e}       % Use booktabs rules and get rid of vertical rules, ragged2e to ragged text
    \usepackage{makecell,siunitx} %for table spacing to second row
    \usepackage{threeparttable} %to add footnote below table
    \renewcommand{\TPTtagStyle}{\itshape} % optional
    \usepackage{lipsum} % for filler text
    \usepackage{makecell} % for bold in table using \small
    \renewcommand\theadfont{\small\bfseries} % for bold in table using \small
    \usepackage{tabulary,siunitx} 
    \renewcommand\theadgape{}
    \usepackage{graphicx}

    \usepackage{tikz}
    \usetikzlibrary{decorations.pathreplacing,calc, positioning,matrix,fit,calc, arrows.meta,
                    backgrounds,
                    chains}
    \usepackage{pgfplots}
    \pgfplotsset{compat=1.15}              

    \begin{document}



\setlength\extrarowheight{1pt}          
\renewcommand{\arraystretch}{1.2}
\newcolumntype{Z}{>{\raggedright\arraybackslash}X}
\newcolumntype{V}{>{\centering\arraybackslash}p{1cm}}  % Centred fix width column
\begin{table}[h!]
\small
\centering
\begin{threeparttable}
    \begin{tabularx}{0.7\textwidth}{@{}ZVZV@{}}  % Limit the width of the tabular
    \toprule
     & 
     & \multicolumn{2}{c}{\textbf{Subset}}   \\
     \cmidrule{3-4} 
    &  & 1 & 2   \\
     \midrule
    \multirow{3}{*}{\textbf{Sample\tnote{1}}}  & FC  & 9.800 & \\ 
    % check echekc
    \cmidrule{2-4} 
     & KL & 12.200 &  \\
     \cmidrule{2-4} 
     & SD &  & 24.500 \\
\cmidrule{1-4}
     \multicolumn{2}{l}{\textbf{Test Statistic}} & 0.573 & 0.0\tnote{2}   \\
\cmidrule{1-4}
     \multicolumn{2}{l}{\textbf{Sig. (2-sided test)}} & 0.449 & 0.0   \\
\cmidrule{1-4}
     \multicolumn{2}{l}{\textbf{Adjusted Sig. (2-sided test)}} & 0.449 & 0.0  \\
     \bottomrule
    \end{tabularx}
    \footnotesize 
\begin{tablenotes}
\item[1] Each cell shows the sample average rank of Classification Accuracy (\%)
\item[2] Unable to compute because the subset contains only one sample.
\end{tablenotes}
\end{threeparttable}
  \caption{Homogeneous subsets based on classification accuracy (\%)}
     \label{tab:Homogeneous Subsets based on Classification Accuracy}
 \end{table}




\end{document}

答案1

解决方案是使用siunitx(我将最后两列设为S列,以便在小数点上对齐),hhline代替 booktabs 规则,xcolor并使用选项[table]和简单的\fbox外部规则。

     \documentclass[12pt,oneside]{book}
\usepackage[showframe]{geometry}
    \usepackage{amsmath}
    \usepackage{ragged2e}
    \usepackage{makecell, multirow, tabularx}
    \renewcommand\theadfont{\small\bfseries} % for bold in table using \small
    \renewcommand\theadgape{}
    \usepackage[svgnames, table]{xcolor}
    \usepackage{hhline}
    \usepackage{seqsplit, caption} %for table spacing to second row
    \usepackage{booktabs, ragged2e} % Use booktabs rules and get rid of vertical rules, ragged2e to ragged text
    \usepackage{siunitx} %for table spacing to second row
    \usepackage{threeparttable} %to add footnote below table
    \usepackage{tabulary}
    \usepackage{graphicx}

    \begin{document}

    \begin{table}[htb!]
    \setlength{\fboxsep}{0pt}
    \setlength{\fboxrule}{0.8pt}
    \setlength\extrarowheight{1pt}
    \renewcommand{\arraystretch}{1.2}
    \newcolumntype{Z}{>{\raggedright\arraybackslash}X}
    \newcolumntype{V}{>{\centering\arraybackslash}p{1cm}} % Centred fix width column
    \sisetup{table-format=2.3, table-number-alignment=right, detect-family, table-column-width=12mm, table-align-text-post=false}
    \sffamily\bfseries\small \centering
    \begin{threeparttable}\setlength{\arrayrulewidth}{0.8pt}
    \begin{tabularx}{0.7\textwidth}{!{\vrule width 0.8pt}>{\columncolor{Gainsboro}}l >{\columncolor{Gainsboro}}Z >{\columncolor{LightCyan}}S >{\columncolor{Coral!50!DeepPink}}S!{\vrule width 0.8pt}} % Limit the width of the tabular
      \hhline{----}
     \rowcolor{DodgerBlue!45} &
         & \multicolumn{2}{c!{\vrule width 0.8pt}}{Subset} \\[-0.1pt]
    \hhline{>{\arrayrulecolor{DodgerBlue!45}}-->{\arrayrulecolor{black}}--}\noalign{\vskip-0.1pt}%
    \rowcolor{DodgerBlue!45} & & {\cellcolor{Gainsboro}1} & {\cellcolor{Gainsboro}2} \\
        \hhline{----}
    & 2 & 9.800 & \cellcolor{AliceBlue} \\[-0.1pt]
    \hhline{|>{\arrayrulecolor{Gainsboro}}-->{\arrayrulecolor{black}}--}\noalign{\vskip-0.1pt}%
         & 1 & 12.200 & \cellcolor{white}\\[-0.1pt]
    \hhline{|>{\arrayrulecolor{Gainsboro}}-->{\arrayrulecolor{black}}--}\noalign{\vskip-0.1pt}%
     \multirow{-3}{*}{Sample\tnote{\mdseries 1}} & 3 & \cellcolor{AliceBlue} & 24.500 \\
        \hhline{----}
         \multicolumn{2}{!{\vrule width 0.8pt}l}{\cellcolor{Gainsboro}Test Statistic} & 0.573 & 0.0\tnote{\mdseries 2} \\
        \hhline{----}
         \multicolumn{2}{!{\vrule width 0.8pt}l}{\cellcolor{Gainsboro}Sig. (2-sided test)} & 0.449 & 0.0 \\
        \hhline{----}
         \multicolumn{2}{!{\vrule width 0.8pt}l}{\cellcolor{Gainsboro}Adjusted Sig. (2-sided test)} & 0.449 & 0.0 \\%
         \hhline{----}
        \end{tabularx}
 \begin{tablenotes}[flushleft]
 \footnotesize\mdseries
 \item[1] Each cell shows the sample average rank of Classification Accuracy (\%)
 \item[2] Unable to compute because the subset contains only one sample.
 \end{tablenotes}
    \end{threeparttable}
    \end{table}

    \end{document} 

在此处输入图片描述

答案2

使用\cellcolor常规 tabular 而不是 tabularx,清理了序言和沃纳\rulefiller命令:

在此处输入图片描述

\documentclass[12pt,oneside]{book} 


\usepackage{booktabs} 
\usepackage{threeparttable}
\renewcommand{\TPTtagStyle}{\itshape} 

\usepackage[table,svgnames]{xcolor}

%%%%% See https://tex.stackexchange.com/a/32366/134144 %%%%%
\newcommand*{\rulefiller}[1]{%
  \arrayrulecolor{#1}% change to cell colour
  \specialrule{\heavyrulewidth}{0pt}{-\heavyrulewidth}% "invisible" rule
  \arrayrulecolor{black}% revert to regular line colour
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\begin{table}[h!]
\setlength{\aboverulesep}{0pt}
\setlength{\belowrulesep}{0pt}
\setlength{\extrarowheight}{.75ex}
\small
\centering
\begin{threeparttable}
    \begin{tabular}{llcc}
    \toprule
    \cellcolor{LightSlateBlue} &  \cellcolor{LightSlateBlue} & \multicolumn{2}{c}{\cellcolor{LightSlateBlue}\textbf{Subset}}   \\
     \rulefiller{LightSlateBlue}\cmidrule{3-4} 
    \cellcolor{LightSlateBlue}& \cellcolor{LightSlateBlue} & \cellcolor{lightgray}1 & \cellcolor{lightgray}2   \\
     \midrule
      \cellcolor{lightgray}  & \cellcolor{lightgray}2 & \cellcolor{Cyan} 9.800 & \cellcolor{Lavender} \\ 
\rulefiller{lightgray}\cmidrule{2-4} 
     \cellcolor{lightgray}\textbf{Sample\tnote{1}} & \cellcolor{lightgray}1 & \cellcolor{Cyan}12.200 &  \cellcolor{white}\\
     \rulefiller{lightgray}\cmidrule{2-4} 
    \cellcolor{lightgray} & \cellcolor{lightgray}3 & \cellcolor{Lavender}  & \cellcolor{Crimson}24.500 \\
\cmidrule{1-4}
     \multicolumn{2}{l}{\cellcolor{lightgray}\textbf{Test Statistic}} & \cellcolor{Cyan}0.573 & \cellcolor{Crimson}0.0\tnote{2}   \\
\cmidrule{1-4}
     \multicolumn{2}{l}{\cellcolor{lightgray}\textbf{Sig. (2-sided test)}} & \cellcolor{Cyan}0.449 & \cellcolor{Crimson}0.0   \\
\cmidrule{1-4}
     \multicolumn{2}{l}{\cellcolor{lightgray}\textbf{Adjusted Sig. (2-sided test)}} & \cellcolor{Cyan}0.449 & \cellcolor{Crimson}0.0  \\
     \bottomrule
    \end{tabular}
    \footnotesize 
\begin{tablenotes}
\item[1] Each cell shows the sample average rank of Classification Accuracy (\%)
\item[2] Unable to compute because the subset contains only one sample.
\end{tablenotes}
\end{threeparttable}
\end{table}




\end{document}

答案3

在此处输入图片描述

\documentclass[12pt,oneside]{book}

\usepackage{ragged2e}
\usepackage{booktabs, makecell, multirow, tabularx, threeparttable}
\renewcommand\theadfont{\small\bfseries} % for bold in table using \small
\renewcommand\theadgape{}
\newcolumntype{Z}{>{\raggedright\arraybackslash}X}
\newcolumntype{V}{>{\centering\arraybackslash}p{1cm}}  % Centred fix width column
%\renewcommand{\TPTtagStyle}{\itshape} % optional

\usepackage{amsmath}
\usepackage{siunitx}
\usepackage[table]{xcolor}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usetikzlibrary{arrows.meta,
                backgrounds,
                calc, chains,
                decorations.pathreplacing,
                fit,
                matrix,
                positioning,
                }
\usepackage{graphicx}
\usepackage{caption}

\usepackage{lipsum}

\begin{document}
    \begin{table}[ht]
\setlength\extrarowheight{1pt}
\renewcommand{\arraystretch}{1.2}
\small
\centering
\begin{threeparttable}
    \begin{tabularx}{0.7\textwidth}{|
            >{\columncolor{gray!30}\bfseries}Z
            >{\columncolor{gray!30}}l
            >{\columncolor{gray!30}}Z
            >{\columncolor{cyan!30}}Z
                                     |} 
    \Xhline{0.8pt}
    \rowcolor{teal!30}
    &   & \multicolumn{2}{c|}{\textbf{Subset}} \\
    \Xcline{3-4}{0.4pt}
    \rowcolor{teal!30}
    &   & \cellcolor{gray!30}{\textbf{1}}         
                                & \cellcolor{gray!30}{\textbf{2}}   \\
    \Xhline{0.5pt}
    & 2 & 9.800                 & \cellcolor{white}{}               \\
    \Xcline{2-4}{0.6pt}
    & 1 & 12.200                & \cellcolor{white}{}               \\
    \Xcline{2-4}{0.6pt}
\multirow{-3}{*}{Sample\tnote{1}}
    & 3 & \cellcolor{white}{}   & 24.500                            \\
    \Xhline{0.5pt}
\multicolumn{2}{|>{\bfseries}l}{\cellcolor{gray!30}{Test Statistic}} 
        & 0.573                 & 0.0\tnote{2}                      \\
    \Xhline{0.5pt}
\multicolumn{2}{|>{\bfseries}l}{\cellcolor{gray!30}{(2-sided test)}} & 0.449 & 0.0   \\
    \Xhline{0.5pt}
\multicolumn{2}{|>{\bfseries}l}{\cellcolor{gray!30}{Adjusted Sig. (2-sided test)}} 
    & 0.449         & 0.0       \\
    \Xhline{0.8pt}
    \end{tabularx}
\begin{tablenotes}[flushleft]\footnotesize
\item[1] Each cell shows the sample average rank of Classification Accuracy (\%)
\item[2] Unable to compute because the subset contains only one sample.
\end{tablenotes}
\end{threeparttable}
    \end{table}
\end{document}

不过,我宁愿按如下方式设计您的表格:

在此处输入图片描述

\documentclass[12pt,oneside]{book}

\usepackage{ragged2e}
\usepackage{booktabs, multirow, tabularx, threeparttable}

\usepackage{siunitx}

\begin{document}
    \begin{table}[ht]
\small
\centering
\begin{threeparttable}
    \begin{tabularx}{0.6\textwidth}{ Xl *{2}{S[table-format=2.3,
                                               table-align-text-post=false]} } 
    \toprule
    &   & \multicolumn{2}{c}{Subset}   \\
    \cmidrule{3-4}  
    &   & {1}       & {2}               \\
    \midrule
\multirow{3}{=}{Sample\tnote{1}}
    & 2 & 9.800     &                   \\
    & 1 & 12.200    &                   \\
    & 3 &           & 24.500            \\
    \midrule
\multicolumn{2}{l}{Test Statistic}
        & 0.573     & 0.0\tnote{2}      \\
\multicolumn{2}{l}{Sig. (2-sided test)}
        & 0.449     & 0.0               \\
\multicolumn{2}{l}{Adjusted Sig. (2-sided test)} 
        & 0.449     & 0.0                \\
    \bottomrule
    \end{tabularx}
\begin{tablenotes}[flushleft]\footnotesize
\item[1] Each cell shows the sample average rank of Classification Accuracy (\%)
\item[2] Unable to compute because the subset contains only one sample.
\end{tablenotes}
\end{threeparttable}
    \end{table}
\end{document}

注意:我建议您清理文档前言。有些包被加载了两次或更多次...

答案4

这也许能给你指明正确的方向

\usepackage[table]{xcolor}%---to be inserted in the preamble
%table with colored row
        \begin{table}[h]
     \centering{
     \fontfamily{ptm}\selectfont{
     \rowcolors{1}{gray!30}{}
     \begin{tabular}{ll}
     $x_{n+1}$ & $|x_{n+1}-x_n|$\\ \hline
     1.17650196994274  &  0.000287349\\
     1.17650193990183  &  3.004$\times10^{-8}$\\
     1.17650193990183  &  4.440$\times10^{-16}$\\ \hline
     \end{tabular}  }}%font
     % \caption{Iteraci\’on de Newton para \mt{x^2-\cos(x)-1=0}  con  \mt{x_0=1.5.}}
     \end{table}
    %second table with one colored cell

    \begin{table}[h]
    \centering{
    \fontfamily{ptm}
    \selectfont{
    \begin{tabular}{ll}
    $x_{n+1}$ & $|x_{n+1}-x_n|$\\ \hline
    \cellcolor[gray]{0.80} 1.17  &  3.$\times10^{-8}$\\
    1.17  &  4.$\times10^{-16}$\\ 
    \hline
    \end{tabular}}}%font
    \end{table}

```````````````````````````````````````````

相关内容