3

In the following code, I tried to create two rows for each cell in the first column. I want the rows be colored. However, the first rows of the colored cell get black. How can I solve this problem?

\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} 

enter image description here

3

one more answer ...

  • used packages newtxtext and newtxmath as in egreg answer
  • for row coloring used \rowcolors{3}{mygray}{white} similarly as in Bernard answer
  • for multi line cells is used makecell from the package makecell, but they are determined as option of the first column (for shorter code)

edit:

the package arydshln is not compatible with many packages ... so it is cause of your problem: it doesn't "like" multi line cells constructed with inserting tabular environments or use of the makecell macro (which is based tabular).

as work around can be:

  • make necessary changes in your preamble which is needed for work of my solution (replace ansmath with mathtools, and color and colortbl with [...,table]{xcolor}, see comments in my mwe below) and will not influence on your real document
  • not use makcell as i did in my first propose
  • instead makcell use column type m{...}

corrected code is:

\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}

enter image description here

edit (2): in case, that you not like boldface numbers in table, than you only need to remove >{\bfseries} in S column type definitions.

edit (3): the final version of the answer. it consider all your additional requests from your comments below answer:

\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}

enter image description here

  • As I commented on egreg's response, I have some other commands and packages in preamble. Your code works fine alone but when I put it into the main file, I encounter a problem in which the output differs from when I compile it alone. I edited the code in the question in which you can see all of my packages and commands in preamble. – AbbasKaramali Apr 12 '18 at 10:56
  • problem is package arydshln. do you really need them? – Zarko Apr 12 '18 at 11:14
  • I had two more tables and for having beautiful tables I wanted to add dash lines in them. If there is no solution, I can remove it. – AbbasKaramali Apr 12 '18 at 11:17
  • @AbbasKaramali, i edit my answer and resolve problem with arydshln. see (test) if my solution works in your document. observe suggested changes in your preamble! – Zarko Apr 12 '18 at 12:12
  • Thank you for your nice answer. It works fine. My last question is that if I want to change the bold font to normal font in the numbers of the table, what changes I should do in your code? – AbbasKaramali Apr 12 '18 at 12:37
4

You can replace the tabulars in the first column with the \Vectorstack command from stackengine. I also simplified the way to colour every other row using the \rowcolors command defined by xcolor with option [table] (which loads colortbl). Last, note a long table is automatically centred by default, and the caption command must be used from within the long table, preferably in the firsthead part.

\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} 

enter image description here

  • Similar to but better than my effort! – Paul Stanley Apr 11 '18 at 16:55
  • Great minds think together! Why did you remove your solution? I find it quite interesting. – Bernard Apr 11 '18 at 16:58
  • Because I thought it was sufficiently close to yours to be merely confusing to the OP. And yours correctly uses siunitx, which I had abandoned because I forgot about boldmath! The main thing is to get rid of the nested tables and the awful repetition of \textbf! – Paul Stanley Apr 11 '18 at 17:01
  • @ Bernard: Thank you for your nice answer. However, when I run your code on my system, the output is a little different. Unfortunately, I don't know how to post its image to show you my output. – AbbasKaramali Apr 12 '18 at 6:42
  • @AbbasKaramali: In the edit window, the sixth icon from the left (looking like a mountain) is for inserting an image. – Bernard Apr 12 '18 at 7:41
2

I wouldn't use boldface at all, but here's how you can do it; I changed longtable into tabular, as the present table is short, but the code would work the same for longtable.

For the headers, it's a tough choice, but I'd remove boldface completely: “max” is a math operator and it being bold might confuse the reader.

\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}

enter image description here

  • I compiled your code in a new file and received the same output as you, however, when I use this code in another file which its preamble includes some other packages and commands, the same problem appear again. I think your preamble commands is inconsistent with one or more commands in mine. I edited the code and attached its output image. Would you tell me which command or package is inconsistent with your code? – AbbasKaramali Apr 12 '18 at 10:32
  • @AbbasKaramali It's arydshln. – egreg Apr 12 '18 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.