3

I want a table column to be aligned using the thousands comma. But when I try to put a number in parentheses I get an error.

I have read previous answers which were not suitable for my situation, either because they answered a situation where the whole column needed parentheses, or because they wrote input-symbols = {()}, which then counts the parentheses as digits.

MWE:

\documentclass{beamer}
\usepackage[group-separator={,}]{siunitx}

\begin{document}
\begin{frame}
    \begin{table}
        \begin{tabular}{S[table-format=5.0]S[table-format = 1.1]}
            12000             & 1.4 \\
            (14000) & 1.2 
        \end{tabular}
    \end{table}
\end{frame}
\end{document}

enter image description here

1 Answer 1

5

Edit: Upon further review, there is a better option. siunitx provides the table-space-text-pre and table-space-text-post options for precisely this use:

\documentclass{article}

\usepackage[
        group-separator={,},
    ]{siunitx}

\begin{document}

\begin{tabular}{S[table-format=5.0, table-space-text-pre={(}, table-space-text-post={)}] S[table-format=1.1]}
        12000   &   1.4 \\
        {(} 14000 {)}   &   1.2 
\end{tabular}

\end{document}

MWE output


[original answer]

The easiest possibility is probably placing the parentheses in the table margins using \llap and \rlap. We can hide them from the siunitx parser by placing them inside a group.

I also included a possibility where the parentheses' width is not ignored by actually placing them in the column margin.

I would however prefer the first method, mostly due to clarity. If you need the width of the parentheses, you can manually add it to the margin.

\documentclass{article}

\usepackage[
        group-separator={,},
    ]{siunitx}

% only needed for second version:
\let\beforecell\relax
\let\aftercell\relax
\def\beforeparencell{%
    (%
    \gdef\aftercell{%
        )%
        \global\let\beforecell\relax
        \global\let\aftercell\relax
    }%
}

\begin{document}

\begin{tabular}{S[table-format=5.0] S[table-format=1.1]}
        12000   &   1.4 \\
        {\llap{(}}14000{\rlap{)}}   &   1.2 
\end{tabular}

\begin{tabular}{@{\hspace{\tabcolsep}\beforecell} S[table-format=5.0] @{\aftercell\hspace{2\tabcolsep}} S[table-format = 1.1]}
    12000   &   1.4 {\global\let\beforecell\beforeparencell}\\
    14000   &   1.2 
\end{tabular}

\begin{tabular}{@{\hspace{\tabcolsep}\hphantom{(}} S[table-format=5.0] @{\hphantom{)}\hspace{2\tabcolsep}} S[table-format=1.1]}
        12000   &   1.4 \\
        {\llap{(}}14000{\rlap{)}}   &   1.2 
\end{tabular}

\end{document}

MWE output

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .