6

I have a table containing both real values and text. I would like to align the values to the dot, but I do not know how to do it.

I tried to use the siunitx option {S[table-format=3.2]}, but it did not work.

Here's my code:

\begin{table}[!h]
\begin{center} 
\begin{tabular}{c c c c}
\toprule 
 & apple & banana okay & peach \\
 & tree & green garden & beach \\
\hline
aaa & \textbf{--36.68} & --20.10 & --17.35 \\
bbb & \textbf{--51.18} & --12.15 & --28.33 \\
ccc & --8.33 & --6.40 & \textbf{--14.47} \\
ddd & \textbf{--53.01} & --18.09 & --16.78 \\
eee & \textbf{--74.02} & --12.26 & --58.64 \\
fff & \textbf{--57.15} & --19.58 & --23.81 \\
ggg & \textbf{--47.56} & --17.90 & --15.26 \\
\bottomrule
\end{tabular}
\captionsetup{width=0.85\linewidth}
\caption{Caption text }
\label{tab:TAB-TAB}
\end{center}
\end{table}

And here's the current output: enter image description here

How can I align the values among the dot?

3 Answers3

4

You can adapt this answer of mine

\documentclass{article}
\usepackage{siunitx,booktabs,etoolbox}

\begin{document}

\begin{table}[!htp]
\centering

%% local redefinitions
\renewrobustcmd{\bfseries}{\fontseries{b}\selectfont}
\renewrobustcmd{\boldmath}{}

\begin{tabular}{
 @{}
 l
 *{3}{S[table-format=-2.2,detect-weight,mode=text]}
 @{}
}
\toprule 
 & {apple} & {banana okay} & {peach} \\
 & {tree} & {green garden} & {beach} \\
\midrule
aaa & \bfseries -36.68 & -20.10 &           -17.35 \\
bbb & \bfseries -51.18 & -12.15 &           -28.33 \\
ccc &            -8.33 &  -6.40 & \bfseries -14.47 \\
ddd & \bfseries -53.01 & -18.09 &           -16.78 \\
eee & \bfseries -74.02 & -12.26 &           -58.64 \\
fff & \bfseries -57.15 & -19.58 &           -23.81 \\
ggg & \bfseries -47.56 & -17.90 &           -15.26 \\
\bottomrule
\end{tabular}
\caption{Caption text }
\label{tab:TAB-TAB}

\end{table}

\end{document}

enter image description here

egreg
  • 1,121,712
2

The main problem is the mix-up of different fonts (bold and non-bold). The following example solves it in a cumbersome way by using three columns for a numeric column:

\documentclass{article}
\usepackage{amsmath}
\usepackage{booktabs}
\usepackage{caption}
\usepackage{siunitx}
\begin{document}
\begin{table}
\centering
\begin{tabular}{
  c
  r@{}c<{.}@{}l
  S[table-format=-2.2]
  r@{}c<{.}@{}l
}
\toprule
 & \multicolumn{3}{c}{apple} & {banana okay} & \multicolumn{3}{c}{peach} \\
 & \multicolumn{3}{c}{tree} & {green garden} & \multicolumn{3}{c}{beach} \\
\midrule
aaa & \boldmath$-36$&&\boldmath$68$ & -20.10 & $-17$&&$35$ \\
bbb & \boldmath$-51$&&\boldmath$18$ & -12.15 & $-28$&&$33$ \\
ccc & $-8$&&$33$ & -6.40 & \boldmath$-14$&\bfseries&\boldmath$47$ \\
ddd & \boldmath$-53$&&\boldmath$01$ & -18.09 & $-16$&&$78$ \\
eee & \boldmath$-74$&&\boldmath$02$ & -12.26 & $-58$&&$64$ \\
fff & \boldmath$-57$&&\boldmath$15$ & -19.58 & $-23$&&$81$ \\
ggg & \boldmath$-47$&&\boldmath$56$ & -17.90 & $-15$&&$26$ \\
\bottomrule
\end{tabular}
\captionsetup{width=0.85\linewidth}
\caption{Caption text }
\label{tab:TAB-TAB}
\end{table}
\end{document}

Result

Heiko Oberdiek
  • 271,626
1

Here's how to do it with siunitx. I grouped the first two rows thanks to the makecell package for a more compact code. Also, I replaced \hline with \midrule to add some padding. Note captions are traditionally placed above tables:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{siunitx}
\usepackage{booktabs, caption}
\usepackage{makecell}

\begin{document}

\begin{table}[!htb]
  \centering
  \captionsetup{width=0.85\linewidth, skip=8pt}
  \sisetup{table-format=-2.2, detect-weight}
  \caption{Caption text }
  \label{tab:TAB-TAB}
  \begin{tabular}{c *{3}{S}}
    \toprule
        & {\makecell{apple \\tree}} & {\makecell{banana okay\\green garden}} & {\makecell{peach\\beach}} \\
    \midrule
    aaa & \boldmath $ -36.68$ & -20.10 & -17.35 \\
    bbb & \boldmath$- 51.18 $ & -12.15 & -28.33 \\
    ccc & -8.33 & -6.40 & \boldmath$ -14.47 $ \\
    ddd & \boldmath $ -53.01 $ & -18.09 & -16.78 \\
    eee & \boldmath $ -74.02 $ & -12.26 & -58.64 \\
    fff & \boldmath $ -57.15 $ & -19.58 & -23.81 \\
    ggg & \boldmath $ -47.56 $ & -17.90 & -15.26 \\
    \bottomrule
  \end{tabular}
\end{table}

\end{document} 

enter image description here

Bernard
  • 271,350