2

For this MWE, how to make D_M virtually have the same width of K_{emf} in order to be left-aligned with K_{emf} while keeping both centered with respect to the column margins?

Generally speaking, how to do it for any number of rows (below the header) without having to manually detect the widest entry of the respective column?

enter image description here

\documentclass{article}
\usepackage{siunitx,ragged2e,booktabs,lipsum}
\begin{document}
    \tabcolsep0pt
    \begin{tabular}{
            >{\Centering}m{0.2\linewidth}
        }
        \toprule
        Symbol\\
        \midrule
        $D_M$\\
        $K_{emf}$\\
        \bottomrule
    \end{tabular}
\end{document}
Diaa
  • 9,599

3 Answers3

3

enter image description here

\documentclass{article}
\usepackage{array,booktabs}
\begin{document}
    \tabcolsep0pt
    \begin{tabular}{
            >{\quad}l<{\quad}
        }
        \toprule
        \multicolumn{1}{c}{Symbol}\\
        \midrule
        $D_M$\\
        $K_{\mathit{emf}}$\\
        \bottomrule
    \end{tabular}
\end{document}
Bernard
  • 271,350
David Carlisle
  • 757,742
  • 1
    I like how simple your answers are. No need to burn the house to kill the spider :) – Diaa Dec 11 '20 at 01:48
  • For the m column used in my question, this >{\hspace{2ex}\RaggedRight}m{0.2\linewidth}<{\hspace{2ex}} doesn't work. May I know why? – Diaa Dec 11 '20 at 11:02
  • adding hspace at the start and end of the text in a paragraph has no effect on the width of the entry as the text is line broken to the specified width, I don't see why you would use m here, there is no linebreaking and m loses baseline alignment with other columns. @Diaa – David Carlisle Dec 11 '20 at 12:33
  • I need to have vertical centering of the cell content. I would be grateful to propose an approach for such a result. – Diaa Dec 11 '20 at 13:03
  • 1
    @Diaa Bernard's answer uses m, so you could move the tick to the other answer if you prefer (I will cope:-) the whole point of this answer is not to use m, using vertically centred cells with subscripted variables will produce potentially very uneven results with the text not following any baseline why do you want that? – David Carlisle Dec 11 '20 at 15:49
  • I believe you answered my question to help me not to gain virtual internet credit :) so that's why I ask to further understand not argue. – Diaa Dec 11 '20 at 16:00
  • @DavidCarlisle: I thought I was posting a complement to my answer, but didn't see first I was editing your answer, so I had to re-edit it to copy-paste at the right place. Quite sorry. Won't do it again! – Bernard Dec 11 '20 at 17:11
  • @Bernard no worries: I have done that to other people's posts in the past as well, it's easily done:-) – David Carlisle Dec 11 '20 at 17:54
  • A quick question if you don't mind: for tabularx, does this, for example, >{\hspace{2ex}\RaggedRight\hsize=1.6\hsize\linewidth=\hsize}X<{\hspace{2ex}} mean that an additional space of 2ex will be added to the column width after it is calculated by \hsize? Or it will behave like m column of explicit width as you explained earlier! – Diaa Dec 11 '20 at 23:31
  • @Diaa the hspace make no sense in an m or X column, they will not affect the column width or the content as space at the start of a paragraph is discarded. – David Carlisle Dec 12 '20 at 00:04
  • I hope your patience is not over yet :) but what is the difference between wrapping the subscript in \mathit or leaving it as I did? Visually speaking, I think both look the same for me. – Diaa Dec 12 '20 at 10:29
  • @Diaa it looks completely different look at the vast ocean of white space between the m and f in your image compared to the one here. \mathit selects the text italic font, for use in math so is a font with spacing designed for words. the default font is math italic which is a completely different font with slightly different letter shapes and very different inter-letter spacing. Compare $diff$ and $\mathit{diff}$ – David Carlisle Dec 12 '20 at 11:02
1

You can easily do that with the stackengine package:

\documentclass{article}
\usepackage{siunitx,ragged2e,booktabs,lipsum}
\usepackage[usestackEOL]{stackengine}

\begin{document}

\tabcolsep0pt
\begin{tabular}{
        &gt;{\Centering}m{0.2\linewidth}
    }
    \toprule
    Symbol\\
    \midrule
\Centerstack[l]{ $D_M$\\
    $K_{emf}$}\\
    \bottomrule
\end{tabular}

\end{document}

enter image description here

To have also coloured rows, you can use this other solution, based on eqparbox add this code to your preamble:

\usepackage{eqparbox} 
\newcommand{\eqmathbox}[2][M]{\eqmakebox[#1][l]{$\displaystyle#2$}}

and this for the tabular:

    \begin{tabular}{
            >{\Centering}m{0.2\linewidth}
        }
        \toprule
        Symbol\\
        \midrule
        \eqmathbox{D_M}\\
        \rowcolor{yellow}\eqmathbox{K_{emf}}\\
        \bottomrule
    \end{tabular}

enter image description here

Bernard
  • 271,350
  • I know it is not stated in the question but if I have this problem https://pastebin.com/zzg2G9gS, how can I make $K_{emf}$ inside \Centerstack be placed in the second row and colored yellow by the \rowcolors? – Diaa Dec 11 '20 at 16:40
  • Actually, with \Centerstack, rows 2 and 3 are combined within a single row. I'll post in a moment another possibility with eqparbox, if I've well undertood what you want. – Bernard Dec 11 '20 at 16:43
  • Many thanks for your consideration to answer a follow-up question in the comments. You can find it here https://tex.stackexchange.com/q/574630/2288 as a separate clear question to avoid any confusion. – Diaa Dec 11 '20 at 19:56
0

Inspired by this answer, the following also works using adjustbox:

enter image description here

\documentclass{article}
\usepackage{siunitx,ragged2e,booktabs,lipsum,adjustbox}
\begin{document}
    \tabcolsep0pt
    \begin{tabular}{
            >{\Centering}m{0.2\linewidth}
        }
        \toprule
        Symbol\\
        \midrule
        \adjustbox{tabular = l}{%
            $D_M$\\
            $K_{emf}$\\
        }\\
        \bottomrule
    \end{tabular}
\end{document}
Diaa
  • 9,599
  • Off topic: (i) more correct is $K_{\mathrm{emf}}$ (e, m and f are not variables, but abbreviation, I assume), (ii) please, can you show the use of the \rowcolor in your solution? – Zarko Dec 11 '20 at 17:55
  • @Zarko I just came here to make the same comment when yours appeared:-), or \mathit as in my answer, Diaa that f is like a poke in the eye:-) never use math italic for a multi-letter word, it is designed to separate the letters so they are not a word but a product of 1-letter variables. – David Carlisle Dec 11 '20 at 17:57
  • @DavidCarlisle, you are right! – Zarko Dec 11 '20 at 18:03
  • For rowcolors, assume I have this document https://pastebin.com/ntL5a7Mv, then I need to left-align the symbols while centering them with respect to the column margins. I should have asked this as a separate question, but I tried to test Bernard generosity first :) I took your note about the subscripts @DavidCarlisle – Diaa Dec 11 '20 at 18:41
  • @Zarko I posted my question here https://tex.stackexchange.com/q/574630/2288. – Diaa Dec 11 '20 at 19:56