1

I want to align some decimal columns in a tabular on their decimal point. I tried to accomplish this with the dcolumn package, but when I use it as listed in the following, my column width gets bloated that it will not fit on the page (A4).

\usepackage{dcolumn}
\usepackage{hhline, multirow, array, cellspace}
\setlength{\cellspacetoplimit}{3pt}
\setlength{\cellspacebottomlimit}{3pt}
\newcolumntype{.}{D{.}{.}{-2}}
...
{\setlength{\doublerulesep}{1em}
\centering
\begin{tabular}{*{3}{ | Sc | Sc | S. |}}
\hhline{---||---||---}
        \multicolumn{1}{|Sc|}{\multirow{2}{*}{\textbf{Symbol}} } &
    \multicolumn{2}{Sc||}{\textbf{Probability}} &
    \multicolumn{1}{Sc|}{\multirow{2}{*}{\textbf{Symbol}} } &
    \multicolumn{2}{Sc||}{\textbf{Probability}} &
    \multicolumn{1}{Sc|}{\multirow{2}{*}{\textbf{Symbol}} } &
    \multicolumn{2}{Sc|}{\textbf{Probability}} \\ 
\hhline{|~|--||~|--||~|--}
    & \textbf{abs.} & \textbf{rel. [\%]} &
    & \textbf{abs.} & \textbf{rel. [\%]} &
    & \textbf{abs.} & \textbf{rel. [\%]} \\
\hhline{---||---||---}
    A & 22 & 81.16 & J & 3 & 0.16 & S & 34 & 6.32 \\
    B & 104 & 1.49 & K & 58 & 0.77 & T & 33 & 9.05 \\
\hhline{---||---||---}
\end{tabular}}

Without dcolumn alignment:

\begin{tabular}{*{3}{ | Sc | Sc | Sc |}}

enter image description here

With dcolumn alignment:

\begin{tabular}{*{3}{ | Sc | Sc | S. |}}

enter image description here

Can someone please help me to align the decimal values in a compact way?

1

Two suggestions:

  • Place the cell headers -- \textbf{rel. [\%]} -- in \multicolumn{1}{c||}{...} wrappers

  • Define a new column type -- say, d -- which takes an argument but otherwise performs alignment on the decimal markers. Using the setup afforded by \newcolumntype{.}{D{.}{.}{-2}} simply doesn't let you achieve really compact numerical columns.

  • I would perform alignment on the (implied) decimal markers in the "abs." columns as well.

In the following code, note that the specification for the first pair of decimal-aligned columns is different from those of the other two pairs.

enter image description here

\documentclass{article}
%% simplified code to bare minimum
\usepackage{dcolumn, hhline, geometry}
\newcolumntype{d}[1]{D{.}{.}{#1}}
\renewcommand\arraystretch{1.5}
\begin{document}
\setlength{\doublerulesep}{1em}
\centering
\begin{tabular}{ |c|d{3.0}|d{2.2}| *{2}{ |c|d{2.0}|d{1.2}|} }
\hhline{---||---||---}
Symbol & \multicolumn{2}{c||}{Probability} &
Symbol & \multicolumn{2}{c||}{Probability} &
Symbol & \multicolumn{2}{c|}{Probability} \\
\hhline{|~|--||~|--||~|--}
  & \multicolumn{1}{c|}{abs.} & \multicolumn{1}{c||}{rel.\ [\%]}
& & \multicolumn{1}{c|}{abs.} & \multicolumn{1}{c||}{rel.\ [\%]}
& & \multicolumn{1}{c|}{abs.} & \multicolumn{1}{c|}{rel.\ [\%]} \\
\hhline{---||---||---}
    A & 22 & 81.16 & J &  3 & 0.16 & S & 34 & 6.32 \\
    B & 104 & 1.49 & K & 58 & 0.77 & T & 33 & 9.05 \\
\hhline{---||---||---}
\end{tabular}
\end{document}

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.