0

I am using the package longtable and what I want to achieve is to have vertically centered cells, and equal row height.

I have tried using m{} column type, to achieve the vertical centering, but when I try to increase row height using arraystretch, the vertical alignmnent fails. So I need help in two matters

  1. Why does arraystretch mess with the vertical alignment? How else could the functionality of arraystretch be achieved and keep the alignment?
  2. Arraystretch does not give me equal row height but it only increases padding. How can I have equal row height?

Here is the output, you can see the difference in the arrow lengths:

enter image description here

MWE:

\documentclass{article}

\usepackage[table,dvipsnames]{xcolor}%table colors
\usepackage{array}
\usepackage{makecell}
\usepackage{longtable}
\setlength{\doublerulesep}{0pt}
\newcommand{\myhline}{\hline\hline\hline}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}m{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}m{#1}}

\begin{document}
\begin{footnotesize} 
\renewcommand{\arraystretch}{3}
  \rowcolors{2}{MidnightBlue!10}{white}
  \begin{longtable}{ | C{1.7in} | C{1.5in} | C{0.65in} | C{0.6in} | C{0.6in} | }
 \hline
\rowcolor{MidnightBlue!30}
\textbf{test1} & \textbf{test2} &  \textbf{test3} & \textbf{test4} & \textbf{test5} \\
  \myhline
  \textbf{qwerty} &  testing simple & - & + & + \\
  \textbf{qwerty2} & testing simple long test & - & + & -\\
  \textbf{qwerty} & testing simple long test & - & + & + \\
  \textbf{qwerty} & testing simple long test & - & + & + \\
  \textbf{qwerty} & testing simple long test & - &+ & + \\
  \textbf{qwerty} & testing reaaaaaaaaaaaally simple loooooooooooooong test & - & + & + \\
  \textbf{qwerty} & testing simple long test & - & + & + \\
  \textbf{qwerty} & testing simple long test (pixels) & - & + & + \\
  \textbf{qwerty} & testing simple long test & - & + & + \\
  \hline
 \rowcolor{white} 
 \caption{Table testing} 
\label{my_label}
 \end{longtable}

\end{footnotesize}

\end{document}
2
  • Will you add more rows to your table later? Is longtable or a different package, that can break tables across pages mandatory?
    – leandriis
    May 27 at 17:53
  • Yes, I will. It is only for the the example that I didn't include all the rows.
    – NickG
    May 27 at 18:01
1

enter image description here

(red lines indicate page layout)

Using cellspace (instead using of arraystretch)and xltabular (instead of longtable):

\documentclass{article}
\usepackage[skip=1ex]{caption}
\usepackage[table,dvipsnames]{xcolor}%table colors 
\usepackage{array, makecell, tabularx, xltabular}
    \newcolumntype{C}[1]{>{\centering\arraybackslash\hsize=#1\hsize}X}
    \newcolumntype{L}[1]{>{\raggedright\arraybackslash\hsize=#1\hsize}X}
    \newcolumntype{R}[1]{>{\raggedleft\arraybackslash\hsize=#1\hsize}X}
\usepackage[column=O]{cellspace}
    \setlength\cellspacetoplimit{5pt}
    \setlength\cellspacebottomlimit{5pt}
    \addparagraphcolumntypes{C}
%
%---------------- Show page layout. Don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage{lipsum}% For dummy text. Don't use in a real document

\begin{document}
{\footnotesize
 \centering
 \setlength\columnsep{4pt}
 \renewcommand\tabularxcolumn[1]{m{#1}}
 \rowcolors{2}{MidnightBlue!10}{white}
    \begin{tabularx}{\linewidth}{ |>{\bfseries}O{C{1.75}}|O{C{1.75}}|
                                               O{C{0.50}}|O{C{0.50}}|O{C{0.50}}| }
     \Xhline{1pt}
test 1  & \textbf{test 2}   
            & \textbf{test 3}   & \textbf{test 4}   & \textbf{test 5}   \\     
     \Xhline{0.5pt}
qwerty  & testing simple            & - & + & + \\
qwerty  & testing simple long test  & - & + & -\\
qwerty  & testing simple long test  & - & + & + \\
qwerty  & testing simple long test  & - & + & + \\
qwerty  & testing simple long test  & - & + & + \\
qwerty  & testing reaaaaaaaaaaaally simple loooooooooooooong test 
                                    & - & + & + \\
qwerty  & testing simple long test  & - & + & + \\
qwerty  & testing simple long test (pixels) 
                                    & - & + & + \\
qwerty  & testing simple long test  & - & + & + \\
     \Xhline{1pt}
    \end{tabularx}
\captionof{table}{Table testing}
\label{my_label}
}

\end{document}

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.