How to center cell contents of a latex table with different text lengths?

4,258

The culprit are the \rule commands that serve no purpose. I also removed the \noalign{\smallskip} bits, whose only effect is to disconnect the rules.

\documentclass{article}

\usepackage{array}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}

\begin{document}

\begin{table*}
\caption{caption goes here}
\label{tab:1}       % Give a unique label    
\centering

\begin{tabular}{|M{1.5cm}|M{2cm}|M{7cm}|M{1.5cm}|M{2cm}|}
\hline
\multicolumn{3}{c}{text} & \multicolumn{2}{c}{text text text}\\
\hline
text & text text & text text text text & text text  & text text text\\
\hline
text & text & text text text text text text text text text text text text. text text text text agricultural areas around text text text text & text & text \\
text & text & text text text text text text text text text text text text. text text text text agricultural areas around text text text text & text & text \\
text & text & text text text text text text text text text text text text. text text text text agricultural areas around text text text text & text & text\\
text & text & text text text text text text text text text text text text. text text text text agricultural areas around text text text text & text & text\\
\hline
\end{tabular}

\end{table*}

\end{document}

enter image description here

You may like to see a different realization:

\documentclass{article}

\usepackage{array,booktabs}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}

\begin{document}

\begin{table*}
\caption{caption goes here}
\label{tab:1}       % Give a unique label    
\centering

\begin{tabular}{M{1.5cm}M{2cm}M{7cm}M{1.5cm}M{2cm}}
\toprule
\multicolumn{3}{c}{text} & \multicolumn{2}{c}{text text text}\\
\cmidrule(lr){1-3} \cmidrule(lr){4-5}
text & text text & text text text text & text text  & text text text\\
\midrule
text & text & text text text text text text text text text text text text. text text text text agricultural areas around text text text text & text & text \\
\addlinespace
text & text & text text text text text text text text text text text text. text text text text agricultural areas around text text text text & text & text \\
\addlinespace
text & text & text text text text text text text text text text text text. text text text text agricultural areas around text text text text & text & text\\
\addlinespace
text & text & text text text text text text text text text text text text. text text text text agricultural areas around text text text text & text & text\\
\bottomrule
\end{tabular}

\end{table*}

\end{document}

enter image description here

Share:
4,258

Related videos on Youtube

Gago-Silva
Author by

Gago-Silva

trying to survive a phd

Updated on August 10, 2020

Comments

  • Gago-Silva
    Gago-Silva over 3 years

    I am using the array package to center vertically the text (see code bellow). The problem is that the text in the first and second column are not align with the first column. how can i fix this?

    \usepackage{array}
    \newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
    \newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
    
    \begin{table*}
    \caption{caption goes here}
    \label{tab:1}       % Give a unique label    
    \begin{center}
    \begin{tabular}{|M{1.5cm}|M{2cm}|M{7cm}|M{1.5cm}|M{2cm}|}
    \hline\noalign{\smallskip}
    \multicolumn{3}{c}{text} & \multicolumn{2}{c}{text text text}\\
    \noalign{\smallskip}\hline\noalign{\smallskip}   
    text & text text& text text text text & text text  & text text text\\
    \noalign{\smallskip}\hline\noalign{\smallskip}
    text & text & {text text text text text text text text text text text text. text text text text agricultural areas around text text text text} & text & text& text\\
    \rule{0pt}{5ex}
    text & text & {text text text text text text text text text text text text. text text text text agricultural areas around text text text text} & text & & text& text & text\\
    \rule{0pt}{5ex}
    text & text & {text text text text text text text text text text text text. text text text text agricultural areas around text text text text} & text & text\\
    \rule{0pt}{5ex}
    text & text & {text text text text text text text text text text text text. text text text text agricultural areas around text text text text} & text & text& text\\
    \noalign{\smallskip}\hline
    \end{tabular}
    \end{center}
    \end{table*}
    

    enter image description here

    • egreg
      egreg over 6 years
      What's the purpose of \noalign{\smallskip} and of the \rule commands?