Graphics, Figures & Tablesproblems with colortbl and tabular environment

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
aminoxol
Posts: 2
Joined: Sat May 16, 2009 12:10 am

problems with colortbl and tabular environment

Post by aminoxol »

Hello TeXnicians,
I've come across a weird problem. I wanted to form a table with lots of decimal numbers properly aligned around the radix point. I also wanted each row to be colored. So I used the colortbl package and the r@{.}l syntax but the result is bad. You can try the following code

Code: Select all

\documentclass{report}
\usepackage{colortbl}

\begin{document}

\begin{figure}
\centering
\begin{tabular}{*{2}{r@{.}l}}
\rowcolor[gray]{0.9} 3&14159 &2&7818987\\
1&41 &273&16
\end{tabular}
\caption{WTF?}
\end{figure}

\end{document}
The thing is that r@{.}l puts dots everywhere before puting the entries, so when color is applied everything gets ugly. I don't know if this has been discussed or not. If you know a workaround please share.
Cheers

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

problems with colortbl and tabular environment

Post by localghost »

Try another approach with numbers aligned to the decimal symbol by using the siunitx package.

Code: Select all

\documentclass[11pt,a4paper,english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage{babel}
\usepackage[includeheadfoot,margin=2cm]{geometry}
\usepackage[font=small,labelfont=bf,tableposition=top]{caption}
\usepackage[table]{xcolor}
\usepackage[
  decimalsymbol=comma,
  emulate=units,
  expproduct=cdot,
  load=abbr
]{siunitx}
\usepackage{lmodern}

\begin{document}
  \begin{table}[!ht]
    \centering
    \caption{A table with a coloured row and aligned decimal numbers}\label{tab:colrow}
    \begin{tabular}{%
      S[tabnumalign=centre,tabformat=1.5]%
      S[tabnumalign=centre,tabformat=3.7]%
    }
      \rowcolor{gray!20} 3.14159 & 2.7182818 \\
      1.41 & 273.16
    \end{tabular}
  \end{table}
\end{document}
This and all other capabilities of the package are described in its comprehensive manual.


Best regards and welcome to the board
Thorsten
aminoxol
Posts: 2
Joined: Sat May 16, 2009 12:10 am

Re: problems with colortbl and tabular environment

Post by aminoxol »

Thank you very much, it works like a charm!
Post Reply