2

As you can see on the image, the left column is justifying its content hence add uneven spaces between the content there. I am trying to keep the left column content vertically in the centre and horizontally leftwards.

enter image description here

I tried adding \begin{tabular}{>{\RaggedRight}m{3.7cm} m{5cm} to the following script but it did not work.

\documentclass{article}
\usepackage{array}
\usepackage{booktabs}
\begin{document}
\begin{table}
\begin{tabular}{m{3.7cm}| m{5cm}}
\toprule
Sample1                            & Sample2      \\  \midrule
AB\_CDEFG and AB\_CDE       & This is sample text.This is sample text. This is sample text. This is sample text. This is sample text.  This is sample text.This is sample text. This is sample text. This is sample text. This is sample text.\\ \midrule
AB\_CD, AB\_CD,AB\_CD and AB\_CD   &  This is sample text.This is sample text. This is sample text. This is sample text. This is sample text. This is sample text.This is sample text. This is sample text. This is sample text. This is sample text.\\ \bottomrule
\end{tabular}
\label{table:referent-table}
\end{table}
\end{document}

Appreciate suggestions on how this can be achieved.

Mad
  • 143
  • 4
  • How or where is \tbl defined? – Mico Jan 12 '23 at 17:10
  • Please add all the packages necessary to compile your code fragment. For example booktabs... – samcarter_is_at_topanswers.xyz Jan 12 '23 at 17:20
  • I updated the question. It should compile now. – Mad Jan 12 '23 at 17:33
  • 1
    @Mico I removed \tbl from the question. It was overriding some of the commands in booktabs package like \midrule – Mad Jan 12 '23 at 17:39
  • 4
    See https://tex.stackexchange.com/q/338009/82917. That question is about p columns but the answer is the same for m columns: use >{\raggedright\arraybackslash}m{...}. – campa Jan 12 '23 at 17:41
  • 3
    Unrelated: booktabs does not like vertical lines in columns, as you can see ;-) – campa Jan 12 '23 at 17:43
  • @campa I actually tired this >{\raggedright\arraybackslash}m{...} but with uppercase R, i.e. \RaggedRight. Thank you for pointing this. It works. If you can add this as an answer, otherwise I will remove the question if this is duplicating the other one. – Mad Jan 12 '23 at 17:54
  • @campa I don't have a strong preference to use m{...} as far as I can left align and centre vertically. When I tried {>{\raggedright\arraybackslash}p{3.5cm} m{5.0cm}} it works but the content is not vertically centred but {>{\raggedright\arraybackslash}m{3.5cm} m{5.0cm}} works well. So I don't think the question you linked directly answer my question but it does help indeed. – Mad Jan 13 '23 at 04:03

1 Answers1

3

You could load the ragged2e package and use its \RaggedRight instruction to achieve your formatting objective. While you're at it, you might also apply \RaggedRight to the second column.

Do note that \RaggedRight permits hyphenation of words in the cells. If you wish to suppress hyphenation as well, just change \RaggedRight to \raggedright\arraybackslash.

enter image description here

Aside: I can't help but admire the typographic river that flows streight through the right-hand cells. :-)

\documentclass{article}
\usepackage{array,booktabs,ragged2e}
\begin{document}

\begin{table} \caption{Example of a table\strut} \label{table:referent-table}

\centering % <-- don't forget this instruction

\begin{tabular}{@{} >{\RaggedRight}m{3.7cm} >{\RaggedRight}m{5.0cm} @{}} \toprule Sample1 & Sample2 \
\midrule AB_CDE, AB_CDE and AB_CDE
& This is sample text. This is sample text. This is sample text. This is sample text. This is sample text. This is sample text. This is sample text. This is sample text. This is sample text. This is sample text. \ \midrule AB_CD, AB_CD, AB_CD and AB_CD
& This is sample text. This is sample text. This is sample text. This is sample text. This is sample text. This is sample text. This is sample text. This is sample text. This is sample text. This is sample text. \ \bottomrule \end{tabular} \end{table}

\end{document}

Mico
  • 506,678
  • 1
    Sorry for the typographic river, I thought it is easier to use it to show the vertical centre alignment I needed. And I liked that you have added the \caption and \label which I have removed from my question snippet. :-) – Mad Jan 13 '23 at 04:06