5

I'm using the tabu package for my tables and this comes with the X column type. I defined an R column type (\newcolumntype{R}{>{\raggedleft\arraybackslash}X}) based on X which I now use in my table to right align all the data.

To fit inside of the page, I need to shrink the table a bit, so I wanted to place the units in a new line. This is possible in X cells by typing \newline.

This all works well, but now the first line (the descriptions) looks a bit weird, as there is no real alignment. I think it should be aligned to the center. So my question is how I could change the default setting of R to C for just the first row.

MWE

\documentclass{article}

\usepackage[table]{xcolor}
\usepackage{tabu}
\usepackage{siunitx}

\newcolumntype{R}{>{\raggedleft\arraybackslash}X}

\begin{document}

\begin{table}[htb!]
    \caption{Table 1}

    \centering
    \taburowcolors[1] {white .. gray!10}
    \begin{tabu}[t]{| R R R R R R R |}
        \hline
        $c$ \newline [\si{mol\per\liter}]
            & $V_\text{Tensid}$ \newline [\si{\milli\liter}]
            & $V_\text{Wasser}$ \newline [\si{\milli\liter}]
            & $\Pi_\text{1. Durchgang}$ \newline [\si{\milli\newton\per\meter}]
            & Offset \newline [\si{\milli\newton\per\meter}]
            & $\Pi_\text{2. Durchgang}$ \newline [\si{\milli\newton\per\meter}]
            & Offset \newline [\si{\milli\newton\per\meter}]\\
        \hline
        0.04 & 60 & 0 & 22.6 & 0.7 & - & -\\
        $\vdots$ & $\vdots$ & $\vdots$ & $\vdots$ & $\vdots$ & $\vdots$ & $\vdots$\\
        0.004 & 60 & 30 & 21.6 & 0.6 & 20.7 & 1.3\\
        0.002 & 60 & 60 & 21.0 & 0.3 & 22.1 & 1.4\\
        0.001 & 60 & 60 & 20.9 & 0.2 & 24.3 & 0.7\\
        \hline
    \end{tabu}
\end{table}

\end{docum

ent}

6

Instead of tabu and its flexible width columns, I would use the S columns from the siunitx package to align the numbers with resprect to the decimal separator. In order to fit the table onto the textwidth I have reduced the horizontal distance between columns (tabcolsep). As one can see from the MWE, the column headers are automatically horizontally centered.

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{siunitx}

\begin{document}

\begin{table}[htb!]
\setlength{\tabcolsep}{4pt}
    \caption{Table 1}
    \centering
    \rowcolors{2}{gray!10}{white}
    \begin{tabular}[t]{| S[table-format=1.3] S[table-format=2] S[table-format=2] S[table-format=2.1] S[table-format=2.1] S[table-format=2.1] S[table-format=1.1] |}
        \hline
              {$c$}
            & {$V_\text{Tensid}$} 
            & {$V_\text{Wasser}$}
            & {$\Pi_\text{1. Durchgang}$}
            & {Offset}
            & {$\Pi_\text{2. Durchgang}$}
            & {Offset}\\
              {[\si{mol\per\liter}]}
            & {[\si{\milli\liter}]}
            & {[\si{\milli\liter}]}
            & {[\si{\milli\newton\per\meter}]}
            & {[\si{\milli\newton\per\meter}]}
            & {[\si{\milli\newton\per\meter}]}
            & {[\si{\milli\newton\per\meter}]}\\
        \hline
        0.04 & 60 & 0 & 22.6 & 0.7 & {-} & {-}\\
        {$\vdots$} & {$\vdots$} & {$\vdots$} & {$\vdots$} & {$\vdots$} & {$\vdots$} & {$\vdots$}\\
        0.004 & 60 & 30 & 21.6 & 0.6 & 20.7 & 1.3\\
        0.002 & 60 & 60 & 21.0 & 0.3 & 22.1 & 1.4\\
        0.001 & 60 & 60 & 20.9 & 0.2 & 24.3 & 0.7\\
        \hline
    \end{tabular}
\end{table}

\end{document}

enter image description here

If you want to stick to tabu and right aligned numbers, you might be interested in using the \makecell command from the eponymous package.

  • Your idea with the S column is great. I didn't know that one! But why shouldn't I use it inside of tabu? It works well :) – Sam May 12 '18 at 18:06
  • @Sam: One typically uses tabu in combination with its X columns that enable automated line breaking of long texts in table cells. As there are no long texts in the table, using tabu for this particular table simply is not necessary. – leandriis May 13 '18 at 7:15
3

It seems like I am a bit late for this answer but still here is my input.

Beatiful typesetting, but not so elegant code. I used multicolumn to cheat the aligment. It also requires some manual tuning of the column width, which can be a pain if you're in a hurry or have too many columns.

I included the threeparttable because it's a easy way to add table notes. The booktabs package is what allows this table to look so much better (IMO) without extra custom command definitions etc.

table with booktabs and wrap text inside multicolumn

Note how the \vdots get shifted to the decimal alignment. As pointed by leandriis this is due to the missing table-format option on the S. Which consistis of two numbers, the first the integer and second is the decimal. So to make it center alignment I did this:

\begin{tabular}{S[table-format=1.3]S[table-format=2.0]S[table-format=2.0]S[table-format=2.1]S[table-format=1.1]S[table-format=2.1]S[table-format=1.1]}

S with table-format

    \documentclass{article}
\usepackage[table]{xcolor}
\usepackage{siunitx}
\usepackage{booktabs, threeparttable}
\setlength{\aboverulesep}{0pt}
\setlength{\belowrulesep}{0pt}
\setlength{\extrarowheight}{.75ex}
\begin{document}
\begin{table}
\begin{threeparttable}
\caption{Some Science Experiment}\label{table:experiment1}
\rowcolors{1}{white}{gray!10}
\begin{tabular}{S[table-format=1.3]S[table-format=2.0]S[table-format=2.0]S[table-format=2.1]S[table-format=1.1]S[table-format=2.1]S[table-format=1.1]} %use l if you want numbers left aligned
\toprule
 \multicolumn{1}{p{3em\relax}}{\centering $c$ [\si{mol\per\liter}]} 
& \multicolumn{1}{p{2.5em\relax}}{\centering $V_\text{Tensid}$ [\si{\milli\liter}]}
& \multicolumn{1}{p{3em\relax}}{\centering $V_\text{Wasser}$ [\si{\milli\liter}]}
& \multicolumn{1}{p{5em\relax}}{\centering $\Pi_\text{1. Durchgang}$  [\si{\milli\newton\per\meter}]}
& \multicolumn{1}{p{4em\relax}}{\centering Offset  [\si{\milli\newton\per\meter}]}
& \multicolumn{1}{p{5em\relax}}{\centering $\Pi_\text{2. Durchgang}$  [\si{\milli\newton\per\meter}]}
& \multicolumn{1}{p{4em\relax}}{\centering Offset [\si{\milli\newton\per\meter}]}
  \\
\midrule
        0.04 & 60 & 0 & 22.6 & 0.7 & {-} & {-}\\
        {$\vdots$} & {$\vdots$} & {$\vdots$} & {$\vdots$} & {$\vdots$} & {$\vdots$} & {$\vdots$}\\
        0.004 & 60 & 30 & 21.6 & 0.6 & 20.7 & 1.3\\
        0.002 & 60 & 60 & 21.0 & 0.3 & 22.1 & 1.4\\
        0.001 & 60 & 60 & 20.9 & 0.2 & 24.3 & 0.7\\
\bottomrule
\end{tabular}
\begin{tablenotes}
\item  Source or other notes.
\end{tablenotes}
\end{threeparttable}
\end{table}
\end{document}
  • 1
    The shift of the vdots that you observe seems due to the usage of S type columns without the usage of the [table-format] option. – leandriis May 13 '18 at 7:20

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.