3

I am trying to construct some tables using the package siunitx. As you can see in the example below, the number in column D are nicely aligned in the first three rows (after the header). However, the last number (46.57%) in the last row is not aligned anymore. I somehow guess that is related to the usage of \itshape (I followed the advice giving here).

Do you have any idea how to correctly align the figures in the last row using an italic font shape?

enter image description here

MWE:

\documentclass[a4paper,11pt]{scrbook}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}

\usepackage{siunitx}
 \sisetup{
        group-digits=true,       
        group-minimum-digits=4,
    add-integer-zero=false,
    }

\begin{document}

\begin{table}[htbp]
  \begin{center}
  \caption{Some Table}
  \begin{tabular}{c
          c
          S[table-format = 2.0]
          S[table-format = 2.2]}
    \toprule
    A & B & {C} & {D} \\ \midrule
    1 & 24 & 12 & 50.00\% \\ 
    2 & 22 & 4 & 18.18\% \\ 
    3 & 85 & 45 & 52.94\% \\ \midrule
    \emph{Total} & \emph{131} & {\itshape 61} &  {\itshape 46.57\%}\\
    \bottomrule
  \end{tabular}
  \end{center}
\end{table}

\end{document}

EDIT:

New MWE based on the comments and answers:

\documentclass{article}
\usepackage{booktabs}
\usepackage{etoolbox}
\robustify\itshape

\usepackage{siunitx}
 \sisetup{
        group-digits=true,       
        group-minimum-digits=4,
    add-integer-zero=false,
    }
\begin{document}
\begin{table}
  \centering
  \sisetup{detect-all = true, table-space-text-post = \%}
  \begin{tabular}{@{}
          c
          c
          S[table-format = 2.0]
          S[table-format = 2.2]
          @{}}
    \toprule
    A & B & {C} & {D}  \\ \midrule
    1 & 24 & 12 & 50.00\% \\ 
    2 & 22 & 4 & 18.18\% \\ 
    3 & 85 & 45 & 52.94\% \\ \midrule
    \emph{Total} & \itshape 131 & \itshape 61 &  \itshape 46.57\% \\
    \bottomrule
  \end{tabular}
\end{table}

\end{document}

I guess I am missing something important here. However, this table (in particular column C and D) does not look right. The numbers in column C are shifted to the left. In addition, the percentage sign is still to close to the right column border. Any suggestions? (Sorry for the detailed and stupid-sounding questions. However, this table starts to steal my sanity.)

enter image description here

5
  • See tex.stackexchange.com/questions/66253/… (\itshape vs \bfseries is the only real difference)
    – Joseph Wright
    Sep 25, 2016 at 10:20
  • Thanks. I tried using \usepackage{etoolbox} \robustify\itshape. However, this did not change anything. Maybe I did not really understand the solution given in the link you just posted.
    – phx
    Sep 25, 2016 at 10:24
  • 1
    You also need to allow siunitx to detect shape (the standard settings force upright): I'd probably detect-all here.
    – Joseph Wright
    Sep 25, 2016 at 10:25
  • Alright. However, I added detect-all and later also detect-weight=true and detect-inline-weight=math to my \sisetup. Still no changes...
    – phx
    Sep 25, 2016 at 10:29
  • 1
    Don't make the option table-space-text-post = \% global, i.e., apply to columns C and D. Make it apply only column D.
    – Mico
    Sep 25, 2016 at 12:25

2 Answers 2

4

The approach is much the same as in siunitx: Bold single numeric cells: make \itshape robust, allow siunitx to find it at the start of a cell, allow siunitx to pick an appropriate font.

Version 3

For v3 (current release), the code you'd want is

\documentclass{article}
\usepackage{booktabs}
\usepackage{etoolbox}
\robustify\itshape

\usepackage{siunitx}
 \sisetup{
        group-digits=all,       
        group-minimum-digits=4
    }
\begin{document}
\begin{table}
  \centering
  \sisetup{mode = text, reset-text-shape = false}
  \begin{tabular}{@{}
          c
          c
          S[table-format = 2.0]
          S[table-format = 2.2]
          @{}}
    \toprule
    A & B & {C} & {D/\%} \\ \midrule
    1 & 24 & 12 & 50.00 \\ 
    2 & 22 & 4 & 18.18 \\ 
    3 & 85 & 45 & 52.94 \\ \midrule
    \emph{Total} & \itshape 131 & \itshape 61 &  \itshape 46.57\\
    \bottomrule
  \end{tabular}
\end{table}
\end{document}

If you want units in each row (which is generally not recommended though it is commonly done), then you need to allow for the space in the appropriate column(s):

\documentclass{article}
\usepackage{booktabs}
\usepackage{etoolbox}
\robustify\itshape

\usepackage{siunitx}
 \sisetup{
        group-digits=true,       
        group-minimum-digits=4,
    }
\begin{document}
\begin{table}
  \centering
  \sisetup{mode = text, reset-text-shape = false}
  \begin{tabular}{@{}
          c
          c
          S[table-format = 2.0]
          S[table-format = 2.2\,\%]
          @{}}
    \toprule
    A & B & {C} & {D} \\ \midrule
    1 & 24 & 12 & 50.00\,\% \\ 
    2 & 22 & 4 & 18.18\,\% \\ 
    3 & 85 & 45 & 52.94\,\% \\ \midrule
    \emph{Total} & \itshape 131 & \itshape 61 &  \itshape 46.57\,\%\\
    \bottomrule
  \end{tabular}
\end{table}
\end{document}

Version 2

With the older version 2 (which you can test if you like with \usepackage{siunitx}[=v2]}, for example

\documentclass{article}
\usepackage{booktabs}
\usepackage{etoolbox}
\robustify\itshape

\usepackage{siunitx}
 \sisetup{
        group-digits=true,       
        group-minimum-digits=4,
    add-integer-zero=false,
    }
\begin{document}
\begin{table}
  \centering
  \sisetup{detect-all = true}
  \begin{tabular}{@{}
          c
          c
          S[table-format = 2.0]
          S[table-format = 2.2]
          @{}}
    \toprule
    A & B & {C} & {D/\%} \\ \midrule
    1 & 24 & 12 & 50.00 \\ 
    2 & 22 & 4 & 18.18 \\ 
    3 & 85 & 45 & 52.94 \\ \midrule
    \emph{Total} & \itshape 131 & \itshape 61 &  \itshape 46.57\\
    \bottomrule
  \end{tabular}
\end{table}
\end{document}

You can use a similar approach for the units-on-each-line, but there you'll need S[table-format = 2.2, table-space-text-post = \,\%] as v2 can't pick up the post-numerical part from table-format.

0
0

For anyone having trouble with this: when I tried Joseph's answer I just didn't get any italics shown, the font was all upright. However, this did not happen with the \bfseries command, only the \itshape command.

What solved it for me was changing \usepackage{siunitx} with \usepackage{siunitx-v2}. According to the log, this changes from v3.0.37 to v2.8e. It's kind of a pain because, for example, this old version of siunitx apparently cannot deal with scientific notation values (i.e. 2.3e4) and I had to change those.

There is a closed issue in siunitx's tracker about a similar problem, but it supposed to be fixed. I'll try to report this.

You must log in to answer this question.

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