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 produce a basic color table as shown below

\documentclass{article}
 \usepackage{booktabs}
 \usepackage[dvipsnames*,svgnames]{xcolor}
 \usepackage{colortbl}
 \usepackage{array}
 \begin{document}

\begin{table}
\centering
\setlength{\aboverulesep}{0pt}
\setlength{\belowrulesep}{0pt}
\setlength{\extrarowheight}{.75ex}
\begin{tabular}{|>{\columncolor{LightSteelBlue}}c| c | c | c|}
\toprule
 \rowcolor{LightSteelBlue}  & {Mann} & {Kvinne} & {Totalt} \\ 
 \rowcolor{LightSteelBlue}  &        &          &        \\ \midrule
 \O nsker        &      &        &        \\ 
 Ballbinge       &      &        &        \\ \midrule
 \O nsker ikke   &      &        &        \\ 
 Ballbinge       &      &        &        \\ \midrule
 Totalt          &      &        &        \\ 
                 &      &        &        \\  
\bottomrule
\end{tabular}
\end{table}
\end{document}

Now I wanted to center the text in the first row, and column, inside their boxes. My first thought was to use \multirows, alas this produces some very ugly results. The text is hidden behind the colors.

What is the proper way to create the table above, with centered text?

(Any other comments about how to improve the looks of the table are also welcome)

share|improve this question

2 Answers 2

up vote 5 down vote accepted

Page 3 of the user guide for the multirow package explains how to deal with problems caused by certain interactions between the colortbl and multirow packages. I believe the following modified form of your MWE, which incorporates the workaround described in the user guid, meets your needs to center the contents of the four cells which, so far, aren't centered correctly.

\documentclass{article}
\usepackage{booktabs,multirow}
\usepackage[dvipsnames,svgnames,table]{xcolor}
\begin{document}
\begin{table}
\centering
\setlength{\aboverulesep}{0pt}
\setlength{\belowrulesep}{0pt}
\setlength{\extrarowheight}{.75ex}
\begin{tabular}{|>{\columncolor{LightSteelBlue}}c| c | c | c|}
\toprule 
\rowcolor{LightSteelBlue} & & & \\   
\rowcolor{LightSteelBlue} 
    & \multirow{-2}{*}{Mann} 
    & \multirow{-2}{*}{Kvinne} 
    & \multirow{-2}{*}{Totalt} \\ 
\midrule
\O nsker        &      &        &        \\ 
Ballbinge       &      &        &        \\ 
\midrule
\O nsker ikke   &      &        &        \\ 
Ballbinge       &      &        &        \\ 
\midrule
                 &      &        &        \\ 
\multirow{-2}*{Totalt}& &        &        \\
\bottomrule
\end{tabular}
\end{table}
\end{document}

enter image description here

share|improve this answer

LaTeX composes the table on a row-by-row basis. Additionally, colour is also printed before any of the cell contents in order to have it show up in the background. So, you need to "wait" until the second line and reposition the cell contents - in this case, move it up about half way between the two lines. For this, the \raisebox{<len>}{<stuff>} is useful.

I've taken your MWE and moved up the appropriate cells' contents by 0.5\normalbaselineskip+\extrarowheight, which puts it about half way between the two rows:

enter image description here

\documentclass{article}
\usepackage{booktabs}
\usepackage[dvipsnames*,svgnames]{xcolor}
\usepackage{colortbl}
\usepackage{array}
\begin{document}

\begin{table}
  \centering
  \setlength{\aboverulesep}{0pt}
  \setlength{\belowrulesep}{0pt}
  \setlength{\extrarowheight}{.75ex}
  \begin{tabular}{|>{\columncolor{LightSteelBlue}}c| c | c | c|}
    \toprule
    \rowcolor{LightSteelBlue} & & & \\ 
    \rowcolor{LightSteelBlue} & %
      \raisebox{\dimexpr0.5\normalbaselineskip+\extrarowheight}{Mann} &
      \raisebox{\dimexpr0.5\normalbaselineskip+\extrarowheight}{Kvinne} &
      \raisebox{\dimexpr0.5\normalbaselineskip+\extrarowheight}{Totalt} \\ \midrule
    \O nsker      &      &        &        \\ 
    Ballbinge     &      &        &        \\ \midrule
    \O nsker ikke &      &        &        \\ 
    Ballbinge     &      &        &        \\ \midrule
                  &      &        &        \\ 
    \raisebox{\dimexpr0.5\normalbaselineskip+\extrarowheight}{Totalt} &      &        &        \\  
    \bottomrule
  \end{tabular}
\end{table}
\end{document}
share|improve this answer
    
The output of your method and of the one proposed in my answer look pretty much identical. :-) Would you happen to know if the multirow package, which I employ, basically implements the method you propose? –  Mico Nov 21 '11 at 3:19
    
@Mico: It does not seem to use the same approach. As such, I find mine to be more logical. :) –  Werner Nov 21 '11 at 3:28
1  
You don't say! :-) –  Mico Nov 21 '11 at 3:49

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.