2

I have a table where the first row contains a multirow cell. The multi-row should be coloured gray.

Question 1

Unfortunately, a thin white line appears between the multirows (see the picture at the end). Here is the code:

\begin{table}[H]
\renewcommand{\arraystretch}{1.5}
\rowcolors{2}{white}{gray!25}
\begin{center}
\begin{tabular}{ | c | c | c | }
 \arrayrulecolor{black} %changes color of hline
 \hline
 \rowcolor{gray!50} & {Max Power} & {Typ. Range}\\
 \rowcolor{gray!50}\multirow{-2}{*}{\textbf{Class}} & (mW)  &  (m) \\
 \hline
 \textbf{1} &  100 & 100\\
 \textbf{2} & 2.5 & 10\\
 \textbf{3} & 1 & 1\\
 \textbf{4} & 0.5 & 0.5\\
 \hline
\end{tabular}
\caption{Σύγκριση μεταξύ 2.4GHz και 5GHz}
\end{center}
\end{table}

Question2

Furthermore, I would like to adjust the spacing indicated with the red arrow. I would like it to be smaller, so that the text is closer to the line above, just for this specific row.

enter image description here

1
  • Not related: In a environment {table}, you should use \centering and not \begin{center} and \end{center} (for correct spaces). Nov 2, 2020 at 21:43

3 Answers 3

3

With column headers in \thead command defined in the makecell package:

\documentclass{article}

\usepackage[table]{xcolor}
\usepackage{array, makecell, multirow}
\renewcommand\theadfont{\normalsize\bfseries}

\usepackage{siunitx}


\begin{document}
    \begin{table}[ht]
    \renewcommand{\arraystretch}{1.5}
    %\arrayrulecolor{black} %changes color of hline
    \rowcolors{2}{white}{gray!25}
\centering
\begin{tabular}{ | >{\bfseries}c | c | c | }
    \hline
 \rowcolor{gray!50} 
    & \thead{Max Power\\ (mW)} & \thead{Typ. Range\\ (m)}\\
    \hline
1 &  100 & 100  \\
2 & 2.5  & 10   \\
3 & 1    & 1    \\
4 & 0.5  & 0.5  \\
    \hline
\end{tabular}
\caption{Text in Greek language \SI{2.4}{GHz} ... \SI{5}{GHz}.}
    \end{table}
\end{document}   

enter image description here

Note: please, always provide an MWE, is the smallest complete document that reproduces the problem. It should contain all necessary packages and definitions required to run the example, but nothing more than that.

1
  • Thank you for the suggestion, will do from now on.
    – NickG
    Nov 2, 2020 at 23:20
5

The package nicematrix has tools to deal with that problem. With the following code, the thin white lines won't appear (they appear in some PDF viewers with the tools of colortbl, which is loaded by the option table of xcolor).

\documentclass{article}

\usepackage{nicematrix}

\begin{document}

\begin{table}
\centering
\renewcommand{\arraystretch}{1.5}
\begin{NiceTabular}{>{\bfseries}ccc}[vlines]
\CodeBefore 
   \rowcolors{2}{white}{gray!25}
   \rowcolor{gray!50}{1,2} 
\Body
   \hline
   \Block{2-1}{Class} & Max Power & Typ. Range\\[-3mm]
                      & (mW)      &  (m) \\
   \hline
   1 &  100 & 100\\
   2 & 2.5 & 10\\
   3 & 1 & 1\\
   4 & 0.5 & 0.5\\
   \hline
\end{NiceTabular}
\end{table}

\end{document}

However, you need several compilations (because nicematrix uses PGF/Tikz nodes).

Output of the above code

2
  • Very useful answer, however I preferred to use the tabular environment. This works perfectly though as well.
    – NickG
    Nov 2, 2020 at 23:20
  • 2
    With {tabular} as in the answer of Bernard, you will still have problems in some PDF viewers: for instance, with Adobe Reader DC, at some levels of zoom, the vertical rule on the left will seem to disappear. With nicematrix, you won't have that effect because nicematrixconstructs all the colored rectangles before the construction of the rules (this is not the case with colortbl). For an example, see here. Nov 3, 2020 at 5:57
2

I suggest to use makecell and its eponymous command. This way, you won't need \multirow and the vertical spacing in the column heads will be shorter. (Note: I had to translate the caption text because I had troubles with my editor)

\documentclass{article}
\usepackage{caption, float, makecell}
\usepackage[svgnames, table]{xcolor}

\begin{document}

\begin{table}[H]
\renewcommand{\arraystretch}{1.5}
\rowcolors{2}{white}{gray!25}
\begin{center}
\begin{tabular}{ | c | c | c | }
 \arrayrulecolor{black} %changes color of hline
 \hline
 \rowcolor{gray!50}\textbf{Class} & \makecell{Max Power\\ (mW)} & \makecell{Typ. Range\\(m)}\\
 \hline
 \textbf{1} & 100 & 100\\
 \textbf{2} & 2.5 & 10\\
 \textbf{3} & 1 & 1\\
 \textbf{4} & 0.5 & 0.5\\
 \hline
\end{tabular}
\caption{Comparison between 2.4GHz and 5GHz}
\end{center}
\end{table}

\end{document}

enter image description here Edit: For the spacing problem, I suggest to delete the modification of \arraystretch, to load the cellspace package and add these lines at the beginning of the table code (if you also load siunitx replace the letter S with the letter C):

\setlength{\cellspacetoplimit}{4pt}
\setlength{\cellspacebottomlimit}{4pt}
\centering
\begin{tabular}{|c|Sc|Sc|}
3
  • what is the difference between 'makecell' and 'thead' commands? I can see that in your suggestion, there is no space around the 2-row cells. Can this be fixed? P.S. Apologies for the greek text, I forgot to change it.
    – NickG
    Nov 2, 2020 at 23:23
  • There's no difference really, but you can specify their formatting independently. Semantically, \thead should be used for column heads, but it resulted in a slightly longer code if yout to have the same font size as the rest of the table (the default font size is \footnotesize
    – Bernard
    Nov 2, 2020 at 23:29
  • 1
    @NickG: For the spacing problem, please see my edit.
    – Bernard
    Nov 2, 2020 at 23:49

You must log in to answer this question.

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