将表格中的单元格变为黑色

将表格中的单元格变为黑色

在下面的代码中,我尝试为第一列中的每个单元格创建两行。我希望这些行是彩色的。但是,彩色单元格的第一行变成了黑色。我该如何解决这个问题?

\documentclass[preprint,12pt]{elsarticle}
\biboptions{sort&compress}

\usepackage[usenames, dvipsnames]{color}
\usepackage{subcaption}
\captionsetup{compatibility=false}
\usepackage{amssymb}
\usepackage{newtxtext,newtxmath} % better than mathptmx
\usepackage{amsmath}
\newtheorem{thm}{Theorem}
\newproof{pf}{Proof}
\newtheorem{lemma}{Lemma}
\DeclareMathOperator*{\argmin}{arg\,min}
\DeclareMathOperator{\sign}{sign}
\usepackage{longtable}
\usepackage{colortbl}%
\usepackage{booktabs}
\usepackage{array}
\usepackage{arydshln}
\setlength\dashlinedash{0.2pt}
\setlength\dashlinegap{1.5pt}
\setlength\arrayrulewidth{0.3pt}
\usepackage{siunitx}
\definecolor{mygray}{gray}{0.925}

\newcommand{\splitcell}[2]{\begin{tabular}{@{}#1@{}}#2\end{tabular}}

\begin{document}

\begin{table}[htp]
\centering

 \begin{tabular}{
 c
 >{\bfseries}S[table-format=1.4,detect-all]
 >{\bfseries}S[table-format=3.4,detect-all]
 >{\bfseries}S[table-format=3.4,detect-all]
 >{\bfseries}S[table-format=2.4,detect-all]
 }
 \toprule
 \centering
 Initial gains & 
 {$\max(x_1)$} & 
 {$\max(F)$} & 
 {$\max(\lvert\tau_1\rvert)$} & 
 {$\max(\lvert\tau_2\rvert)$} \\
 \midrule

 \splitcell{l}{$L_1=100$\\$L_2=100$} & 0.6221 & 121.2892 & 149.8136 & 
 27.9641 \\

 \rowcolor{mygray}%
 \splitcell{l}{$L_1=100$\\$L_2=100$} & 0.6221 & 120.5161 & 149.4271 & 
 34.7235 \\

 \splitcell{l}{$L_1=100$\\$L_2=100$} & 0.6213 & 112.7963 & 148.2077 & 
  28.1165 \\

 \rowcolor{mygray}%
 \splitcell{l}{$L_1=100$\\$L_2=100$} & 0.6213 & 112.7133 & 148.0053 & 
 34.9203 \\ 

 \splitcell{l}{$L_1=100$\\$L_2=100$} & 0.6207 & 107.3273 & 281.3320 & 
 56.0344 \\

 \rowcolor{mygray}%
 \splitcell{l}{$L_1=100$\\$L_2=100$} & 0.6193 & 92.9236 & 145.1045 & 35.5135 
 \\

  \splitcell{l}{$L_1=100$\\$L_2=100$} & 0.6192 & 91.6388 & 281.3320 & 
  56.3469 \\ 

  \rowcolor{mygray}%
  \splitcell{l}{$L_1=100$\\$L_2=100$} & 0.6187 & 86.6108 & 418.2479 & 
  76.9534 \\

  \splitcell{l}{$L_1=100$\\$L_2=100$} & 0.6180 & 80.0001 & 281.3320 & 
  56.6598 \\

  \rowcolor{mygray}%
  \splitcell{l}{$L_1=100$\\$L_2=100$} & 0.6189 & 89.1374 & 142.7592 & 
  30.0929 \\

  \bottomrule
  \end{tabular}

  \caption{Numerical values}
  \label{t1}

  \end{table}

  \end{document}

  \end{document} 

在此处输入图片描述

答案1

还有一个答案……

  • 使用包装newtxtextnewtxmath埃格尔回答
  • 对于行着色的使用方法\rowcolors{3}{mygray}{white}伯纳德回答
  • 对于多行单元格,可以使用makecell包中的选项makecell,但它们被确定为第一列的选项(用于较短的代码)

编辑:

该包arydshln与许多包不兼容......所以这是导致您出现问题的原因:它不“喜欢”用插入tabular环境或使用makecell宏(基于tabular)构建的多行单元格。

解决方法如下:

  • 在您的序言中做出必要的更改,这对于我的解决方案的工作是必要的(ansmath用替换mathtools,和colorcolortbl替换[...,table]{xcolor},请参阅下面我的 mwe 中的注释),并且不会影响您的实际文档
  • makcell不像我第一次提议的那样使用
  • 而是makcell使用列类型m{...}

更正后的代码为:

\documentclass[preprint,12pt]{elsarticle}
\biboptions{sort&compress}
\usepackage{subcaption}
\captionsetup{compatibility=false}
\usepackage{amssymb}
\usepackage{newtxtext,newtxmath} % better than mathptmx
%\usepackage{amsmath}            % for my answer should be replaced by "mathtools"
\usepackage{mathtools}           % mathtools load "amsmath"
    \DeclarePairedDelimiter\abs{\lvert}{\rvert}
    \DeclareMathOperator*{\argmin}{arg\,min}
    \DeclareMathOperator{\sign}{sign}
\newtheorem{thm}{Theorem}
\newproof{pf}{Proof}
\newtheorem{lemma}{Lemma}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage{array}
\usepackage{siunitx}

%\usepackage[usenames, dvipsnames]{color}% better merge with "colortbl" package into "[table]{xcolor}"
                                         % my solution doesn't wirk with this package
%\usepackage{colortbl}                   % better merge with "color" package into "[table]{xcolor}"
\usepackage[usenames, dvipsnames,table]{xcolor}
\definecolor{mygray}{gray}{0.925}
\usepackage{arydshln}
\setlength\dashlinedash{0.2pt}
\setlength\dashlinegap{1.5pt}
\setlength\arrayrulewidth{0.3pt}

    \begin{document}
        \begin{table}[htb]
    \sisetup{detect-all}
    \rowcolors{3}{mygray}{white}
    \centering
        \begin{tabular}{
                >{$L_1=100$\newline
                  $L_2=100$}  m{11 ex}@{}
            >{\bfseries}S[table-format=1.4]
       *{2}{>{\bfseries}S[table-format=3.4]}
            >{\bfseries}S[table-format=2.4]
                    }
    \toprule
\multicolumn{1}{l}{Initial gains}
    &{$x_1$}
                & {$\max(F)$}
                                & {$\max(\abs{\tau_1})$}
                                                & {$\max(\abs{\tau_2})$}
                                                            \\
    \midrule
    & 0.6221    & 121.2892      & 149.8136      & 27.9641   \\
    & 0.6221    & 120.5161      & 149.4271      & 34.7235   \\
    & 0.6213    & 112.7963      & 148.2077      & 28.1165   \\
    & 0.6213    & 112.7133      & 148.0053      & 34.9203   \\
    & 0.6207    & 107.3273      & 281.3320      & 56.0344   \\
    & 0.6193    &  92.9236      & 145.1045      & 35.5135   \\
    & 0.6192    &  91.6388      & 281.3320      & 56.3469   \\
    & 0.6187    &  86.6108      & 418.2479      & 76.9534   \\
    & 0.6180    &  80.0001      & 281.3320      & 56.6598   \\
    & 0.6189    &  89.1374      & 142.7592      & 30.0929   \\[-\aboverulesep]
    \bottomrule
    \end{tabular}
\caption{Numerical values}
\label{t1}
    \end{table}
\end{document}

在此处输入图片描述

编辑(2): 如果您不喜欢表格中的粗体数字,那么您只需要删除>{\bfseries}S类型定义。

编辑(3): 答案的最终版本。它考虑了您在以下评论中提出的所有其他要求:

\documentclass[preprint,12pt]{elsarticle}
\biboptions{sort&compress}
\usepackage{subcaption}
\captionsetup{compatibility=false}
\usepackage{amssymb}
\usepackage{newtxtext,newtxmath} 
\usepackage{mathtools}           
    \DeclarePairedDelimiter\abs{\lvert}{\rvert}
    \DeclareMathOperator*{\argmin}{arg\,min}
    \DeclareMathOperator{\sign}{sign}
\newtheorem{thm}{Theorem}
\newproof{pf}{Proof}
\newtheorem{lemma}{Lemma}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage{array}
\usepackage{siunitx}

%\usepackage[usenames, dvipsnames]{color}% better merge with "colortbl" package into "[table]{xcolor}"
                                         % my solution doesn't wirk with this package
%\usepackage{colortbl}                   % better merge with "color" package into "[table]{xcolor}"
\usepackage[usenames, dvipsnames,table]{xcolor}
\definecolor{mygray}{gray}{0.925}
\usepackage{arydshln}
\setlength\dashlinedash{0.2pt}
\setlength\dashlinegap{1.5pt}
\setlength\arrayrulewidth{0.3pt}

    \begin{document}
        \begin{table}[htb]
    \sisetup{detect-all}
    \rowcolors{3}{mygray}{white}
    \centering
        \begin{tabular}{
            >{\raggedright\arraybackslash}m{11 ex}
            S[table-format=1.4]
       *{2}{S[table-format=3.4]}
            S[table-format=2.4]
                    }
    \toprule
\multicolumn{1}{l}{Initial gains}
    & {$x_1$}   & {$\max(F)$}   & {$\max(\abs{\tau_1})$}
                                                & {$\max(\abs{\tau_2})$}
                                                            \\
    \midrule
$L_1 = 100$  $L_2=111$
    & 0.6221    & 121.2892      & 149.8136      & 27.9641   \\
$L_1 = 101$  $L_2=112$
    & 0.6221    & 120.5161      & 149.4271      & 34.7235   \\
$L_1 = 102$  $L_2=113$
    & 0.6213    & 112.7963      & 148.2077      & 28.1165   \\
$L_1 = 103$  $L_2=114$
    & 0.6213    & 112.7133      & 148.0053      & 34.9203   \\
$L_1 = 104$  $L_2=115$
    & 0.6207    & 107.3273      & 281.3320      & 56.0344   \\
$L_1 = 105$  $L_2=116$
    & 0.6193    &  92.9236      & 145.1045      & 35.5135   \\
$L_1 = 106$  $L_2=117$
    & 0.6192    &  91.6388      & 281.3320      & 56.3469   \\
$L_1 = 107$  $L_2=118$
    & 0.6187    &  86.6108      & 418.2479      & 76.9534   \\
$L_1 = 108$  $L_2=119$
    & 0.6180    &  80.0001      & 281.3320      & 56.6598   \\
$L_1 = 109$  $L_2=120$
    & 0.6189    &  89.1374      & 142.7592      & 30.0929   \\[-\aboverulesep]
    \bottomrule
    \end{tabular}
\caption{Numerical values}
\label{t1}
    \end{table}
\end{document}

在此处输入图片描述

答案2

\Vectorstack您可以使用中的命令替换第一列中的表格 。我还简化了使用带有选项(加载)定义的命令对stackengine每隔一行进行着色的方式。最后,请注意,长表默认自动居中,并且必须在长表内使用该命令,最好在部分中。\rowcolorsxcolor[table]colortblcaptionfirsthead

\documentclass[preprint,12pt]{elsarticle}

%\usepackage{amssymb}
\usepackage{mathptmx}
\usepackage{amsmath}

\usepackage{longtable}
\usepackage[table, svgnames]{xcolor}%
\newcommand{\myrowcolour}{\rowcolor[gray]{0.925}}
\usepackage{booktabs}
\usepackage{array, siunitx, stackengine}
\setstackEOL{\\}
\usepackage{arydshln}
\setlength\dashlinedash{0.2pt}
\setlength\dashlinegap{1.5pt}
\setlength\arrayrulewidth{0.3pt}
\begin{document}

\bfseries\boldmath
\sisetup{table-number-alignment=center, detect-weight}
\rowcolors{3}{white}{Gainsboro}
 \begin{longtable}{%@{}
  >{$}l<{$}>{\bfseries\boldmath}S[table-format=1.4]*{2}{>{\bfseries\boldmath}S[table-format=3.4]>{\bfseries\boldmath}}S[table-format=2.4]}
 \caption{Numerical values}
 \label{t1}
\endfirsthead
\toprule
\textbf{Initial gains}
 &{\textbf{max ($ x_1 $)}}
 &{\textbf{max($F$)}}
 &{\textbf{max($\vert\tau_1\vert$)}}
 &{\textbf{max($\vert\tau_2\vert$)}}
 \\ \midrule
\Vectorstack[l]{L_1=100\\ L_2=100} & 0.6221
 & 121.2892 & 149.8136 & 27.9641 \\
 \Vectorstack{L_1=100\\ L_2=100} & 0.6221
 & 120.5161 & 149.4271 & 34.7235 \\
\addlinespace
 \Vectorstack{L_1=100 \\ L_2=100} & 0.6213
 & 112.7963 & 148.2077 & 28.1165 \\
 \Vectorstack{L_1=100\\L_2=100}
 & 0.6213 & 112.7133 & 148.0053 & 34.9203 \\
 \Vectorstack{L_1=100\\ L_2=100}
  & 0.6207 & 107.3273 & 281.3320 & 56.0344 \\
 \Vectorstack{L_1=100\\ L_2=100}
  & 0.6193 & 92.9236 & 145.1045 & 35.5135 \\
 \Vectorstack{L_1=100\\ L_2=100}
  & 0.6192 & 91.6388 & 281.3320 & 56.3469 \\
 \Vectorstack{L_1=100\\ L_2=100}
  & 0.6187 & 86.6108 & 418.2479 & 76.9534 \\
 \Vectorstack{L_1=100\\ L_2=100}
  & 0.6180 & 80.0001 & 281.3320 & 56.6598 \\
 \Vectorstack{L_1=100\\ L_2=100}
 & 0.6189 & 89.1374 & 142.7592 & 30.0929\\[-\aboverulesep]
 \bottomrule
%
% \caption{Numerical values}
% \label{t1}
 \end{longtable}

 \end{document} 

在此处输入图片描述

答案3

我根本不会使用粗体,但您可以按照以下方法操作;我将其改为longtabletabular因为当前的表格很短,但代码对于 的作用相同longtable

对于标题来说,这是一个艰难的选择,但我会完全删除粗体:“max”是一个数学运算符,它的粗体可能会让读者感到困惑。

\documentclass[preprint,12pt]{elsarticle}

\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{newtxtext,newtxmath} % better than mathptmx

\usepackage{longtable}
\usepackage{colortbl}
\usepackage{booktabs}
\usepackage{array}
\usepackage{siunitx}

\definecolor{mygray}{gray}{0.925}

\newcommand{\splitcell}[2]{\begin{tabular}{@{}#1@{}}#2\end{tabular}}

\begin{document}

\begin{table}[htp]
\centering

\begin{tabular}{
 c
 >{\bfseries}S[table-format=1.4,detect-all]
 >{\bfseries}S[table-format=3.4,detect-all]
 >{\bfseries}S[table-format=3.4,detect-all]
 >{\bfseries}S[table-format=2.4,detect-all]
}
\toprule
\centering
Initial gains & 
 {$\max(x_1)$} & 
 {$\max(F)$} & 
 {$\max(\lvert\tau_1\rvert)$} & 
 {$\max(\lvert\tau_2\rvert)$} \\
\midrule

\splitcell{l}{$L_1=100$\\$L_2=100$} & 0.6221 & 121.2892 & 149.8136 & 27.9641 \\

\rowcolor{mygray}%
\splitcell{l}{$L_1=100$\\$L_2=100$} & 0.6221 & 120.5161 & 149.4271 & 34.7235 \\

\splitcell{l}{$L_1=100$\\$L_2=100$} & 0.6213 & 112.7963 & 148.2077 & 28.1165 \\

\rowcolor{mygray}%
\splitcell{l}{$L_1=100$\\$L_2=100$} & 0.6213 & 112.7133 & 148.0053 & 34.9203 \\ 

\splitcell{l}{$L_1=100$\\$L_2=100$} & 0.6207 & 107.3273 & 281.3320 & 56.0344 \\

\rowcolor{mygray}%
\splitcell{l}{$L_1=100$\\$L_2=100$} & 0.6193 & 92.9236 & 145.1045 & 35.5135 \\

\splitcell{l}{$L_1=100$\\$L_2=100$} & 0.6192 & 91.6388 & 281.3320 & 56.3469 \\ 

\rowcolor{mygray}%
\splitcell{l}{$L_1=100$\\$L_2=100$} & 0.6187 & 86.6108 & 418.2479 & 76.9534 \\

\splitcell{l}{$L_1=100$\\$L_2=100$} & 0.6180 & 80.0001 & 281.3320 & 56.6598 \\

\rowcolor{mygray}%
\splitcell{l}{$L_1=100$\\$L_2=100$} & 0.6189 & 89.1374 & 142.7592 & 30.0929 \\

\bottomrule
\end{tabular}

\caption{Numerical values}
\label{t1}

\end{table}

\end{document}

在此处输入图片描述

相关内容