0

How can I align numbers in table according to the decimal comma please when I don't know how many numbers are befor the comma and how many decimal places the number will have?

\documentclass[12pt,a4paper]{report}
\usepackage[english,czech]{babel}
\usepackage[margin=2.5cm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern,textcomp}
\usepackage{amsmath, amssymb, bm}
\usepackage[nottoc]{tocbibind}
\usepackage{icomma,dcolumn,booktabs}

\newcommand{\mc}[1]{\multicolumn{1}{c}{#1}}
\usepackage{tabularx, ragged2e}
\newcolumntype{d}[1]{D{.}{,}{#1}}
\newcolumntype{L}{>{\RaggedRight\arraybackslash}X}
\begin{document}

\begin{table}\small
\setlength\tabcolsep{3pt}
\begin{tabularx}{\textwidth}{@{} L *{8}{c} @{}}
\toprule
Číslo & $P$ & $T_0$ & $e$ & $\omega$ & $i$ & $a$  \\[0.33ex]
zdroje& (dny) & (RJD) & & ($^\circ$) & ($\mathrm{M}_{\odot}$) & ($\mathrm{M}_{\odot}$) \\
\midrule
1 & 5,732436(15) & 54002,780(46) & 0,0910(37) & 149,2(3,0) &  &  \\
2 & 5.732824(3) & 54002,7241(96) & 0,0858(41) & 145,6(3,9) && \\
3 & 5.732824(1) & 54002,7775(34) & 0,0883(26) & 149,1(2,3) && \\
\bottomrule
\addlinespace

\end{tabularx}
\end{table}
\end{document}

enter image description here

  • 1
    Could you please describe a case where you have a table with numbers of which you don't know 'how many numbers are before the comma and how many decimal places the number will have'? In the example code in your question, you could just count. – leandriis Nov 25 '18 at 16:02
  • Do you know of siunitx package? – AboAmmar Nov 25 '18 at 16:02
  • I mean that not all number will have same format - same count before and after decimal comma. So it should be set according to the greatest? I know about siunitx package but it doesn't work because I do not have an update and I don't know how it update - I can not find MikTex Console in my computer. – Elisabeth Nov 25 '18 at 16:35
  • it seems that you have an old MikTeX installation ... for upgrade use upgrade utility or better console. for instruction see MikTeX home page. number of digits you need to accommodate to the number with biggest number of digits in each column separately (as i did in my answer) – Zarko Nov 25 '18 at 20:09
  • I don't know how to update it tex.stackexchange.com/questions/461728/… – Elisabeth Nov 25 '18 at 20:11
2

with siunitx and makecell packages:

enter image description here

in your mwe you have mess in use of decimal points and commas. i decide to use decimal points. if you like to have commas in resulting table, than you need to add in preamble:

\sisetup{output-decimal-marker={,}}

also your use of uncertainty (with commas) is not clear. such notation is not supported in the siunitx pacakage (as far as i know).

\documentclass[12pt,a4paper]{report}
\usepackage{booktabs, makecell}
\usepackage{siunitx}

\begin{document}
    \begin{table}[htb]
    \small
    \setlength\tabcolsep{0pt}
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} 
                            c
                            S[table-format=1.6(2)]
                            S[table-format=5.3(2)]
                            S[table-format=1.4(2)]
                            S[table-format=3.1(2)]
                            cc % for those columns is not clear what is in them
                            }
    \toprule
\makecell[t]{Číslo\\ zdroje}
    &   {\makecell[t]{$P$\\ (dny)}}
        &   {\makecell[t]{$T_0$\\ (RJD)}}
            &   {\makecell[t]{$e$}}
                &   {\makecell[t]{$\omega$\\ (\si{\degree})}}
                    &   {\makecell[t]{$i$\\ ($\mathrm{M}_{\odot}$)}}
                        &   {\makecell[t]{$a$\\ ($\mathrm{M}_{\odot}$)}}    \\
    \midrule
1 & 5.732436(15) & 54002.780(46)  & 0.0910(37) & 149.2(30) &   &            \\
2 & 5.732824(3)  & 54002.7241(96) & 0.0858(41) & 145.6(39) &   &            \\
3 & 5.732824(1)  & 54002.7775(34) & 0.0883(26) & 149.1(23) &   &            \\
    \bottomrule
\end{tabular*}
    \end{table}
\end{document}
  • +1. Since the OP uses commas rather than dots as the decimal separator, I'd addt the option output-decimal-marker={,}. – Mico Nov 25 '18 at 17:09
  • @Mico, i sow that she has mess in using decimal dots and commas. also i'm confusing in use of comas in uncertainty. but this over latex issue :-). i will edit my answer. – Zarko Nov 25 '18 at 17:13
  • Thank you very much and S[table-format=1.6(2)] belongs to siunitx package? – Elisabeth Nov 25 '18 at 19:33
  • 1
    @Elisabeth, yes, it is. for more details you need to read its documentation. it is very educational. beside latex topic it is also informative about SI units. S[table-format=1.6(2)] means that column is centered at decimal point, has reserved place for 1 integer digit and 6 decimal, to which follows 2 digits for uncertainty. – Zarko Nov 25 '18 at 19:53
  • I have a problem with this package (I added picture in my question). I have it installed, but there is a problem. – Elisabeth Nov 25 '18 at 19:56

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.