1

Quick question regarding colouring rows in a latex table:

I simply want to colour the first row in the following table light gray as in the image. Unfortunately the colour does not seem to fill the full row. I think this has something to do with my specification column separation command "\setlength{\tabcolsep}{0.5cm}".

enter image description here

\documentclass[reprint,amsmath,amssymb,aps,pra,]{revtex4-1}
\usepackage{graphicx}
\usepackage{dcolumn}
\usepackage{bm}
\usepackage{color}
\usepackage{mathrsfs}
\usepackage[table]{xcolor}

\begin{document}
\begin{figure}
\hspace*{0cm}
\setlength{\tabcolsep}{0.5cm}
    \begin{table}
        \centering
        \renewcommand{\arraystretch}{2.0}
        \begin{tabular}{|c|c|c|c|c|c|{5cm}}
        \hline
    \rowcolor[gray]{0.9}
        \multicolumn{6}{|c|}{\boldsymbol{\text{Some Text}}}\\
        \hline
        a & b & c & d & e & f \\
        \hline
        \end{tabular}
        \caption{Example...}
        \label{tab:Example}
    \end{table}
\end{figure}
\end{document}

Does anyone know how to resolve this? Thanks.

6
  • Please provide the MWE as in error free format...
    – MadyYuvi
    Oct 11, 2019 at 8:47
  • What is {5cm} supposed to do in the list of column specifiers? Probably you wanted something like p{5cm} instead?
    – leandriis
    Oct 11, 2019 at 8:51
  • You have a \boldsymbol outside mathmode.
    – Bernard
    Oct 11, 2019 at 8:52
  • Aslo, \boldsymbol will only work if used in math mode. Nevertheless, \boldsymbol{\text{Some Text}} seems quite unusual. Maybe you wanted \textbf{some text} instead?
    – leandriis
    Oct 11, 2019 at 8:52
  • 1
    Please check the post tex.stackexchange.com/questions/294790/… which may suits with your requirement...
    – MadyYuvi
    Oct 11, 2019 at 8:58

2 Answers 2

4

You've modified the value of \tabcolsep inside the figure environment, not in the preamble, so \rowcolor (and \columncolor) overhang is based on the default value. Workaround: use the optional overhang argument.

Unrelated: needless to load color when you load xcolor.

\documentclass[reprint,amsmath,amssymb,aps,pra,]{revtex4-1}
\usepackage{graphicx}
\usepackage{dcolumn}
\usepackage{bm}
\usepackage{mathrsfs}
\usepackage[table]{xcolor}

\begin{document}

\begin{figure}
\hspace*{0cm}
\setlength{\tabcolsep}{0.5cm}
    \begin{table}
        \centering
        \renewcommand{\arraystretch}{2.0}
        \begin{tabular}{|c|c|c|c|c|c|p{5cm}}
        \hline
    \rowcolor[gray]{0.9}[0.5cm]
        \multicolumn{6}{|c|}{\textbf{Some Text}}\\
        \hline
        a & b & c & d & e & f \\
        \hline
        \end{tabular}
        \caption{Example...}
        \label{tab:Example}
    \end{table}
\end{figure}

\end{document} 

enter image description here

0

With {NiceTabular} of nicematrix, you can change the value of \tabcolsep anywhere, the command \rowcolor will act as expected.

\documentclass{article}
\usepackage{graphicx}
\usepackage{bm}
\usepackage{mathrsfs}
\usepackage{nicematrix}

\begin{document}

\begin{table}
  \centering
  \renewcommand{\arraystretch}{2.0}
  \setlength{\tabcolsep}{0.5cm}
  \begin{NiceTabular}{cccccc}[colortbl-like,hvlines]
  \rowcolor[gray]{0.9}
  \Block{1-*}{\textbf{Some Text}}\\
  a & b & c & d & e & f \\
  \end{NiceTabular}
  \caption{Example...}
  \label{tab:Example}
\end{table}

\end{document} 

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

You must log in to answer this question.

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