3

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?

1

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}
1

I can not figured out why you put column headers in multicolum with p type of column` and than horizontally center content again. Do you like to determine the table width? If no, than see if the following solution gives what you like to have:

enter image description here

\documentclass{IEEEtran}
\usepackage{makecell}
\renewcommand\theadfont{\normalsize\bfseries}% <-- defined fonts for column heads

\usepackage{lipsum}% for dummy text

\begin{document}
\lipsum[2]
    \begin{table}[htb]
    \centering
\caption{my table}
    \label{tab:very-important-table}
\setlength\extrarowheight{2pt}
\begin{tabular}{|c|c|c|c|}
  \hline
\thead{a few rows\\ of text}
        &   \thead{a few rows\\ of text}
                    &   \thead{a few rows\\ of text}
                            &   \thead{a few more\\ more rows\\ of text} \\
  \hline
Three   &   Four    &   5   &   6   \\
  \hline
\end{tabular}
    \end{table}
\lipsum
\end{document}

If yes, than the package tabularx can help:

\documentclass{IEEEtran}
\usepackage{makecell,tabularx} % <-- added tabularx
\renewcommand\theadfont{\normalsize\bfseries}
\newcolumntype {C}{>{\centering\arraybackslash}X}% <-- defined new column type
\usepackage{lipsum}% for dummy text

\begin{document}
\lipsum[2]
    \begin{table}[htb]
    \setlength\extrarowheight{2pt}
    \centering
\caption{my table}
    \label{tab:very-important-table}
\begin{tabularx}{\columnwidth}{|C|C|C|C|}% determined table width 
  \hline
\thead{a few rows\\ of text}
        &   \thead{a few rows\\ of text}
                    &   \thead{a few rows\\ of text}
                            &   \thead{a few more\\ more rows\\ of text} \\
  \hline
Three   &   Four    &   5   &   6   \\
  \hline
\end{tabularx}
    \end{table}
\lipsum
\end{document}

enter image description here

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.