0

I am trying to specify a custom column command, taking a width parameter and aligning the column by the decimal point of the content.


I am using the tabularx package to use columns with a pre-defined width

p{<width>}

The width is corrected by table margins with my defined \COLW{<width>} command (see Table layout with tabularx (column widths: 50%|25%|25%) for reference)

What I am trying to achieve is a columend aligned along the decimal dot, which is provided by the 'S' column option of the siunitx. But this does not allow to specify the column width. I found the array package providing the \centerdots and \endcenterdots commands to define this behavior manually with a custom column type 'd' like

\newcolumntype{d}[1]{>{\centerdots\arraybackslash}p{#1}<{\endcenterdots}} 

But somehow these commands are not properly recoginzed

undefined control sequence [...] \centerdots

Any ideas how to combine dot-aligned columns with explicit width specifications?


Minimal working example

\documentclass[a4paper,11pt]{report}

\usepackage{array}      %should provide \centerdots command!?
\usepackage{tabularx}   %tabularx environment with fixed size columns
\usepackage{ragged2e}   %Provide \RaggedLeft and \RaggedRight commands
\usepackage{siunitx}    %Add 'S' option in tables to align along decimal point

\newcommand{\COLW}[1]{\dimexpr#1\textwidth-2\tabcolsep-1.3333\arrayrulewidth} %defined column width with margin correction
\newcolumntype{R}[1]{>{\RaggedLeft\arraybackslash}p{#1}} %right-aligned column with given column width
\newcolumntype{L}[1]{>{\RaggedRight\arraybackslash}p{#1}} %left-aligned column with given column width
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}} %centered column with given column width
\newcolumntype{d}[1]{>{\centerdots\arraybackslash}p{#1}<{\endcenterdots}} %dot-aligned column with given column width

\begin{document}

\begin{table}[hbtp!]
  \centering
  %this setup uses the 'S' column from siunitx package which properly centeres the content along the dot, BUT does not allow for column width specification
  \begin{tabularx}{1.0\textwidth}{|L{\COLW{0.25}}|C{\COLW{0.25}}|R{\COLW{0.25}}|d{\COLW{0.25}}|}
  %this setup uses \centerdots command from array package and defined column width, but doesn't work
  %\begin{tabularx}{1.0\textwidth}{|L{\COLW{0.25}}|C{\COLW{0.25}}|R{\COLW{0.25}}|S|}
    \hline
    left-aligned & centered & right-aligned & \multicolumn{1}{c|}{dot-aligned} \\ %multicolumn as workaround for non decimal number conflicting with 'S' option
    \hline 
    1 & 1 & 1 & 100.0   \\
    2 & 2 & 2 &  10.0   \\
    3 & 3 & 3 &   1.0   \\
    4 & 4 & 4 &   0.1   \\
    5 & 5 & 5 &   0.01  \\
    6 & 6 & 6 &   0.001 \\
    \hline  
  \end{tabularx}    
\end{table}

\end{document}
  • That is correct, but the standard tabular environment does not allow to specify the overall tabular width, which I would like to stretch to full \textwidth, hence I switched to tabularx. – HeXor Mar 10 '16 at 12:10
  • On contrary : I do have some tables with more complex content, looking a bit packed with the dynamic width determination of the standard tabular environment, not using the fulll (or 90%) textwidth. So I now confirmed for myself that I can just use the standard tabular environment without specifying the full tabular width, but just defining N columns with textwidth/N columnwidth to achieve my desired behavior, which brings me back to the problem of the 'S' option not providing a width parameter option. – HeXor Mar 10 '16 at 12:21
  • But this way I do not have an explicit control on the tabular width in terms of fraction of the full textwidth. I am editing a larger document, trying to follow the convention to use a common style with 0.9\textwidth tables, so I would like to have the possibility to define the column width as fraction of textwidth adding up to 0.9. This should be possible by manually using the \centerdots command as shown in my MWE, but I am not able to make this work – HeXor Mar 10 '16 at 12:31
  • updated answer with .9\textwidth table – David Carlisle Mar 10 '16 at 12:38
  • This helps to reach my needs, thank you! For curiosity, how would I be able to define a custom column using the \centerdots option as it is proposed in the array package manual ftp.tu-chemnitz.de/pub/tex/macros/latex/required/tools/… with \newcolumntype{d}{>{\centerdots}c<{\endcenterdots}} – HeXor Mar 10 '16 at 12:50
2

You can not use tabularx unless you specify at least one X column to allow it to stretch that column to achieve the stated width.

Here I would just use siunitx (or dcolumn) and control the column widths by specifying (larger) formats for the numbers if that is required, for example the following where two of the columns have been forced wider.

enter image description here

\documentclass[a4paper,11pt]{report}

\usepackage{array}      %should provide \centerdots command!?

\usepackage{ragged2e}   %Provide \RaggedLeft and \RaggedRight commands
\usepackage{siunitx}    %Add 'S' option in tables to align along decimal point

\begin{document}

\begin{table}[hbtp!]
  \centering
  \begin{tabular}{SSS[table-format=6.2]S[table-format=4.4]}
    \hline 
    1 & 1 & 1 & 100.0   \\
    2 & 2 & 2 &  10.0   \\
    3 & 3 & 3 &   1.0   \\
    4 & 4 & 4 &   0.1   \\
    5 & 5 & 5 &   0.01  \\
    6 & 6 & 6 &   0.001 \\
    \hline  
  \end{tabular} 

\bigskip

or to a specified width

\bigskip

  \begin{tabular*}{.9\textwidth}{
@{\extracolsep{\fill}}
SSS[table-format=6.2]S[table-format=4.4]
@{}}
    \hline 
    1 & 1 & 1 & 100.0   \\
    2 & 2 & 2 &  10.0   \\
    3 & 3 & 3 &   1.0   \\
    4 & 4 & 4 &   0.1   \\
    5 & 5 & 5 &   0.01  \\
    6 & 6 & 6 &   0.001 \\
    \hline  
  \end{tabular*} 

\end{table}

\end{document}
  • Thank you! I adapted this a little bit to work with my column definitions \begin{tabular*}{0.9\textwidth}{|L{\COLW{0.225}}|C{\COLW{0.225}}|R{\COLW{0.225}}|@{\extracolsep{\fill}}S|@{}} – HeXor Mar 10 '16 at 12:49

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.