24

I have a table with color in the rows. When the first or last column is colored as shown below, there is an ugly white space before or after the cell. I want to remove this white space (in this picture, it only appears in the first row as it is the colored one. But the same thing applies when the last row is colored. I do not want this space in both the first and last rows).

enter image description here

Here is the script:

%README: this copy with comprehensive attack and defences description.

\documentclass[compsoc, conference, letterpaper, 10pt, times]{IEEEtran}
\ifCLASSOPTIONcompsoc
  % IEEE Computer Society needs nocompress option
  % requires cite.sty v4.0 or later (November 2003)
  \usepackage[nocompress]{cite}
\else
  % normal IEEE

  \usepackage{cite}
\fi
\ifCLASSINFOpdf
\else
\fi

\usepackage{colortbl}       
\usepackage{array}
\usepackage{booktabs}
\usepackage{multirow}
\newcommand{\head}[1]{\textnormal{\textbf{#1}}}
\newcommand{\normal}[1]{\multicolumn{1}{l}{#1}}
\usepackage{amssymb}% http://ctan.org/pkg/amssymb
\usepackage{pifont}% http://ctan.org/pkg/pifont
\newcommand{\cmark}{\ding{51}}%
\newcommand{\xmark}{\ding{55}}%
\newcommand*\rot{\rotatebox{90}}
\usepackage{tabularx}

\usepackage{msc}
\usepackage{xcolor}
\newcommand{\quotes}[1]{``#1''} %for qoutation marks

\DeclareRobustCommand*{\IEEEauthorrefmark}[1]{%
\raisebox{0pt}[0pt][0pt]{\textsuperscript{\footnotesize\ensuremath{#1}}}}

\begin{document}
\title{Test}

\maketitle

\begin{table}
\caption{mytable}
\label{table:mytable}

\begin{tabular}{ l *{45}{c} }

  \toprule
    &   \multicolumn{3}{c}{B} \\
  \cmidrule(lr){2-4}
  \multicolumn{1}{c}{\multirow{-2}{*}[0.5ex]{A}}
    &   AAA & BBB & CCC \\
  \midrule
  \rowcolor[gray]{.9}
  Text1 &   \cmark & \cmark & \cmark \\
  Text2 &   \xmark & \cmark & \xmark \\
  \rowcolor[gray]{.9}
  Text3 &   \cmark & \xmark & \xmark \\
  Text4 &   \cmark & \cmark & \cmark \\
  \bottomrule
\end{tabular}
\end{table}
\end{document}
2

7 Answers 7

24

enter image description here

The spacing can be adjusted using \aboverulesep = 0.605mm (adjusts the separation above all kinds of rules except the top one and it has a default value of 0.4ex or 0.605mm) and \belowrulesep = 0.984mm (adjusts the separation below all kinds of rules except the bottom one and it has a default value of 0.65ex or 0.984mm).

If you need to remove the separation frequently you can define \newcommand{\midsepremove}{\aboverulesep = 0mm \belowrulesep = 0mm} and \newcommand{\midsepdefault}{\aboverulesep = 0.605mm \belowrulesep = 0.984mm} to remove spacing and reset it to the default value. Before tabular environment use \midsepremove and after it use \midsepdefault. This helps reset the spacing to the default values and avoid crowded lines in normal tables.

%README: this copy with comprehensive attack and defences description.

\documentclass[compsoc, conference, letterpaper, 10pt, times]{IEEEtran}
\ifCLASSOPTIONcompsoc
  % IEEE Computer Society needs nocompress option
  % requires cite.sty v4.0 or later (November 2003)
  \usepackage[nocompress]{cite}
\else
  % normal IEEE

  \usepackage{cite}
\fi
\ifCLASSINFOpdf
\else
\fi

\usepackage{colortbl}       
\usepackage{array}
\usepackage{booktabs}

    % The new commands
    \newcommand{\midsepremove}{\aboverulesep = 0mm \belowrulesep = 0mm}
    \midsepremove
    \newcommand{\midsepdefault}{\aboverulesep = 0.605mm \belowrulesep = 0.984mm}
    \midsepdefault

\usepackage{caption}
\captionsetup{justification=centering}

\usepackage{multirow}
\newcommand{\head}[1]{\textnormal{\textbf{#1}}}
\newcommand{\normal}[1]{\multicolumn{1}{l}{#1}}
\usepackage{amssymb}% http://ctan.org/pkg/amssymb
\usepackage{pifont}% http://ctan.org/pkg/pifont
\newcommand{\cmark}{\ding{51}}%
\newcommand{\xmark}{\ding{55}}%
\newcommand*\rot{\rotatebox{90}}
\usepackage{tabularx}

\usepackage{msc}
\usepackage{xcolor}
\newcommand{\quotes}[1]{``#1''} %for qoutation marks

\DeclareRobustCommand*{\IEEEauthorrefmark}[1]{%
\raisebox{0pt}[0pt][0pt]{\textsuperscript{\footnotesize\ensuremath{#1}}}}

\begin{document}
\title{Test}

\maketitle

\begin{table}
\caption{mytable}
\label{table:mytable}
\centering

\midsepremove
\begin{tabular}{ l *{45}{c} }

  \toprule
    &   \multicolumn{3}{c}{B} \\
  \cmidrule(lr){2-4}
  \multicolumn{1}{c}{\multirow{-2}{*}[0.5ex]{A}}
    &   AAA & BBB & CCC \\
  \midrule
  \rowcolor[gray]{.9}
  Text1 &   \cmark & \cmark & \cmark \\
  Text2 &   \xmark & \cmark & \xmark \\
  \rowcolor[gray]{.9}
  Text3 &   \cmark & \xmark & \xmark \\
  Text4 &   \cmark & \cmark & \cmark \\
  \bottomrule
\end{tabular}
\midsepdefault

\end{table}
\end{document}

Off-topic: use \centering to center the table below the caption. Also, use \captionsetup{justification=centering} for better handling of floats caption positions.

6
  • Are you sure your suggestions will be acceptable to the conference or journal to which the OP is submitting?
    – cfr
    Jul 22, 2017 at 2:31
  • Don't use mm for the \aboveruleseps. In \midsepdefault you should use 0.4ex and 0.65ex for the one below. There is a reason why these are in ex and not in mm or another absolute measurement.
    – Skillmon
    Jul 22, 2017 at 6:16
  • @cfr Yes, modify columns separation is acceptable if it makes the document more readable. The editor will make the final decisions on how the format will be if the paper is accepted. Jul 22, 2017 at 6:54
  • @Skillmon. Why shouldn't we use mm here? Jul 22, 2017 at 6:56
  • @Al-MotasemAldaoudeyeh because \aboverulesep is 1.8067pt without your redefinition to 0.605mm and 1.72137pt with it. Well maybe you shouldn't use ex in your command either (because if it's called in a surrounding of different font size, the width is wrong either). You might use mm, but then use the correct value.
    – Skillmon
    Jul 22, 2017 at 7:04
14

You can change \midrule by something like \specialrule{.4pt}{2pt}{0pt} and the \bottomrule by \specialrule{.8pt}{0pt}{2pt}

mwe

4
  • 1
    I think you should use \heavyrulewidth for the width of the \bottomrule and \lightrulewidth for \midrule to ensure a uniform look.
    – Skillmon
    Jul 21, 2017 at 22:51
  • @Skillmon, uhmm...yes, if you change \heavyrulewidth length or you can distinguish 0.8pt of the default (0.80301pt).
    – Fran
    Jul 21, 2017 at 23:00
  • 1
    @Fran can you post the modified script please? Jul 21, 2017 at 23:19
  • 1
    @user6875880 Why? Is just replace both commands in your own example, nothing too tricky.
    – Fran
    Jul 22, 2017 at 9:37
9

enter image description here

With use of cellspace package for adding S attribute to columns type and settings above and below booktabs' rules space to zero:

\documentclass[compsoc, conference, letterpaper, 10pt, times]{IEEEtran}

\usepackage{pifont}
\newcommand{\cmark}{\ding{51}}%
\newcommand{\xmark}{\ding{55}}%

\usepackage[table]{xcolor}
\usepackage{booktabs, cellspace, multirow}
\setlength\aboverulesep{0pt}
\setlength\belowrulesep{0pt}
\setlength\cellspacetoplimit{3pt}
\setlength\cellspacebottomlimit{3pt}

\begin{document}
    \begin{table}
    \centering
\caption{Table title}
    \label{tab:my important table}
\rowcolors{2}{}{gray!20}
\begin{tabular}{ l *{4}{Sc} }
  \toprule
    &   \multicolumn{3}{Sc}{B} \\
  \cmidrule(lr){2-4}
  \multicolumn{1}{c}{\multirow{-2}{*}[1.5pt]{A}}
    &   AAA & BBB & CCC \\
  \midrule
  Text1 &   \cmark & \cmark & \cmark \\
  Text2 &   \xmark & \cmark & \xmark \\
  Text3 &   \cmark & \xmark & \xmark \\
  Text4 &   \cmark & \cmark & \cmark \\
  \bottomrule
\end{tabular}
    \end{table}
\end{document}
1

When using tabularx (for whatever reason), I found this solution useful:

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

\newcommand{\midruletop}{\specialrule{\lightrulewidth}{\aboverulesep}{0em}}
\newcommand{\midrulebottom}{\specialrule{\lightrulewidth}{0em}{\belowrulesep}}
\newcommand{\vertsp}{{\Large$\phantom{\mid}$\hspace{-.25em}}}
\newcommand{\hlrow}{\rowcolor{lightgray}\vertsp}
\newcommand{\hlcell}{\cellcolor{lightgray}\vertsp}

\begin{document}
  \begin{tabularx}{.5\columnwidth}{XX}
    \toprule
    \textbf{A} & \textbf{B}
    \\\midruletop
    \multicolumn{2}{l}{\hlcell a}
    \\\midrulebottom
    a & b
    \\\midruletop
    \hlrow a & b
    \\\midrulebottom
  \end{tabularx}
\end{document}

For highlighting the last line, create a new command from \midrulebottom using \heavyrulewidth.

Note that the cellspace manual suggests that it might also work with tabularx. I didn't try, though.

enter image description here

2
  • There appears to be a white line bw columns. Can that be avoided? Sep 7, 2021 at 12:17
  • I suppose that's only due to my PDF renderer. Try rendering it on your own and zoom in and out. Hope it works for you.
    – mfg
    Sep 7, 2021 at 14:22
1

An easy solution with booktabs environment of tabularray package: row colors work as expected.

Note that you need to put trim option lr into square brackets.

\documentclass{article}

\usepackage{xcolor}       
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\usepackage{pifont}% http://ctan.org/pkg/pifont
\newcommand{\cmark}{\ding{51}}%
\newcommand{\xmark}{\ding{55}}%

\begin{document}

\begin{table}
\centering
\caption{mytable}
\label{table:mytable}
\begin{booktabs}{
  colspec = {l*{3}{c}},
  cell{1}{1} = {r=2}{c}, % multirow
  cell{1}{2} = {c=3}{c}, % multicolumn
}
  \toprule
        A &   B      &        &        \\
  \cmidrule[lr]{2-4}
          &   AAA    & BBB    & CCC    \\
  \midrule
  \SetRow{gray9}
    Text1 &   \cmark & \cmark & \cmark \\
    Text2 &   \xmark & \cmark & \xmark \\
  \SetRow{gray9}
    Text3 &   \cmark & \xmark & \xmark \\
    Text4 &   \cmark & \cmark & \cmark \\
  \bottomrule
\end{booktabs}
\end{table}

\end{document}

enter image description here

1

A small variation of interesting L.J.R. answer (+1), for exercise :-)

\documentclass[border=3.141592, varwidth]{standalone}

\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\usepackage{pifont}
\newcommand{\cmark}{\ding{51}}%
\newcommand{\xmark}{\ding{55}}%

\begin{document}
    \begin{table}
    \centering
\caption{My table}
\label{table:mytable}
\begin{booktabs}{colspec = {l*{3}{c}},
                 row{odd}={gray9},
                 row{1}={bg=},      <--- a way to define "not colored" row
                }
  \toprule
\SetCell[r=2]{c} A
            &   \SetCell[c=3]{c} B
                        &        &        \\
  \cmidrule[lr]{2-4}
            &   AAA     & BBB    & CCC    \\
  \midrule
    Text1   &   \cmark  & \cmark & \cmark \\
    Text2   &   \xmark  & \cmark & \xmark \\
    Text3   &   \cmark  & \xmark & \xmark \\
    Text4   &   \cmark  & \cmark & \cmark \\
  \bottomrule
\end{booktabs}
    \end{table}
\end{document}

enter image description here

7
  • Good usage of bg=. Better than using bg=white.
    – L.J.R.
    Sep 11, 2021 at 6:10
  • And maybe we need an optional argument to odd and even selectors. It would be great if we could write odd[3] here.
    – L.J.R.
    Sep 11, 2021 at 6:22
  • @L.J.R., yes. I just intent to write a wish on Github, if it is possible to start even/odd coloring of rows at some row. Similarly as it is possible with rows{<start at row>}{<color 1<}{<color2>} as I did in the my first answer (using [table]{xcolor} package).
    – Zarko
    Sep 11, 2021 at 6:58
  • An optional argument has been added to odd and even selectors in the source code. In next version 2022A you will be able to write odd[3] or even[2-Y] for example.
    – L.J.R.
    Jan 30, 2022 at 10:25
  • @L.J.R., thank you for info! When will the 2022A be available?
    – Zarko
    Jan 30, 2022 at 11:08
1

If you want to keep the exact positions of the rules drawn with booktabs and to color rows with respect to these rules, you may use nicematrix. You need several compilations.

\documentclass{article}

\usepackage{nicematrix}
\usepackage{booktabs}
\usepackage{pifont}
\newcommand{\cmark}{\ding{51}}
\newcommand{\xmark}{\ding{55}}

\begin{document}

\begin{NiceTabular}{lccc}[color-inside]
  \toprule
  \Block{2-1}{A} &  & B & \\
  \cmidrule{2-4} 
  & AAA & BBB & CCC \\
  \midrule
  \rowcolors[gray]{0.9}{}
  Text1 & \cmark & \cmark & \cmark \\
  Text2 & \xmark & \cmark & \xmark \\
  Text3 & \xmark & \xmark & \xmark \\
  Text4 & \cmark & \cmark & \cmark \\
  \bottomrule
\end{NiceTabular}

\end{document}

Result of the above code

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .