Dot-aligned table column with defined width

1,685

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}
Share:
1,685

Related videos on Youtube

HeXor
Author by

HeXor

Updated on March 11, 2020

Comments

  • HeXor
    HeXor over 3 years

    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}
    
    • HeXor
      HeXor over 7 years
      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
      HeXor over 7 years
      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
      HeXor over 7 years
      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
    • David Carlisle
      David Carlisle over 7 years
      updated answer with .9\textwidth table
    • HeXor
      HeXor over 7 years
      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}}
    • David Carlisle
      David Carlisle over 7 years
      I don't see why \centerdots wouldn't work as >{\centering\centerdots}p{3cm}<\endcenterdots if you copied the definitions from the array package documentation (they are only given as example definitions in the doc, not actually defined in the package), as it notes there similar but improved versions are in my dcolumn package (but there again I assume c rather tahn p columns.
    • HeXor
      HeXor over 7 years
      In my case this command is unkown when using the array package. Do you know where it is defined?
    • David Carlisle
      David Carlisle over 7 years
      as I say it is not defined anywhere. the definition is in the text of the documentation just at the point you are reading, the code block at the bottom of page 2 is the definition you just need to copy from there into your document
  • HeXor
    HeXor over 7 years
    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.2‌​25}}|R{\COLW{0.225}}‌​|@{\extracolsep{\fil‌​l}}S|@{}}