7

I use siunitx package to display intervals in tabular, according to this answer. But I use parenthesis instead of square bracket. This is a MWE:

%!TeX program = xelatex
%!TeX encoding = UTF-8
\documentclass{article}

\usepackage{siunitx}
\usepackage{pdflscape}
\usepackage{booktabs}

\begin{document}
\begin{landscape}
\begin{table}
\begin{tabular}{
        l*{3}{
        S[table-format=1.3]@{\hspace{0.5em}}
        >{{(}}
        S[table-format=1.3,table-space-text-pre={(}]
        @{,\,}
        S[table-format=1.3,table-space-text-post={)}]
        <{{)}}@{\hspace{2em}}
        }
        S[table-format=1.3]@{\hspace{0.5em}}
        >{{(}}
        S[table-format=1.3,table-space-text-pre={(}]
        @{,\,}
        S[table-format=1.3,table-space-text-post={)}]
        <{{)}}
    }

    \toprule

    {} & \multicolumn{3}{c@{\hspace{2em}}}{$\mathit{Exp}(\lambda)$} & \multicolumn{3}{c@{\hspace{2.5em}}}{$G(2,\lambda)$} & \multicolumn{3}{c@{\hspace{2.5em}}}{$G(3,\lambda)$} & \multicolumn{3}{@{}c@{\hspace{0.5em}}}{$N^+(\mu,\omega^2)$} \\

    \midrule

    $\alpha_0$ & 4.239 & 4.150 & 4.330   & 4.315 & 4.225 & 4.404   & 4.371 & 4.281 & 4.459   & 4.273 & 4.183 & 4.365\cr
    $\alpha_1$ & 0.024 & 0.024 & 0.025   & 0.024 & 0.024 & 0.025   & 0.024 & 0.024 & 0.025   & 0.024 & 0.024 & 0.025\cr
    $\beta_1$ & 0.054 & 0.049 & 0.060   & 0.051 & 0.045 & 0.056   & 0.049 & 0.043 & 0.054   & 0.051 & 0.046 & 0.057\cr
    $\beta_2$ & 0.755 & 0.749 & 0.761   & 0.761 & 0.755 & 0.767   & 0.765 & 0.758 & 0.771   & 0.761 & 0.755 & 0.767\cr
    $\beta_3$ & 0.109 & 0.102 & 0.116   & 0.106 & 0.099 & 0.113   & 0.105 & 0.098 & 0.112      & 0.106 & 0.099 & 0.113\cr
    $\sigma^2$ & 0.032 & 0.031 & 0.033   & 0.031 & 0.030 & 0.032   & 0.030 & 0.030 & 0.031   & 0.031 & 0.030 & 0.032\cr
    $\lambda$ & 1.426 & 1.352 & 1.502   & 2.468 & 2.356 & 2.588   & 3.383 & 3.240 & 3.531     & \multicolumn{3}{@{}c@{\hspace{0.5em}}}{--}\cr
    $\mu$ & \multicolumn{3}{c@{\hspace{2em}}}{--} & \multicolumn{3}{c@{\hspace{2.5em}}}{--} & \multicolumn{3}{c@{\hspace{2.5em}}}{--}    & 0.286 & 0.243 & 0.324\cr
    $\omega^2$ & \multicolumn{3}{c@{\hspace{2em}}}{--} & \multicolumn{3}{c@{\hspace{2.5em}}}{--} & \multicolumn{3}{c@{\hspace{2.5em}}}{--}   & 0.440 & 0.397 & 0.485\cr
    DIC & \multicolumn{3}{c@{\hspace{2em}}}{$-$9687.5} & \multicolumn{3}{c@{\hspace{2.5em}}}{$-$10274.1} & \multicolumn{3}{c@{\hspace{2.5em}}}{$-$10592.1} & \multicolumn{3}{@{}c@{\hspace{0.5em}}}{$-$10173.7}\cr

    \bottomrule
\end{tabular}
\end{table}
\end{landscape}
\end{document}

Actually, it works well on TeX Live 2017 (the version of siunitx is v2.7g). But when I compile it on TeX Live 2019 (the version of siunitx is v2.7s), an error arises, which says invalid numerical input '(4.150'. I'm confused. However, if I use square brackets, the error disappears. So I think the problem is that the parenthese are always considered as opening and closing symbols used for uncertainty input, when only left parenthesis used, the siunitx package decides it is an invalid numerical input. Therefore, I change input-open-uncertainty to [ and input-close-uncertainty to ]

\sisetup{input-open-uncertainty=[,input-close-uncertainty=]}

And it works! So I want to know why a single parenthesis can not be used with a number even if table-space-text-pre is specified. Why does this problem happen in the newer version of siunitx package?

  • The use case for the S column type isn't clear: Your code compiles fine -- and the ouput looks just fine -- if every S column is replaced with c. – Mico Jun 2 at 8:40
  • @Mico Yeah, in this case, S column perhaps is not necessary. Actually, I have quite a few tables, and some of them need S column type. I got this problem incidentally. – North Jun 2 at 9:13
4

You correctly surmised that the opening parenthesis is always treated as marking the start of the uncertainty. This is necessary for siunitx to be able to reliably parse numbers. The option table-space-text-pre does not change this; it only adds the width of its value (( in this case) to the column, the actual token (() is not treated any differently because of this.

You can still achieve your goal without redefining the uncertainty input by hiding the parentheses from the parser. This can be done by putting them inside a group (between { and }). It looks like you already tried this in >{{(}}, but the error message tells you that this group is not there any more when the siunitx parser reads the parenthesis. How can this be?

When TeX consumes the argument of a macro, it strips it of one layer of braces (if present). This means that sometimes, depending on the implementation of the macros you are using, some of the braces you type disappear before they reach the part of the machine they were meant for.

There are two ways of dealing with this.

  • You could add more braces. In your case, one additional layer is enough: >{{{(}}}
  • You can hide your braces from TeX by putting them inside a macro. Define a macro \newcommand*\foo{{(}} and use it like >{\foo}. Now, when this argument is handed through the different macros implementing the table, \foo is still unexpanded. It only gets expanded once the siunitx parser tries to make sense of its input and then it finds {(}, as desired. In general, this approach is much safer and easier than the first one since you don't have to first find out how many layers of braces you need and your code does not break when this number changes.

As to why this does not occur in older versions of siunitx I can only guess. To me, it looks like either a bug in the old versions or a new feature in the new ones. (Being able to add parts of the number before parsing using > and < is definitely a feature, I would say.)

  • Thanks for your explanation. I just tried defining a macro, and it works! This is exactly what I want. – North Jun 2 at 9:41
8

You forgot to add option input-symbols = {( )}. With adding it and cleaning a bit of your code, I obtain:

enter image description here

MWE:

\documentclass{article}

\usepackage{siunitx}
\usepackage{pdflscape}
\usepackage{booktabs}
\newcommand\dsh{\multicolumn{3}{c@{\hspace{2em}}}{--}}
\newcommand\mcc[1]{\multicolumn{3}{@{} c @{\hspace{1em}}}{$#1$}}

\begin{document}
\begin{landscape}
\begin{table}
% \centering 
\sisetup{table-format=1.3,      % <---
         input-symbols = {( )}} % <===
\begin{tabular}{
         >{$}l<{$}
        *{3}{S @{\hspace{0.5em}}
         >{(}S[table-space-text-pre={(}]
        @{,\,}
             S[table-space-text-post={)}]<{)}@{\hspace{2em}} 
                }
        S @{\hspace{0.5em}}
        >{{(}}
        S[table-space-text-pre={(}]
       @{,\,}
        S[table-space-text-post={)}]<{)}
                }
    \toprule
            & \mcc{\mathit{Exp}(\lambda)}   & \mcc{G(2,\lambda)}
            & \mcc{G(3,\lambda)}            & \mcc{N^+(\mu,\omega^2)} \\
    \midrule
\alpha_0    & 4.239 & 4.150 & 4.330   & 4.315 & 4.225 & 4.404
            & 4.371 & 4.281 & 4.459   & 4.273 & 4.183 & 4.365   \cr
\alpha_1    & 0.024 & 0.024 & 0.025   & 0.024 & 0.024 & 0.025
            & 0.024 & 0.024 & 0.025   & 0.024 & 0.024 & 0.025\cr
\beta_1     & 0.054 & 0.049 & 0.060   & 0.051 & 0.045 & 0.056
            & 0.049 & 0.043 & 0.054   & 0.051 & 0.046 & 0.057\cr
\beta_2     & 0.755 & 0.749 & 0.761   & 0.761 & 0.755 & 0.767
            & 0.765 & 0.758 & 0.771   & 0.761 & 0.755 & 0.767\cr
\beta_3     & 0.109 & 0.102 & 0.116   & 0.106 & 0.099 & 0.113
            & 0.105 & 0.098 & 0.112   & 0.106 & 0.099 & 0.113\cr
\sigma^2    & 0.032 & 0.031 & 0.033   & 0.031 & 0.030 & 0.032
            & 0.030 & 0.030 & 0.031   & 0.031 & 0.030 & 0.032\cr
\lambda     & 1.426 & 1.352 & 1.502   & 2.468 & 2.356 & 2.588
            & 3.383 & 3.240 & 3.531   & \dsh\cr
\mu         & \dsh  & \dsh  & \dsh    & 0.286 & 0.243 & 0.324\cr
\omega^2    & \dsh  & \dsh  & \dsh    & 0.440 & 0.397 & 0.485\cr
\omega^2    & \multicolumn{3}{c@{\hspace{2em}}}{--}
            & \multicolumn{3}{c@{\hspace{2.5em}}}{--}
            & \multicolumn{3}{c@{\hspace{2.5em}}}{--}
                                      & 0.440 & 0.397 & 0.485\cr
\mathrm{DIC}& \mcc{-9687.5} & \mcc{-10274.1}  & \mcc{-10592.1}
            & \mcc{-10173.7}\cr
    \bottomrule
\end{tabular}
\end{table}
\end{landscape}
\end{document}

edit: corrected column specification.

  • Thanks! Actually, I have seen this question. It's a good solution, but the problem is it produces bad boxes. – North Jun 2 at 9:23
6

Your code doesn't seem to require the siunitx package and its S column type. The basic c column type would appear to perform just as well.

Another suggestion: use an array environment rather than a tabular environment; this change will save you from having to type lots of $ symbols.

In addition to replacing S with c in the following example, I cleaned up and simplified your code a bit.

enter image description here

\documentclass{article}
\usepackage{array,amsmath,pdflscape,booktabs}

\begin{document}
\begin{landscape}
\begin{table}
\centering
\setlength\arraycolsep{1em}
$\begin{array}{@{}
        l 
        *{4}{ c 
              @{\hspace{1em}}
              >{(}c
              @{,\,}
              c<{)} }
        @{}}

    \toprule

    & \multicolumn{3}{c}{\exp(\lambda)} 
    & \multicolumn{3}{c}{G(2,\lambda)} 
    & \multicolumn{3}{c}{G(3,\lambda)} 
    & \multicolumn{3}{c@{}}{N^+(\mu,\omega^2)} \\

    \midrule

    \alpha_0 & 4.239 & 4.150 & 4.330   & 4.315 & 4.225 & 4.404   
       & 4.371 & 4.281 & 4.459   & 4.273 & 4.183 & 4.365\\
    \alpha_1 & 0.024 & 0.024 & 0.025   & 0.024 & 0.024 & 0.025   
       & 0.024 & 0.024 & 0.025   & 0.024 & 0.024 & 0.025\\
    \beta_1  & 0.054 & 0.049 & 0.060   & 0.051 & 0.045 & 0.056   
       & 0.049 & 0.043 & 0.054   & 0.051 & 0.046 & 0.057\\
    \beta_2  & 0.755 & 0.749 & 0.761   & 0.761 & 0.755 & 0.767   
       & 0.765 & 0.758 & 0.771   & 0.761 & 0.755 & 0.767\\
    \beta_3  & 0.109 & 0.102 & 0.116   & 0.106 & 0.099 & 0.113   
       & 0.105 & 0.098 & 0.112   & 0.106 & 0.099 & 0.113\\
    \sigma^2 & 0.032 & 0.031 & 0.033   & 0.031 & 0.030 & 0.032   
       & 0.030 & 0.030 & 0.031   & 0.031 & 0.030 & 0.032\\
    \lambda  & 1.426 & 1.352 & 1.502   & 2.468 & 2.356 & 2.588   
       & 3.383 & 3.240 & 3.531   & \multicolumn{3}{c@{}}{\text{--}}\\

    \mu 
    & \multicolumn{3}{c}{\text{--}} 
    & \multicolumn{3}{c}{\text{--}} 
    & \multicolumn{3}{c}{\text{--}}    
    & 0.286 & 0.243 & 0.324\\
    \omega^2 
    & \multicolumn{3}{c}{\text{--}} 
    & \multicolumn{3}{c}{\text{--}} 
    & \multicolumn{3}{c}{\text{--}}   
    & 0.440 & 0.397 & 0.485\\
    \mathrm{DIC}
    & \multicolumn{3}{c}{-9687.5} 
    & \multicolumn{3}{c}{-10274.1} 
    & \multicolumn{3}{c}{-10592.1} 
    & \multicolumn{3}{c@{}}{-10173.7}\\

    \bottomrule
\end{array}$
\end{table}
\end{landscape}
\end{document}

Addendum: If you do need to use the S column type, all you'd have to change in the code above is (a) load the siunitx package and (b) replace

$\begin{array}{@{}
        l 
        *{4}{ c 
              @{\hspace{1em}}
              >{(}c
              @{,\,}
              c<{)} }
        @{}}

with

\sisetup{input-symbols = {()}, % inform siunitx that "(" and ")" have no special meaning
         table-format=1.3}
$\begin{array}{@{}
        l 
        *{4}{ S 
              @{\hspace{1em}}
              >{(}S
              @{,\,}
              S<{)} }
        @{}}
  • 1
    Thank you Mico. This result looks good, I think I should use the basic c type in some of my tables. – North Jun 2 at 9:34

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.