10

I'm having difficulty creating a new command for \rowstyle that allows me to color the text of different rows in a table and preserve my ability to space out the columns and not induce space between the lines. I'm trying to stay away from Tabu. Here is what I'm using:

\documentclass{article}

 \usepackage{array}
 \usepackage{threeparttable}
 \usepackage{xcolor}
 \makeatletter
 \newcommand*{\@rowstyle}{}
\newcommand*{\rowstyle}[1]{% sets the style of the next row
 \gdef\@rowstyle{#1}%
 \@rowstyle\ignorespaces%
}
\newcolumntype{=}{% resets the row style
>{\gdef\@rowstyle{}}%
}
\newcolumntype{+}{% adds the current row style to the next column
>{\@rowstyle}%
}

 \newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
 \makeatother
 \begin{document}
\begin{table}[h!] \small
\begin{threeparttable}
\begin{tabular}{ =p{2.0cm}  +C{1.0cm} +C{1.0cm}  +C{1.0cm}   +C{1.0cm} +C{1.0cm}   +C{1.0cm}  }
 \hline
1 & -0.043 & -0.206 & -0.01 & -0.46 & 0.336 & 0.191 \\ 
\rowstyle{\color{red}}
2 & (0.902) & (0.63) & (1) & (0.286) & (0.259) & (0.502) \\ 
\rowstyle{\color{blue}}
3 & 119 & 111 & 120 & 106 & 119 & 141 \\
\rowstyle{\color{black}}
 4 & -0.04 & -0.182 & 0.074 & -0.227 & -0.189 & 0.127 \\ 
 \hline
\end{tabular}
\begin{tablenotes}
  \footnotesize
  \item \textbf{Note:} This table XYZ. 
\end{tablenotes}
\end{threeparttable}
\end{table}
\end{document}

Result

Thanks for any and all thoughts.

1
  • 1
    Do remember math mode in the table as well, there is no meaning to hyphen 0.04 ;-)
    – daleif
    Jun 18, 2013 at 15:32

1 Answer 1

8

\color introduces a whatsit (\special). At the start of a p column vertical mode is in force, that means the whatsit becomes the top of the cell. As p columns are aligned at the base line, the following text line is put below the current row base line. Adding \leavevmode helps, then the whatsit is put at the begin of the first text line in horizontal mode.

\documentclass{article}

 \usepackage{array}
 \usepackage{threeparttable}
 \usepackage{xcolor}
 \makeatletter
 \newcommand*{\@rowstyle}{}
\newcommand*{\rowstyle}[1]{% sets the style of the next row
  \gdef\@rowstyle{#1}%
  \leavevmode\@rowstyle
  \ignorespaces
}
\newcolumntype{=}{% resets the row style
  >{\gdef\@rowstyle{}\ignorespaces}%
}
\newcolumntype{+}{% adds the current row style to the next column
  >{\leavevmode\@rowstyle\ignorespaces}%
}

 \newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
 \makeatother
 \begin{document}
\begin{table}[h!] \small
\begin{threeparttable}
\begin{tabular}{ =p{2.0cm}  +C{1.0cm} +C{1.0cm}  +C{1.0cm}   +C{1.0cm} +C{1.0cm}  +C{1.0cm}}
 \hline
1 & -0.043 & -0.206 & -0.01 & -0.46 & 0.336 & 0.191 \\
\rowstyle{\color{red}}
2 & (0.902) & (0.63) & (1) & (0.286) & (0.259) & (0.502) \\
\rowstyle{\color{blue}}
3 & 119 & 111 & 120 & 106 & 119 & 141 \\
\rowstyle{\color{black}}
 4 & -0.04 & -0.182 & 0.074 & -0.227 & -0.189 & 0.127 \\
 \hline
\end{tabular}
\begin{tablenotes}
  \footnotesize
  \item \textbf{Note:} This table XYZ.
\end{tablenotes}
\end{threeparttable}
\end{table}
\end{document}

Result

You must log in to answer this question.

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