5

I use the package siunitx to print tables. In the following table, I would like to have a different rounding precision and number of printed decimal places for each row.

\usepackage{siunitx}
\sisetup{   
        round-mode      = places,
        round-precision = 3,
        group-separator = {,},
        }
\begin{tabular}{l*{2}{S}}
        & A         & B     \\
Row 1   & 3.22222   & 5.66666   \\
Row 2   & 1.44444   & 9.99999   \\
\end{tabular}

For instance, I would like three decimal places to be printed in row 1, and one decimal place in row 2.

Is there a way to adjust the rounding precision from row 1 to row 2 as well as the number of decimal places to be printed?

2

This answer describes a way to define a common style for single rows in a tabular environment. Starting from that, I defined a command to set the precision for a single row:

  • The columntype P (for: precision) is needed as a first "column". It sets the precision back to the default value at the start of every row.
  • The columntype ^ has to be put before every column where this precision shall be applied. It calls \sisetup in every cell of these columns.
  • Then, the macro \rowprecision can be used at the start of a tabular row to set the precision to the value given as single argument.

Your example:

\documentclass{article}

\usepackage{siunitx}
\usepackage{xifthen}
\sisetup{round-mode      = places,
         round-precision = 3,
         group-separator = {,}}
\usepackage{array}
\newcolumntype{P}{>{\global\let\currentprecision\relax}}
\newcolumntype{^}{>{\ifthenelse{\isequivalentto{\currentprecision}{\relax}}{}{\currentprecision}}}
\newcommand{\rowprecision}[1]{\gdef\currentprecision{\sisetup{round-precision=#1}}}

\begin{document}

\begin{tabular}{Pl*{2}{^S}}
    & A & B \\
    \rowprecision{4} Row 1 & 3.22222 & 5.66666   \\
    \rowprecision{2} Row 2 & 1.44444 & 9.99999   \\
\end{tabular}

\end{document}

enter image description here

  • If I add \usepackage{hyperref} to the preamble, this example produces an error "Argument of __siunitx_table_collect_not_braced:N has an extra }. & A &." How to solve this? – Fang Jing Jul 18 '18 at 19:40
  • @FangJing: I don't know what is the problem there. I suggest you ask this as a separate question. – Tiuri Jul 19 '18 at 12:33

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.