6

I'm having a problem with making a "pretty" table

The intent is to have alternating light background colors with black text for normal rows to facilitate reading across, with the header having a blue background and white text to make it stand out, and having numbers line up on the decimal, with "nominal" columns being separated by vertical lines

I think these are the relevant lines of LaTeX code

\usepackage{color,colortbl}

\renewcommand{arrayrulewidth}{1.5}

\definecolor{headercolor}{rgb}{0.392,0.584,0.929}
\definecolor{oddrowcolor}{rgb}{0.961,1.000,0.980}
\definecolor{evenrowcolor}{rgb}{0.902,0.902,0.980}
\definecolor{white}{rgb}{1.0,1.0,1.0}

\begin{table}{!htbp}
\large
\begin{caption}
\begin{tabular}{|r@{.}l|r@{.}l|}
\hline
\multicolumn{2}{|c|}{\cellcolor{headercolor}{\color{white}{column 1 header}}} & \multicolumn{2}{|c|}{\cellcolor{headercolor}{\color{white}{column 2 header}}} \\
\hline
\rowcolor{oddrowcolor}
-0&333 & 1&444 \\
\rowcolor{evenrowcolor}
1&001 &  10.02 \\
\rowcolor{oddrowcolor}
0& & 401&32 \\
\hline
\end{tabular}
\end{center}
\caption{\label{table:example} Some caption.}
\end{table}

The problems I have are

  1. \rowcolor isn't playing nice with the align on decimal stuff (splitting the "nominal" column at the decimal and doing r@{.}l for the alignment of "actual" columns making up a "nominal" column) basically the left half of the smallest digit in the left (with alignment r) actual column and the decimal point are being hidden
  2. the vertical lines are showing up double thick in the header (twice as thick as in the normal rows)

I greatly appreciate if anyone can help me with this.

By the way, I'm compiling with a version of pdflatex that shipped with redhat 5.X (not sure what X is and I can't upgrade the machine's LaTeX because I'm not an administrator and it has to be compiled on this machine).

1
  • 1
    While code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. While solving problems is fun, setting them up is not. Then those trying to help can simply cut and paste your MWE and get started on solving problem Commented Nov 13, 2012 at 1:53

3 Answers 3

7

This seems like the perfect application for the siunitx package's S column type, which I have used below as

    \begin{tabular}{|S[table-format=1.0]S[table-format=3.0]S[table-format=3.2]S[table-format=3.0]|}

enter image description here

The S type takes an optional argument in which you can specify formatting options; I have used

table-format=<number of integers before decimal>-<number of integers after decimal>

MWE

\documentclass{article}
\usepackage{siunitx}
\usepackage{colortbl}
\definecolor{headercolor}{rgb}{0.392,0.584,0.929}
\definecolor{oddrowcolor}{rgb}{0.961,1.000,0.980}
\definecolor{evenrowcolor}{rgb}{0.902,0.902,0.980}
\definecolor{white}{rgb}{1.0,1.0,1.0}

\begin{document}

\begin{table}[!htbp]
    \centering
    \caption{SIunitx}
    \label{table:example} 
    \begin{tabular}{|S[table-format=1.0,parse-numbers=false]|S[table-format=3.0,parse-numbers=false]|S[table-format=3.2]|S[table-format=3.0]|}
        \hline
        \multicolumn{2}{|c|}{\cellcolor{headercolor}{\color{white}{column 1 header}}} & \multicolumn{2}{c|}{\cellcolor{headercolor}{\color{white}{column 2 header}}} \\
        \hline
        \rowcolor{oddrowcolor}
        -0 & 333 & 1     & 444 \\
        \rowcolor{evenrowcolor}
        1  & 001 & 10.02 &     \\
        \rowcolor{oddrowcolor}
        0  &     & 401   & 32  \\
        \hline
    \end{tabular}
\end{table}

\end{document}

Some other notes:

If you don't have access to the siunitx package, then you can achieve a similar result using the dcolumn package, as shown below

enter image description here

\documentclass{article}
\usepackage{dcolumn}
\usepackage{colortbl}
\definecolor{headercolor}{rgb}{0.392,0.584,0.929}
\definecolor{oddrowcolor}{rgb}{0.961,1.000,0.980}
\definecolor{evenrowcolor}{rgb}{0.902,0.902,0.980}
\definecolor{white}{rgb}{1.0,1.0,1.0}

\newcolumntype{d}[1]{D{.}{.}{#1}}


\begin{document}

\begin{table}[!htbp]
    \centering
    \caption{dcolumn}
    \label{table:example} 
    \begin{tabular}{|d{0}|d{0}|d{2}|d{0}|}
        \hline
        \multicolumn{2}{|c|}{\cellcolor{headercolor}{\color{white}{column 1 header}}} & \multicolumn{2}{c|}{\cellcolor{headercolor}{\color{white}{column 2 header}}} \\
        \hline
        \rowcolor{oddrowcolor}
        -0 & 333 & 1     & 444 \\
        \rowcolor{evenrowcolor}
        1  & 001 & 10.02 &     \\
        \rowcolor{oddrowcolor}
        0  &     & 401   & 32  \\
        \hline
    \end{tabular}
\end{table}
\end{document}
0

cmhughes' answer remains valid, but I guess you wanted an output like this (although you missed one . in your code where an & should have been).


I also added an example (at the beginng) that uses xcolor's \rowcolors command to get alternating row colors, so you don't have to apply them manually (what would you have done if you had twenty rows?).

It has three obligatory parameters:

  • the first line that gets the odd-row color,
  • the odd-row color, and
  • the even-row color.

\rowcolors{1}{evenrowcolor}{oddrowcolor}

The first row's color will be overwritten by its own \rowcolor command.


Loosely related to you question I added examples of tables that

  • don't use lines and colors
    • only colors
    • only horizontal lines provided by the booktabs package (which produce in my eyes a cleaner/prettier output, but your mileage may vary)
  • highlights the uses of the table-format options by siunitx.

Code

\documentclass{article}
\usepackage{siunitx}
\usepackage[table]{xcolor}
\definecolor{headercolor}{rgb}{0.392,0.584,0.929}
\definecolor{oddrowcolor}{rgb}{0.961,1.000,0.980}
\definecolor{evenrowcolor}{rgb}{0.902,0.902,0.980}
\definecolor{white}{rgb}{1.0,1.0,1.0}
\usepackage{booktabs}
\begin{document}
\centering % for this example

{%
\rowcolors{1}{evenrowcolor}{oddrowcolor}%
\begin{tabular}{S[table-format=+1.2]S[table-format=3.3]}
    \rowcolor{headercolor}{\color{white}{column 1 header}} & {\color{white}{column 2 header}} \\
    -0.333                                                 & 1.444                            \\
    1.001                                                  & 10.02                            \\
    0                                                      & 401.32                           \\
\end{tabular}%
}

\vspace{2.4em} % for this example
\begin{tabular}{S[table-format=+1.2]S[table-format=3.3]}
    \rowcolor{headercolor}{\color{white}{column 1 header}} & {\color{white}{column 2 header}} \\
    \rowcolor{oddrowcolor} -0.333                          & 1.444                            \\
    \rowcolor{evenrowcolor} 1.001                          & 10.02                            \\
    \rowcolor{oddrowcolor}  0                              & 401.32                           \\
\end{tabular}

\vspace{2.4em} % for this example
\begin{tabular}{|S[table-format=+1.2]|S[table-format=3.3]|}
    \hline
    \rowcolor{headercolor}{\color{white}{column 1 header}} & {\color{white}{column 2 header}} \\ \hline
    \rowcolor{oddrowcolor} -0.333                          & 1.444                            \\
    \rowcolor{evenrowcolor} 1.001                          & 10.02                            \\
    \rowcolor{oddrowcolor}  0                              & 401.32                           \\ \hline
\end{tabular}

\vspace{2.4em} % for this example
\begin{tabular}{S[table-format=+1.2]S[table-format=3.3]}
    \toprule
    {column 1 header} & {column 2 header} \\ \midrule
    -0.333            & 1.444             \\
    1.001             & 10.02             \\
    0                 & 401.32            \\ \bottomrule
\end{tabular}

\vspace{2.4em} % for this example
\begin{tabular}{
    S
    S[table-format = 2.2]
    S[table-format = 2.2(1)]
    S[table-format = +2.2]
    S[table-format = 2.2e1]
}
    \toprule
    {Values} & {Values} & {Values} & {Values} &  \\ \midrule
    2.3      & 2.3      & 2.3(5)   & 2.3      & 2.3e8   \\
    34.23    & 34.23    & 34.23(4) & 34.23    & 34.23   \\
    56.78    & 56.78    & 56.78(3) & -56.78   & 56.78e3 \\
    3,76     & 3,76     & 3.76(2)  & +-3.76   & e6      \\ \bottomrule
\end{tabular}
\end{document}

Output

enter image description here

1
  • @cmhughes Yes and no. If I understand the OP correctly (and based on his code), he was aiming for an output closer to my example as he was aligning the integer part and the decimal part at r@{.}l (although he has one . left in his code where an & should have been). Commented Nov 13, 2012 at 2:53
0

Thanks so much for all your help, Qrrbrbirlbel was right about aligning on the decimal. The machine I need to do this on does not have siunitx installed on it so this is the solution I'm going with (I didn't know of dcolumn so thanks for that a quick google search turned up a pdf document that mentioned using D{.}{.}{-1} would center it on the decimal rather than right justify it with N decimal places.

\documentclass{article}
\usepackage{dcolumn}
\usepackage[table]{xcolor}
\definecolor{headercolor}{rgb}{0.392,0.584,0.929}
\definecolor{oddrowcolor}{rgb}{0.961,1.000,0.980}
\definecolor{evenrowcolor}{rgb}{0.902,0.902,0.980}
\definecolor{white}{rgb}{1.0,1.0,1.0}
\newcolumntype{d}{D{.}{.}{-1}}
\begin{document}

{
\rowcolors{1}{evenrowcolor}{oddrowcolor}
\begin{tabular}{|d|d}
\multicolumn{1}{|c|}{\cellcolor{headercolor}\color{white}{column 1 header}} & \multicolumn{1}{c|}{\cellcolor{headercolor}\color{white}{column 2 header}} \\
-0.333                                                 & 1.444 \\
1.001                                                  & 10.02 \\
0                                                      & 401.32 \\
\end{tabular}
}
\end{document}

This site is temporarily in read-only mode and not accepting new answers.

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