使用 cellcolor 对表格中的行进行着色

使用 cellcolor 对表格中的行进行着色

我正在尝试使用以下代码为表格的标题行添加颜色:

\documentclass[a4paper, 10pt]{article}

%% Packages %%
\usepackage[top=1in, bottom=1in, left=1in, right=1in]{geometry}
\usepackage{fancyhdr}
\usepackage{tikz,float}
\usetikzlibrary{shapes,arrows}
\usepackage{verbatim}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{tabu}
\usepackage{sidecap}
\usepackage{multirow}
\usepackage[sorting=none]{biblatex}
\usepackage{subcaption}
\usepackage{setspace}
\usepackage{multicol}
\usepackage{scalerel}
\usepackage{enumitem}
\usepackage{empheq}
\usepackage{caption}
\usepackage{float}
\usepackage{kantlipsum}
\usepackage{xcolor,colortbl}
\usepackage{mathtools}
\usepackage{listings}
\usepackage[document]{ragged2e}
\setcounter{MaxMatrixCols}{20}
\definecolor{mygreen}{RGB}{28,172,0} % color values Red, Green, Blue
\definecolor{mylilas}{RGB}{170,55,241}
%% Title Page %%

\addbibresource{refbib.bib}

\begin{document}

\begin{table}[htbp]
\begin{center}
{\tabulinesep=1.2mm    
    \begin{tabu} to 0.9\linewidth { |[2pt] *{3}{X[1 c]|}[2pt] } \tabucline[2pt]{-}
        \multicolumn{3}{ |[2pt]c|[2pt] }{\cellcolor{green!20}Initial Conditions} \\ \tabucline[2pt]{-}
        $[x(0), y(0), z(0)]_{\text{R}}$ & [-29136.9, 0, 0] & m \\ \hline
        $[x(0), y(0), z(0)]_{\text{T}}$ & [0, 0, 0] & m \\ \hline    
        $[V_{x}(0), V_{y}(0), V_{z}(0)]_{\text{R}}$ & [680.7, 0, 680.7] & m/s \\ \hline
        $[V_{x}(0), V_{y}(0), V_{z}(0)]_{\text{Wind}}$ & [0, -10, 0] & m/s \\ \hline
        $[V_{x}(0), V_{y}(0), V_{z}(0)]_{\text{Wind Error}}$ & [0, 0, 0] & m/s \\ \hline
        Launch Angle & 45 & Deg \\ \tabucline[2pt]{-}
    \end{tabu}}
\end{center}
\caption{Initial conditions for test 1}
\end{table}
\end{document}

然而,由于某种原因我仍然得到这个结果:

在此处输入图片描述

它没有显示绿线,有什么原因吗?

谢谢

答案1

我不会将该tabu包用于我的表格。不幸的是,这个意图诱人的包有缺陷(特别是在它所基于的新版 array` 包之后)并且无人维护。

对于您的表格,使用普通环境似乎更合适tabular,不要将单元格的内容锁定在“水平和垂直表格的监狱”规则中,左对齐单元格。为了获得更多的垂直空间,我会使用包cellspace

在此处输入图片描述

\documentclass[a4paper, 10pt]{article}
\usepackage[top=1in, bottom=1in, left=1in, right=1in]{geometry}
\usepackage{mathtools}
\usepackage[skip=1ex]{caption}
\usepackage[table]{xcolor}
\definecolor{mygreen}{RGB}{28,172,0} % color values Red, Green, Blue
\definecolor{mylilas}{RGB}{170,55,241}
\usepackage{cellspace, makecell, tabularx}
\setlength\cellspacetoplimit{5pt}
\setlength\cellspacebottomlimit{5pt}


\begin{document}
    \begin{table}[htbp]
    \centering
\caption{Initial conditions for test 1}
\label{my table}
    \begin{tabular}{ *{2}{>{$}Sl<{$}} l } 
    \Xhline{1pt}
    \rowcolor{mygreen!20}
\multicolumn{3}{Sc}{Initial Conditions}     \\ 
    \Xhline{0.75pt}
{[x(0), y(0), z(0)]_{\mathrm{R}}}   
        & [-29136.9, 0, 0]      & m         \\  
{[x(0), y(0), z(0)]_{\text{T}}}     
        & [0, 0, 0]             & m         \\
{[V_{x}(0), V_{y}(0), V_{z}(0)]_{\text{R}}}
    & [680.7, 0, 680.7]         & m/s       \\ 
{[V_{x}(0), V_{y}(0), V_{z}(0)]_{\text{Wind}}}
    & [0, -10, 0]               & m/s       \\ 
{[V_{x}(0), V_{y}(0), V_{z}(0)]_{\text{Wind Error}}}
    &  [0, 0, 0]                & m/s       \\ 
\text{Launch Angle}             
    & 45                        & Degrees   \\ 
    \Xhline{1pt}
    \end{tabular}
    \end{table}
\end{document}

**附录:**与包的使用相同的表格siunitx。使用它们的名称作为单位可以使它们的用途更加清晰……并希望鼓励您将其用于文档中的所有单位:

\documentclass[a4paper, 10pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage[skip=1ex]{caption}
\usepackage[table]{xcolor}
\definecolor{mygreen}{RGB}{28,172,0} % color values Red, Green, Blue
\definecolor{mylilas}{RGB}{170,55,241}
\usepackage{cellspace, makecell, tabularx}
\setlength\cellspacetoplimit{5pt}
\setlength\cellspacebottomlimit{5pt}
\usepackage{siunitx}

\begin{document}
    \begin{table}[htbp]
    \centering
    \sisetup{per-mode=symbol}
\caption{Initial conditions for test 1}
\label{my table}
    \begin{tabular}{ *{2}{>{$}Cl<{$}} s } 
    \Xhline{1pt}
    \rowcolor{mygreen!20}
\multicolumn{3}{Cc}{Initial Conditions}             \\ 
    \Xhline{0.6pt}
{[x(0), y(0), z(0)]_{\mathrm{R}}}   
        & [-29136.9, 0, 0]      & \metre            \\  
{[x(0), y(0), z(0)]_{\text{T}}}     
        & [0, 0, 0]             & \metre            \\
{[V_{x}(0), V_{y}(0), V_{z}(0)]_{\text{R}}}
    & [680.7, 0, 680.7]         & \metre/\second    \\ 
{[V_{x}(0), V_{y}(0), V_{z}(0)]_{\text{Wind}}}
    & [0, -10, 0]               & \metre\per\second \\ 
{[V_{x}(0), V_{y}(0), V_{z}(0)]_{\text{Wind Error}}}
    &  [0, 0, 0]                & \metre\per\second \\ 
\text{Launch Angle}             
    & 45                        & Degrees   \\ 
    \Xhline{1pt}
    \end{tabular}
    \end{table}
\end{document}

结果与第一个例子相同。

相关内容