TransWikia.com

Left-aligning column entries with respect to each other while centering them with respect to their respective column margins

TeX - LaTeX Asked on April 26, 2021

Following up my previous question, I need to left-align the entries (below the header Symbol) of the second column of this multi-column table with respect to each other while centering them with respect to their respective column margins without breaking the row colors.

In other words, all the entries of the second column below the header need to virtually have the same width so that they would be left-aligned with each other and centered with respect to their column margins.

enter image description here

PassOptionsToPackage{table}{xcolor}
documentclass{article}
usepackage{siunitx,ragged2e,booktabs,lipsum,adjustbox}
usepackage[usestackEOL]{stackengine}
begin{document}
tabcolsep0pt
rowcolors{2}{gray!20}{}
begin{tabular}{
    >{RaggedRight}m{0.5linewidth}
    >{Centering}m{0.2linewidth}
    S
    s
    }
    toprule
    Parameter                          & Symbol & multicolumn{2}{c}{Value}                         
    midrule
    DC Motor  Rotor Inertia            & $J_M$  & 0.6                       & kgmsquared         
    DC Motor Rotor Damping Coefficient & $D_M$  & 4                         & Nmperradianpers 
    DC Motor Supply Voltage            & $V_ٍ$   & 20                        & V                    
    bottomrule
end{tabular}
end{document}

3 Answers

A solution with eqparbox which defines box commands similar to the standard ones, but use a system of tags, so that all boxes that share the same tag have as width the natural width of the widest of them:

documentclass[table]{article}
usepackage{siunitx,ragged2e,booktabs,lipsum,adjustbox}
usepackage[table]{xcolor}
usepackage{eqparbox}
newcommand{eqmathbox}[2][M]{eqmakebox[#1][l]{$displaystyle#2$}}

begin{document}

tabcolsep0pt
rowcolors{2}{gray!20}{}
begin{tabular}{
    >{RaggedRight}m{0.5linewidth}
    >{Centering}m{0.2linewidth}
    S
    s
    }
    toprule
    Parameter & Symbol & multicolumn{2}{c}{Value} 
    midrule
    DC Motor Rotor Inertia & eqmathbox{J_M} & 0.6 & kgmsquared 
    DC Motor Rotor Damping Coefficient & eqmathbox{D_M} & 4 & Nmperradianpers 
    DC Motor Supply Voltage & eqmathbox{V} & 20 & V 
    bottomrule
end{tabular}

end{document} 

enter image description here

Correct answer by Bernard on April 26, 2021

For this particular job it can be done like this:

    PassOptionsToPackage{table}{xcolor}
documentclass{article}
usepackage{siunitx,ragged2e,booktabs,lipsum,adjustbox}
usepackage[usestackEOL]{stackengine}   
usepackage{calc}
begin{document}
    newlength{vp}
    setlength{vp}{0.1linewidth-widthof{$D_M$}/2}    % the widest element    
    tabcolsep0pt
    rowcolors{2}{gray!20}{}
    begin{tabular}{
            >{RaggedRight}m{0.5linewidth}
            >{Centering}m{0.2linewidth}
            S
            s
        }
        toprule
        Parameter                          & Symbol & multicolumn{2}{c}{Value}                         
        midrule
        DC Motor  Rotor Inertia            & multicolumn{1}{l}{hspace{vp}$J_M$}  & 0.6                       & kgmsquared         
        DC Motor Rotor Damping Coefficient &multicolumn{1}{l}{hspace{vp}$D_M$} & 4                         & Nmperradianpers 
        DC Motor Supply Voltage            & multicolumn{1}{l}{hspace{vp}$V_ٍ$}   & 20                        & V                    
        bottomrule
    end{tabular}
end{document}

out2

Answered by Simon Dispa on April 26, 2021

Edit: Original solution doesn't work as I expected. Later I recognise that centering of symbol values depends from width of column and proposed manual tweaking of center position is not what OP expect. This aspect is better solved in the @Simon Dispa answer (+1). SO below is small variation ih his answer with some off topic suggestion as use of tabularx for table, determine table format for dimension values, aligning dimensions to the left with table-alignment = left optionand wee bit shorter code for table:

documentclass{article}
usepackage[table]{xcolor}
usepackage{ragged2e}
usepackage{booktabs, tabularx}
newcounter{tblerows}% see https://tex.stackexchange.com/questions/297345/
expandafterletcsname c@tblerowsendcsnamerownum

usepackage{siunitx}
usepackage{calc}

begin{document}
centering
renewcommandtabularxcolumn[1]{m{#1}}
renewcommandarraystretch{1.2}
setlengthtabcolsep{0pt}
rowcolors{2}{gray!20}{}
begin{tabularx}{linewidth}{ 
                >{RaggedRight}X
                >{RaggedRighthspace{vp}}m{0.2linewidth}
                S[table-format=3.2]
                s[table-alignment = left]
                             }
    toprule
    Parameter                          & multicolumn{1}{c}{Symbol}
                                                & multicolumn{2}{c}{Value}     
    midrule
    DC Motor  Rotor Inertia            & $J_M$  & 0.6   & kgmsquared         
    DC Motor Rotor Damping Coefficient & $D_M$  & 4     & Nmperradianpers 
    DC Motor Supply Voltage            & $V$    & 20    & V                    
    bottomrule
end{tabularx}
end{document}

enter image description here

However, use of the eqparbox, as propose @Bernard (+1) in his answer, is more robust solution since it not require manual settings of widest symbol in the second column.

Answered by Zarko on April 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP