Table: align comma-separated pairs of numbers on comma

alignmenttablesvertical alignment

I have a table where entries are two (rational/integer) numbers separated by a comma. Is it possible in each column to (a) keep the column labels $L$, $M$, and $R$ centered and (b) align the entries so that the commas are aligned?

\documentclass{article}
\usepackage{array}
\begin{document}
\begin{table}
  \centering
  \setlength{\extrarowheight}{3pt}
  \begin{tabular}{c|ccc|}
       \multicolumn{1}{c}{} & \multicolumn{1}{c}{$L$} & \multicolumn{1}{c}{$M$} & \multicolumn{1}{c}{$R$} \\ \cline{2-4}
       $T$                  & $-1, \frac{3}{5}$       & $3,4$                   & $0,-1$                  \\
       $B$                  & $1,-8$                  & $2,-4$                  & $0,1$                   \\ \cline{2-4}
    \end{tabular}
\end{table}
\end{document}

enter image description here

Best Answer

The dcolumn package is your friend.

enter image description here

Addendum: In the code used here, no whitespace padding is inserted around the commas. If do you want the commas to be surrounded by, say, \thinspace (aka \,), just change \newcolumntype{C}{D{,}{,}{2.2}} to \newcolumntype{C}{D{,}{\,,\,}{2.2}}.

\documentclass{article}
\usepackage{dcolumn}        % for 'D' column type
\newcolumntype{C}{D{,}{,}{2.2}} % center cell contents on commas
\newcommand\mc[1]{\multicolumn{1}{c}{#1}} % handy shortcut macro

\begin{document}
\begin{table}
\centering
\setlength{\extrarowheight}{3pt}
  $\begin{array}{c|CCC|}
  \mc{} & \mc{L} & \mc{M} & \mc{R} \\ 
  \cline{2-4}
    T & -1, \frac{3}{5} & 3,4  & 0,-1  \\
    B & 1,-8            & 2,-4 & 0,1   \\ 
  \cline{2-4}
  \end{array}$
\end{table}
\end{document}
Related Question