2

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.5\linewidth}
    >{\Centering}m{0.2\linewidth}
    S
    s
    }
    \toprule
    Parameter                          & Symbol & \multicolumn{2}{c}{Value}                         \\
    \midrule
    DC Motor  Rotor Inertia            & $J_M$  & 0.6                       & \kg\m\squared         \\
    DC Motor Rotor Damping Coefficient & $D_M$  & 4                         & \N\m\per\radian\per\s \\
    DC Motor Supply Voltage            & $V_ٍ$   & 20                        & \V                    \\
    \bottomrule
\end{tabular}
\end{document}

3 Answers 3

4

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.5\linewidth}
    >{\Centering}m{0.2\linewidth}
    S
    s
    }
    \toprule
    Parameter & Symbol & \multicolumn{2}{c}{Value} \\
    \midrule
    DC Motor Rotor Inertia & \eqmathbox{J_M} & 0.6 & \kg\m\squared \\
    DC Motor Rotor Damping Coefficient & \eqmathbox{D_M} & 4 & \N\m\per\radian\per\s \\
    DC Motor Supply Voltage & \eqmathbox{V} & 20 & \V \\
    \bottomrule
\end{tabular}

\end{document} 

enter image description here

2
  • So clean that my eyes hurt. Have a nice day/night :)
    – Diaa
    Dec 11, 2020 at 20:19
  • 1
    It's rather night here… It's a very useful package for alignment problem. Note that it requires usually two compilations. , `stackengine is not required here (albeit it's also a useful package).
    – Bernard
    Dec 11, 2020 at 20:23
3

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.1\linewidth-\widthof{$D_M$}/2}    % the widest element    
    \tabcolsep0pt
    \rowcolors{2}{gray!20}{}
    \begin{tabular}{
            >{\RaggedRight}m{0.5\linewidth}
            >{\Centering}m{0.2\linewidth}
            S
            s
        }
        \toprule
        Parameter                          & Symbol & \multicolumn{2}{c}{Value}                         \\
        \midrule
        DC Motor  Rotor Inertia            & \multicolumn{1}{l}{\hspace{\vp}$J_M$}  & 0.6                       & \kg\m\squared         \\
        DC Motor Rotor Damping Coefficient &\multicolumn{1}{l}{\hspace{\vp}$D_M$} & 4                         & \N\m\per\radian\per\s \\
        DC Motor Supply Voltage            & \multicolumn{1}{l}{\hspace{\vp}$V_ٍ$}   & 20                        & \V                    \\
        \bottomrule
    \end{tabular}
\end{document}

out2

3
  • Thanks for your answer but when I look closely at the output, i feel like, for example, D_M (as the widest column entry) is not centered or horizontally aligned with "Symbol". It is a bit right shifted from the cell center.
    – Diaa
    Dec 11, 2020 at 20:11
  • You are right. I updated the answer to provide a more exact center. I sometimes prefer to do a visual adjustment because a mathematical one not always looks the best. Dec 11, 2020 at 20:45
  • Your update is great, however, it would be nice to have such a robust approach of finding the widest entry instead of manually judging the widest one by my eyes.
    – Diaa
    Dec 11, 2020 at 21:11
2

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/
\expandafter\let\csname c@tblerows\endcsname\rownum

\usepackage{siunitx}
usepackage{calc}

\begin{document}
\centering
\renewcommand\tabularxcolumn[1]{m{#1}}
\renewcommand\arraystretch{1.2}
\setlength\tabcolsep{0pt}
\rowcolors{2}{gray!20}{}
\begin{tabularx}{\linewidth}{ 
                >{\RaggedRight}X
                >{\RaggedRight\hspace{\vp}}m{0.2\linewidth}
                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   & \kg\m\squared         \\
    DC Motor Rotor Damping Coefficient & $D_M$  & 4     & \N\m\per\radian\per\s \\
    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.

1
  • As an active OCD member, I feel like the center of "D_M" is a bit shifted to the right from the center of the word "Symbol" :)
    – Diaa
    Dec 11, 2020 at 20:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .