4

I need to create a table which aligns the numbers in a specific format, if x is none or a digit it would look like that: (xxx, xxx) (the first xxx could also be a negative number). What I want to do is align the entries in a column by the comma and maintain the space after the comma and I don't have a clue how to do that.

I also had a look at the siunitx handbook but I couldn't figure out how to do that.

If anyone could help me, I'd be really lucky.

Currently it looks like this: Wrong alignment

And here's a minimum example:

\documentclass[12pt,a4paper,titlepage]{article}
\usepackage{siunitx, booktabs}
\usepackage{rotating}
\begin{document}
\begin{sidewaystable}
\sisetup{
input-symbols = {()},
table-number-alignment = center,
table-figures-integer  = 2,
parse-numbers=false,
locale = DE
}

\begin{tabular}{@{}cS[separate-uncertainty, table-figures-uncertainty = 1]ll@{}}
\multicolumn{1}{l}{}           & Mean            &                       &                       \\
\multicolumn{1}{l}{Lead Time} & \multicolumn{1}{c}{1} & \multicolumn{1}{c}{2} & \multicolumn{1}{c}{3} \\ \midrule
0                              & (-10, 11)              & (-25, 116)              & (21, 240)               \\
1                              & (11, 252)               & (22, 139)               & (24, 244)               \\
2                              & (63, 390)               & (48, 21)               & (47, 27)               \\
3                              & (66, 790)               & (68, 24)               & (150, 31)              \\ \bottomrule
\end{tabular}
\end{sidewaystable}
\end{document}
  • 1
    Welcome to TeX.SX! sidewaystable is not defined in your MWE, could you add the necessary package/definition to make the code compilable. – Dai Bowen Nov 22 '16 at 15:36
  • Sorry, missed that. It's in the rotating package and just rotates the table so it's sideways – FriendlyGuy Nov 22 '16 at 15:43
  • Welcome to TeX SX! What you're trying to achieve is not clear at all. What is siunitx supposed to add, as compared to the l columns? – Bernard Nov 22 '16 at 15:51
  • I edited the description, I hope it's now more clear. I normally use the siunitx package to align normal numbers so I thought I could also use it for this purpose. – FriendlyGuy Nov 22 '16 at 16:13
4

Here's a solution that (a) uses an array environment instead of a tabular environment -- this will generate typographically correct - minus symbols -- and (b) spares you the tedium of having to type all those opening and closing parentheses.

enter image description here

\documentclass[12pt,a4paper]{article}
\usepackage{booktabs, array, amsmath, rotating}
\begin{document}
\begin{sidewaystable}
\centering
$\begin{array}{@{} c *{3}{>{(}r @{,\,} r<{)}} @{}}
\toprule
\text{Lead Time} & \multicolumn{6}{c}{\text{Mean}} \\
\cmidrule(l){2-7}
& \multicolumn{2}{c}{1} & \multicolumn{2}{c}{2} & \multicolumn{2}{c@{}}{3} \\
\midrule
0        & -10 &  11 & -25 & 116 &  21 & 240 \\
1        &  11 & 252 &  22 & 139 &  24 & 244 \\
2        &  63 & 390 &  48 &  21 &  47 &  27 \\
3        &  66 & 790 &  68 &  24 & 150 &  31 \\
\bottomrule
\end{array}$
\end{sidewaystable}
\end{document} 
  • Wohoo great, that's exactly what I was trying to do. Thank you very much :) – FriendlyGuy Nov 22 '16 at 20:44
4

Do you mean something like this?

\documentclass[12pt,a4paper,titlepage]{article}
\usepackage{siunitx, booktabs}

\newcommand{\lparen}{(}
\newcommand{\rparen}{)}

\begin{document}

\sisetup{
  table-align-text-pre=false,
  table-align-text-post=false,
}

\begin{tabular}{
  @{}
  S[table-format=1.0]
  >{\lparen}S[table-format=-2.0,table-space-text-pre=(]
  @{,\,}
  S[table-format=3.0,table-space-text-post=)]<{\rparen}
  >{\lparen}S[table-format=-2.0,table-space-text-pre=(]
  @{,\,}
  S[table-format=3.0,table-space-text-post=)]<{\rparen}
  >{\lparen}S[table-format=3.0,table-space-text-pre=(]
  @{,\,}
  S[table-format=3.0,table-space-text-post=\rparen]<{\rparen}
  @{}l@{}
}
\toprule
&\multicolumn{6}{c}{Mean} \\
{Lead Time} & \multicolumn{2}{c}{1} & \multicolumn{2}{c}{2} & \multicolumn{2}{c}{3} \\
\midrule
0         & -10 &  11 & -25 & 116 &  21 & 240 &\\
1         &  11 & 252 &  22 & 139 &  24 & 244 &\\
2         &  63 & 390 &  48 &  21 &  47 &  27 &\\
3         &  66 & 790 &  68 &  24 & 150 &  31 &\\
\bottomrule
\end{tabular}
\end{document}

enter image description here

2

If I understand you correctly, than you like to obtain something like this:

enter image description here

The simplest way to obtain above table is not use siunitx. I put table in standard table environment. If you liek to have as sidewaystable, then just add missing package and change table to sidewaystable.

\documentclass[12pt,a4paper,titlepage]{article}
\usepackage{mathtools}
\usepackage{booktabs}
\newcommand\ml[1]{\multicolumn{1}{c}{\text{#1}}}
\newcommand\mc[1]{\multicolumn{2}{c}{\text{#1}}}

\begin{document}
\begin{table}
\[
\begin{array}{@{}c *{3}{r@{\ } l}}
\ml{}           & \mc{Mean}             &           &       &           &       \\
\ml{Lead Time}  & \mc{1}                & \mc{2}            & \mc{3}            \\ 
    \midrule
    0           & (-10,     & 11)       & (-25,     & 116)  & (21,      & 240)  \\
    1           & (11,      & 252)      & (22,      & 139)  & (24,      & 244)  \\
    2           & (63,      & 390)      & (48,      & 21)   & (47,      & 27)   \\
    3           & (66,      & 790)      & (68,      & 24)   & (150,     & 31)   \\
    \bottomrule
\end{array}
\]
\end{table}
\end{document}
  • Thank you very much that's nearly what I'd like to achieve but the parenthesis should also be aligned so that within a column they look the same – FriendlyGuy Nov 22 '16 at 16:12
  • Consider using an array environment instead of a tabular environment. That way, negative numbers will be printed with proper mathematical minus symbols rather than with short text-mode dash symbols. – Mico Nov 22 '16 at 16:16
  • @Mico, again you remember me on array environment ... It seems that I'm not the best student ... :) – Zarko Nov 22 '16 at 16:24

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.