1

I have a table which cells can have one or more lines of text. I want to center all cell contents vertically, while horizontally making text left aligned. So far I have come up with this code:

\documentclass[final,10pt]{book}
\usepackage{longtable,booktabs}
\usepackage{array} % needed for "m" in longtable
\usepackage[paperwidth=9in,paperheight=3in,right=0.5in,bottom=0in,left=0.2in]{geometry}
\begin{document}
\thispagestyle{empty}
\noindent\begin{longtable}{@{} m{.30\paperwidth} m{.30\paperwidth} m{.30\paperwidth}@{}}
\toprule\addlinespace
\textbf{Header 1} & \textbf{Header 2} & \textbf{Header 3}
\\\addlinespace
\toprule\endhead
1. Lorem ipsum dolor sit amet (c1, r1) & This is second column (c2, r1), line one\newline This is second column (c2, r1), line two & This is third column (c3, r1), line one\newline This is third column (c3, r1), line two
\\\addlinespace\hline
2. Lorem ipsum dolor sit amet (c1, r2) & This is second column (c2, r2), line one\newline This is second column (c2, r2), line two & This is third column (c3, r2), line one\newline This is third column (c3, r2), line two
\\\addlinespace\hline
3. Lorem ipsum dolor sit amet (c1, r3) & c2, r3 & c3, r3
\\\addlinespace\hline
4. Lorem ipsum dolor sit amet (c1, r4) & c2, r4, line one \newline c2, r4, line two & c3, r4, line one\newline c3, r4, line two
\\\addlinespace
\bottomrule
\end{longtable}
All text in the cells should be centered vertically and left aligned horizontally.
\end{document}

which produces this:

enter image description here

Only header row contents is properly centered vertically. All other cells are not centered vertically and they differ in how much padding is at the top of the cell.

Why are those cells not centered as intended? Isn't it what m (from array package) in longtable definition is supposed to do? How can I make sure that text in each cell is centered vertically, regardless of how many lines of text is in it?

  • 1
    your image shows you are getting vertical alignment in all cells. Draw a line through the vertical centre of each entry and you'll see they line up. (m is nothing to do with the space above and below the row it is about the alignment of entries with other entries in the same row. – David Carlisle Oct 7 '15 at 6:40
  • 1
    The reason you have more space below the single line entries than above is that you added extra space with \addlinespace – David Carlisle Oct 7 '15 at 6:42
  • Instead of \addlinespace\hline, use \midrule. – Mico Oct 7 '15 at 6:51
1

Instead of manually inserting \addlinespace directives in lots of places, you may want to increase the values of \aboverulesep and \belowrulesep via \addtolength instructions. Separately, do consider using \midrule instead of \hline.

enter image description here

\documentclass[final,10pt]{book}
\usepackage{booktabs}
\addtolength\aboverulesep{0.15ex}  % extra spacing above and below rules
\addtolength\belowrulesep{0.35ex}

\usepackage{longtable, array} % "array" is needed for "m" in longtable

\usepackage[paperwidth=9in,paperheight=3in,right=0.5in,bottom=0in,left=0.2in]{geometry}

\begin{document}
\thispagestyle{empty}

\begin{longtable}{@{} *{3}{m{.30\paperwidth}} @{}}

\toprule
\textbf{Header 1} & \textbf{Header 2} & \textbf{Header 3} \\
\midrule[\heavyrulewidth] % make this rule as thick as \toprule
\endhead

\bottomrule
\endfoot

1. Lorem ipsum dolor sit amet (c1, r1) & This is second column (c2, r1), line one\newline This is second column (c2, r1), line two & This is third column (c3, r1), line one\newline This is third column (c3, r1), line two \\
\midrule
2. Lorem ipsum dolor sit amet (c1, r2) & This is second column (c2, r2), line one\newline This is second column (c2, r2), line two & This is third column (c3, r2), line one\newline This is third column (c3, r2), line two \\
\midrule
3. Lorem ipsum dolor sit amet (c1, r3) & c2, r3 & c3, r3 \\
\midrule
4. Lorem ipsum dolor sit amet (c1, r4) & c2, r4, line one \newline c2, r4, line two & c3, r4, line one\newline c3, r4, line two \\

\end{longtable}
\end{document}
  • What is advantage of using \midrule over \hline? – Rafal Oct 7 '15 at 8:07
  • 1
    @Rafal - \midrule automatically inserts whitespace -- in the amounts of \aboverulsep and \belowrulesep, resp. -- above and below the rule, whereas \hline does not. – Mico Oct 7 '15 at 8:54
  • That's a nice solution but (1) now I have white spaces if I use \rowcolor{} and (2) vertical lines do not connect ... there are white spaces in-between too. Any suggestions? thanks! – Jonas Oct 8 '18 at 10:04
  • @Jonas - My answer does not feature any vertical lines (by design!), and it also doesn't feature any \rowcolor directives. I'm afraid my psychic abilities are utterly worthless, and my divination skills are equally poor. My deepest apologies. Try though I might, I simply have no idea how your code differs from mine, let alone what your ultimate typesetting objectives may be. Hence, I'm unable to make any constructive suggestions regarding your code. Very sorry. – Mico Oct 8 '18 at 13:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.