Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am trying to add color to certain columns in a table for highlighting purposes. In said columns, I have positioned the data presented to be aligned by the decimal point. However, when adding color to these columns, this alignment is disregarded. Any thoughts about how to restore this alignment? I have provided MWE below.

\documentclass{article}

\usepackage{graphicx,xcolor,colortbl}

\definecolor{Gray}{gray}{0.85}
\definecolor{LightCyan}{rgb}{0.88,1,1}

\setlength{\columnseprule}{0.8pt}

\begin{document} 
\newcolumntype{g}{>{\columncolor{Gray}}c}

\begin{table}
\begin{tabular}{l| g@{,}g r@{.}l g@{,}g r@{.}l}
\hline
\multicolumn{1}{c}{\cellcolor{LightCyan}} &\multicolumn{2}{c}{\cellcolor{LightCyan}x}&\multicolumn{2}{c}{\cellcolor{LightCyan}y}&\multicolumn{2}{c}{\cellcolor{LightCyan}w}&\multicolumn{2}{c}{\cellcolor{LightCyan}z}\\
\hline
variable 1 &-0&54&&&1&64&&\\
variable 2&&&&&&&&\\\hline
\end{tabular}
\end{table}

\end{document}
share|improve this question

1 Answer 1

I'm not sure if I got your problem correctly, but here is how I would do it.

First some explanations ahead:

  • dcolumn is a package that provides you with columns for aligning decimal numbers at a given seperator, e.g. I define in the following two new columntypes using D{.}{.}{2}, the first . is your input and the second the output seperator, the 2 sets alignment to two decimal places
  • \mc is short for a centered multicolumn
  • a and b are white and gray dcolumns
  • \rowcolor{LightCyan} sets the color for the entire row, so you don't have do define it for every single cell

Code

\documentclass{article}
\usepackage{graphicx,xcolor,colortbl}
\usepackage{dcolumn}

\newcommand{\mc}[2]{\multicolumn{#1}{c}{#2}}
\definecolor{Gray}{gray}{0.85}
\definecolor{LightCyan}{rgb}{0.88,1,1}

\newcolumntype{a}{>{\columncolor{Gray}}D{.}{.}{2}}
\newcolumntype{b}{>{\columncolor{white}}D{.}{.}{2}}

\begin{document} 


\begin{table}
\begin{tabular}{l | a | b | a | b}
\hline
\rowcolor{LightCyan}
\mc{1}{}  & \mc{1}{x} & \mc{1}{y} & \mc{1}{w} & \mc{1}{z} \\
\hline
variable 1 & -0.54 & & 1.64 &  \\
variable 2 &       & &      &  \\ \hline
\end{tabular}
\end{table}

\end{document}

Resulting in:

image

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.