更改表格单元格的颜色

更改表格单元格的颜色

我使用 cellcolor 来更改表格中单元格的颜色。但是,出于某种奇怪的原因,它没有完全着色。为什么会发生这种情况?以下是 MWE:

\documentclass[twoside,b5paper,9.5pt,openright]{book}
\usepackage[total={13cm,19.5cm}]{geometry} 
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{tocloft}
\usepackage{fixltx2e}
\usepackage{graphicx}           % include graphs/ figures  in the file
\usepackage{setspace}           % to set the line spacing in the document
\usepackage{ragged2e}
\usepackage{lscape}             % to write pages in landscape environment
\usepackage{array,threeparttable}       % to add footnotes to the tables
\usepackage{booktabs}
\usepackage{caption}    % to create some space between table caption and table, otherwise there was no space
\captionsetup[figure]{font=small,skip=0pt}
\captionsetup[table]{font=small,skip=0pt}
\usepackage[normalem]{ulem}     % to underline the text
\setcounter{secnumdepth}{3}
\usepackage{multirow}
\usepackage[labelfont=bf]{caption}
\let\newfloat\relax
\usepackage{floatrow}
\usepackage{array,threeparttable,siunitx}
\floatsetup[table]{capposition=top}
\floatsetup[figure]{capposition=bottom}
\usepackage{etoolbox}
\usepackage{subfig}
\usepackage[table]{xcolor}
\usepackage{grffile}            % to avoid printing the figure name (or otherwise, give figure names without spaces)
\usepackage{float}              % figures as 6 (a), 6 (b) etc.
\usepackage{tabu}
\usepackage{tikz}
\usepackage{arydshln}
\raggedbottom
%\allsectionsfont{\sf

\begin{document}

\begin{table}[h!]
\scriptsize
  \centering
  \caption{Free  (mg/100g ter) and aride compos (gy maer) in pokuls}
    \begin{tabular}{p{3cm}p{1.75cm}p{1.75cm}p{1.75cm}p{1.75cm}}
    \toprule
    \textbf{\cellcolor{blue!25}Frtion (matter)} & \multicolumn{1}{c}{\cellcolor{blue!25}\textbf{M\textsubscript{0}}} & \multicolumn{1}{c}{\cellcolor{blue!25}\textbf{M\textsubscript{15}}} & \multicolumn{1}{c}{\cellcolor{blue!25}\textbf{M\textsubscript{60}}} & \multicolumn{1}{c}{\cellcolor{blue!25}\textbf{M\textsubscript{120}}}\tabularnewline  
    \midrule
    \textit{Esseo acids} &       &       &       &  \tabularnewline     \addlinespace
    Valine & \multicolumn{1}{c}{75} & \multicolumn{1}{c}{40} & \multicolumn{1}{c}{26} & \multicolumn{1}{c}{10} \tabularnewline      \addlinespace
        \textit{\textbf{Total }} & \multicolumn{1}{c}{\textbf{11.84}} & \multicolumn{1}{c}{\textbf{10.24}} & \multicolumn{1}{c}{\textbf{9.2}} & \multicolumn{1}{c}{\textbf{10.18}} \tabularnewline      
    \bottomrule
    \end{tabular}%
   \end{table}%    

   \end{document}

答案1

\cellcolor应该是单元格中出现的第一项,除非使用\multicolumn。下面我还提供了一个更新的\toprulec和 ,\midrulec它使用 插入彩色分隔符,\specialrule使标题更加统一:

在此处输入图片描述

\documentclass{article}
\usepackage{booktabs,fixltx2e}
\usepackage[table]{xcolor}

\newcommand{\toprulec}{% Coloured \toprule
  \arrayrulecolor{black}\specialrule{\heavyrulewidth}{\aboverulesep}{0pt}
  \arrayrulecolor{blue!25}\specialrule{\belowrulesep}{0pt}{0pt}
  \arrayrulecolor{black}
}
\newcommand{\midrulec}{% Coloured \midrule
  \arrayrulecolor{blue!25}\specialrule{\aboverulesep}{0pt}{0pt}
  \arrayrulecolor{black}\specialrule{\lightrulewidth}{0pt}{\belowrulesep}
}
\begin{document}

\begin{tabular}{p{3cm}p{1.75cm}p{1.75cm}p{1.75cm}p{1.75cm}}
  \toprulec
  \cellcolor{blue!25}\textbf{Frtion (matter)} & 
    \multicolumn{1}{c}{\cellcolor{blue!25}\textbf{M\textsubscript{0}}} & 
    \multicolumn{1}{c}{\cellcolor{blue!25}\textbf{M\textsubscript{15}}} & 
    \multicolumn{1}{c}{\cellcolor{blue!25}\textbf{M\textsubscript{60}}} & 
    \multicolumn{1}{c}{\cellcolor{blue!25}\textbf{M\textsubscript{120}}} \\
  \midrulec
  \textit{Esseo acids} &       &       &       &  \\
  \addlinespace
  Valine & \multicolumn{1}{c}{75} & \multicolumn{1}{c}{40} & \multicolumn{1}{c}{26} & \multicolumn{1}{c}{10} \\
  \addlinespace
  \textit{\textbf{Total }} & 
    \multicolumn{1}{c}{\textbf{11.84}} & 
    \multicolumn{1}{c}{\textbf{10.24}} & 
    \multicolumn{1}{c}{\textbf{9.2}} & 
    \multicolumn{1}{c}{\textbf{10.18}} \\     
  \bottomrule
\end{tabular}

\end{document}

同样,\rowcolor应该是行中出现的第一件事。

相关内容