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.

Bernard
  • 271,350
John Doe
  • 155
  • Please provide the MWE as in error free format... – MadyYuvi Oct 11 '19 at 08:47
  • What is {5cm} supposed to do in the list of column specifiers? Probably you wanted something like p{5cm} instead? – leandriis Oct 11 '19 at 08:51
  • You have a \boldsymbol outside mathmode. – Bernard Oct 11 '19 at 08: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 '19 at 08:52
  • @leandriis Yes that's fine. There will be math used I just reduced my tex so that I could show the incomplete row colouring for this post. Do you have an idea of why the colouring is incomplete? – John Doe Oct 11 '19 at 08:55
  • 1
    Please check the post https://tex.stackexchange.com/questions/294790/how-to-setup-a-longtable-with-booktabs-and-a-colored-head-row which may suits with your requirement... – MadyYuvi Oct 11 '19 at 08:58

2 Answers2

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

Bernard
  • 271,350
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

F. Pantigny
  • 40,250