5

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)

5

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 guide, 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

2

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}
3
  • 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
0

Instead of using multirow you may use a zero-width last column where you put an "invisible" rule with the height you need for the cell (\rule{0pt}{<height>}). Since it looks best with square cells in this type of tabulars, the height should be the same as the width (except for the first column). If you use m-columns, the text will be centred in the middle. Then, you can get rid of the multirow.

I have used hhline to compose the rules, but that seems to make no difference to ordinary \hline in this example. All the examples suffers from the "disappearing" rules problem in certain PDF-viewers. In Adobe Reader, I had to increase zoom to 200 per cent to see all rules:

\documentclass[a4paper, 10pt]{article}
\usepackage{calc, hhline}
\usepackage[dvipsnames,svgnames,table]{xcolor} 

\setlength\extrarowheight{1pt}                          % Move cell content down
\newcolumntype{C}{>{\centering\arraybackslash}m{7.5ex}}
\newcolumntype{F}{>{\centering\arraybackslash\columncolor{LightSteelBlue}}m{13ex}}
\newcolumntype{L}{@{}>{\rule{0pt}{7.5ex+1\tabcolsep}}m{0pt}@{}}   % To ensure equal row height 

 % Shortcuts
\newcommand*{\blue}{\cellcolor{LightSteelBlue}}

\arrayrulecolor{gray!75}                        % Dark gray table rules

\begin{document}

\small
\begin{tabular}{|F|*{3}{C|}L}                                                    
                    \hhline{|-|-|-|-}%

                    &   \blue Mann
                    &   \blue Kvinne
                    &   \blue Total
                    &   \\
                    \hhline{|-|-|-|-}%
                    Ønsker ballbinge
                    &   
                    &   
                    &   
                    &\\
                    \hhline{|-|-|-|-}%

                    \mbox{Ønsker ikke} ballbinge
                    &   
                    &   
                    &   
                    &\\
                    \hhline{|-|-|-|-}%
                     Total
                    &   
                    &   
                    &   
                    &\\
                    \hhline{|-|-|-|-}%
        \end{tabular}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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