TransWikia.com

table with siunitx - alignment on precision mode

TeX - LaTeX Asked on January 28, 2021

enter image description here

In a test on making tables, i use the cool package siunitx for having some defined precision on values after the comma and to aligning these values on the dot in the middle column but unfortunately, it is doing some alignment on the text Val 2, the $beta$ remained untouched. Does the number 2 being considered as a value for siunitx, even with the text before as it was like a 2.00 ?
or do i have to create a separate table for the first line (Val 1 … Val 3) – not convinced…

here is my code :

documentclass[11pt,a4paper]{article}
usepackage[utf8]{inputenc}
usepackage[T1]{fontenc}
usepackage{frcursive}
usepackage{amsmath}


% le package qui pemet de définir des unités et leur affichage
usepackage{siunitx} % Required : alignement des valeurs etc.
sisetup{
    round-mode          = places, % Rounds numbers
    round-precision     = 2, % to 2 places
}

begin{document}

%exemples de tableau
noindentbegin{cursive}Des tableaux:end{cursive}
begin{table}[h!]
    begin{center}

    label{tab:table1}
        
        begin{tabular}{l|c|r} % Alignements: left, center, right
            textbf{Val 1} & textbf{Val 2} & textbf{Val 3}\
            $alpha$ & $beta$ & $gamma$ \
            hline
            1 & 1110.1 & a\
            2 & 10.1 & b\
            3 & 23.113231 & c\
        end{tabular}
    caption{Valeurs basiques}

vspace{1cm}    

    label{tab:table2}
    
    begin{tabular}{l|S|r} % c devient S ==> unitsx
        textbf{Val 1} & textbf{Val 2} & textbf{Val 3}\
        $alpha$ & $beta$ & $gamma$ \
        hline
        1 & 1110.1 & a\
        2 & 10.1 & b\
        3 & 23.113231 & c\
    end{tabular}
  caption{Valeurs alignées et arrondies}
  
    end{center}
  end{table}


end{document}

One Answer

  • you need to define table-format to number of decimal digits which you like to show in table, in your case table-format=4.2
  • column headers, which are not numbers and should be centered in text, should be enclosed in curly braces
  • labels for tables had to be after caption
documentclass[11pt,a4paper]{article}
usepackage[T1]{fontenc}
usepackage{frcursive}
usepackage{amsmath}
% le package qui pemet de définir des unités et leur affichage
usepackage{siunitx} % Required : alignement des valeurs etc.
sisetup{   % this is now globally set, you ma consider to move it inside table
    round-mode      = places, % Rounds numbers
    round-precision = 2, % to 2 places
}

begin{document}
%exemples de tableau
noindentbegin{cursive}Des tableaux:end{cursive}
    begin{table}[h!]
centering
    begin{tabular}{l|c|r} % Alignements: left, center, right
textbf{Val 1} & textbf{Val 2} & textbf{Val 3}\
$alpha$ & $beta$ & $gamma$ \
    hline
1 & 1110.1      & a\
2 & 10.1        & b\
3 & 23.113231   & c\
    hline
    end{tabular}
caption{Valeurs basiques}
label{tab:table1}
   
vspace{2baselineskip}
    begin{tabular}{l|S[table-format=4.2]|r}             % <---
textbf{Val 1}  & {textbf{Val 2}} & textbf{Val 3}  \  % <---
$alpha$}       & {$beta$}        & $gamma$        \  % <---
    hline
1 & 1110.1      & a\
2 & 10.1        & b\
3 & 23.113231   & c\
    hline
    end{tabular}
caption{Valeurs alignées et arrondies}
label{tab:table2}
  end{table}
end{document}

enter image description here

Correct answer by Zarko on January 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP