4

How to use siunitx and tabularx together? works great if you want every X column to be interpreted by siunitx, however, what if I want that for just a few tables? So, is it possible to have the S[opt] column behavior with automatic width?

I'm struggling with the optional arguments part of the S column, so far this is what I've got.

MWE

\documentclass{article}
\usepackage{array,tabularx,siunitx,booktabs}
\usepackage[utf8]{inputenc}

% Allow `_` and `:` in macro names (LaTeX3 style)
\catcode`\_=11
\catcode`\:=11
% Internal code of `S`
\newcolumntype{Y}[1]{% << mandatory argument (how to make it optional?)
    >{\__siunitx_table_collect_begin:Nn S{#1} }%
    X%
    <{\__siunitx_table_print:}%
}
\catcode`\_=8
\catcode`\:=12
\begin{document}

    \begin{table}[h]
        \centering
        \caption{Some table}
        \begin{tabularx}{\textwidth}{Y{}Y{}Y{}}
            \toprule
            {Tempo (min)} & {Fase A (\%v/v)} & {Fase B (\%v/v)} \\ 
            \midrule
               0,01     &       100      &    0\\
                15      &       80       &    20\\
                25      &       80       &    20\\
                30      &       100      &    0\\
            \bottomrule
        \end{tabularx}
    \end{table}

\end{document}

If the answer is "No that's not possible" it's ok, but it would be nice to have it running perfectly. :D

Someone will ask why use tabularx for numeric columns, easy explanation: crazy advisor requirement. If it was up to me... :)

3 Answers3

6

You want tabular*, not tabularx. But the result is wrong anyway, as you can clearly see from the image below.

\documentclass{article}
\usepackage{array,siunitx,booktabs}
\usepackage[utf8]{inputenc}

\begin{document}

\begin{table}[htp]
\centering
\caption{Some table}\label{label}

\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}SSS@{}}
\toprule
{Tempo (\si{min})} & {Fase A ($\%v/v$)} & {Fase B ($\%v/v$)} \\ 
\midrule
 0,01 & 100 &  0\\
15    &  80 & 20\\
25    &  80 & 20\\
30    & 100 &  0\\
\bottomrule
\end{tabular*}

\end{table}

\begin{table}[htp]
\centering
\caption{Some table}\label{label2}

\begin{tabular}{@{}SSS@{}}
\toprule
{Tempo (\si{min})} & {Fase A ($\%v/v$)} & {Fase B ($\%v/v$)} \\ 
\midrule
 0,01 & 100 &  0\\
15    &  80 & 20\\
25    &  80 & 20\\
30    & 100 &  0\\
\bottomrule
\end{tabular}

\end{table}

\end{document}

enter image description here

egreg
  • 1,121,712
5

To spread the columns out to textwidth you want tabular* not tabularx but this should really be a non-aim, as it just makes the table harder to read.

enter image description here

this also makes it easy to use the S optional argument as optional

\documentclass{article}
\usepackage{array,tabularx,siunitx,booktabs}
\usepackage[utf8]{inputenc}

\begin{document}

    \begin{table}[htp]%never just [h]
        \centering
        \caption{Some table}
        \begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}SSS@{}}
            \toprule
            {Tempo (min)} & {Fase A (\%v/v)} & {Fase B (\%v/v)} \\ 
            \midrule
               0,01     &       100      &    0\\
                15      &       80       &    20\\
                25      &       80       &    20\\
                30      &       100      &    0\\
            \bottomrule
        \end{tabular*}
    \end{table}

\end{document}
David Carlisle
  • 757,742
  • I agree that what I ask is ugly, no need to make further comments on that :). But, what's truly wanted is to have the columns like in Zarko's answer (with all columns spreaded evenly, like if you had {XXX}), is that possible with tabular* I didn't know this environment at all... – Guilherme Zanotelli Dec 06 '16 at 07:22
  • Also, I always had the impression tabularx was written for having a table, which has long text cells, set to a specific width. Sometimes there's just no long text cells, but I still have to set the table to a width... :) Of course I had no idea of this tabular* then. – Guilherme Zanotelli Dec 06 '16 at 07:22
5

If you really like to have tabularx:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs,tabularx}
\newcommand\mcx[1]{\multicolumn{1}{>{\centering\arraybackslash}X}{#1}}
\usepackage{siunitx}

\begin{document}

\begin{table}[htp]%never just [h]
    \centering
    \caption{Some table}
    \begin{tabularx}{\textwidth}{*{3}{S[table-format=3.2]}}
        \toprule
    \mcx{Tempo (min)} & \mcx{Fase A (\%v/v)} & \mcx{Fase B (\%v/v)} \\
        \midrule
     0,01   &       100      &    0\\
    15      &       80       &    20\\
    25      &       80       &    20\\
    30      &       100      &    0\\
        \bottomrule
    \end{tabularx}
\end{table}

\end{document}

but result is (to my opinion) far worse than obtained in other answers. It can be slightly improved, if you reduce table width, but why then you make such complication if this can be done automatically without tabularx?

enter image description here

Zarko
  • 296,517