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... :)

  • 1
    please don't use tabularx for tables of data. it is all about line breaking . When I see it used with unbreakable numbers I feel sad for having written it:-) – David Carlisle Dec 5 '16 at 22:16
  • 4
    print out the above comment and stick it under your advisor's door:-) – David Carlisle Dec 5 '16 at 22:17
  • @DavidCarlisle, it would be my pleasure. But, very fortunately, it's not my advisor! ;) – Guilherme Zanotelli Dec 6 '16 at 6:53
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

  • I refer you to my comment under the question! – David Carlisle Dec 5 '16 at 23:33
  • Nice thinking Zarko. @DavidCarlisle, unfortunately that's exactly what's wanted... Please don't be sad, some people have either very little care for typography or extremely bad taste, and this time, these people are in charge... :( – Guilherme Zanotelli Dec 6 '16 at 6:51
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

  • The result is not wrong... It's just tremendously ugly and a little stupid... :) – Guilherme Zanotelli Dec 6 '16 at 7:23
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}
  • 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 6 '16 at 7: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 6 '16 at 7:22

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.