[Tex/LaTex] Vertically align first multiline row of table

tablesvertical alignment

I'm using IEEEtran style for a two column document. I want my table cells to be aligned center both horizontally and vertically. First row has to be multiline with some columns having more lines than rest, but my code does not center align it horizontally.

\begin{tabular}{|c|c|c|c|}
\hline
\multicolumn{1}{|p{0.2\columnwidth}|}{\centering {\bfseries a few rows of text}} & \multicolumn{1}{p{0.2\columnwidth}|}{\centering {\bfseries a few rows of text}} & \multicolumn{1}{p{0.2\columnwidth}|}{\centering {\bfseries a few rows of text}} & \multicolumn{1}{p{0.2\columnwidth}|}{\centering {\bfseries a few more more rows of text}}\\
\hline
Three & Four&5&6\\
\hline
\end{tabular}

Which produces this:

result

that clearly first row is not vertically aligned in center.

Is there an easy fix for this?

Best Answer

There are a number of ways to achieve this. Either use array's m{<len>} column (instead of p{<len>}) which places the anchor at the vertical centre automatically. Or, use makecell to stack the content manually (which also provides a default vertical centre alignment:

enter image description here

\documentclass{IEEEtran}

\usepackage{makecell,array}

\begin{document}

\begin{tabular}{|c|c|c|c|}
  \hline
  \multicolumn{1}{|p{0.2\columnwidth}|}{\centering\bfseries a few rows of text} 
    & \multicolumn{1}{p{0.2\columnwidth}|}{\centering\bfseries a few rows of text}
    & \multicolumn{1}{p{0.2\columnwidth}|}{\centering\bfseries a few rows of text}
    & \multicolumn{1}{p{0.2\columnwidth}|}{\centering\bfseries a few more more rows of text} \\
  \hline
  Three & Four& 5 & 6 \\
  \hline
\end{tabular}

\bigskip

\begin{tabular}{|c|c|c|c|}
  \hline
  \multicolumn{1}{|m{0.2\columnwidth}|}{\centering\bfseries a few rows of text} 
    & \multicolumn{1}{m{0.2\columnwidth}|}{\centering\bfseries a few rows of text}
    & \multicolumn{1}{m{0.2\columnwidth}|}{\centering\bfseries a few rows of text}
    & \multicolumn{1}{m{0.2\columnwidth}|}{\centering\bfseries a few more more rows of text} \\
  \hline
  Three & Four& 5 & 6 \\
  \hline
\end{tabular}

\bigskip

\begin{tabular}{|c|c|c|c|}
  \hline
  \multicolumn{1}{|p{0.2\columnwidth}|}{\bfseries \makecell{a few rows \\ of text}}
    & \multicolumn{1}{p{0.2\columnwidth}|}{\bfseries \makecell{a few rows \\ of text}}
    & \multicolumn{1}{p{0.2\columnwidth}|}{\bfseries \makecell{a few rows \\ of text}}
    & \multicolumn{1}{p{0.2\columnwidth}|}{\bfseries \makecell{a few more \\ more rows \\ of text}} \\
  \hline
  Three & Four& 5 & 6 \\
  \hline
\end{tabular}

\end{document}