Tuesday, May 26, 2015

Latex Tables: How to Center Text Both Horizontally and Vertically

The normal approach is to use ">{\centering\arraybackslash}m{3cm}" to specify cell format at the top:
\begin{table}
\centering\begin{tabular}{|>{\centering\arraybackslash}m{3cm}|>{\centering\arraybackslash}m{5cm}|} \hline\textbf{Topic} & \textbf{Paragraph} \\\hline \hlineTopic 1 & This is a paragraph. This is a paragraph. This is a paragraph.\\\hline\end{tabular} \end{table}
The text is both vertically and horizontally centered, but, you probably want the Paragraph column to be left aligned. If you use "p" to specify the cell format for the second column, the text will no longer be vertically centered:

\begin{table} \centering\begin{tabular}{|>{\centering\arraybackslash}m{3cm}|p{5cm}|} \hline\textbf{Topic} & \textbf{Paragraph} \\\hline \hlineTopic 1 & This is a paragraph. This is a paragraph.\\\hline\end{tabular} \end{table}
To have a paragraph style text and vertical alignment both at the same time, use  ">{\centering\arraybackslash}m{3cm}" to specify cell format at the top and use \multicolumn{1}{m}{Text} in the table entry:
\begin{table} \centering\begin{tabular}{|>{\centering\arraybackslash}m{3cm}|m{5cm}|} \hline\textbf{Topic} & \textbf{Paragraph} \\\hline \hlineTopic 1 & This is a paragraph. This is a paragraph.\\\hline\end{tabular} \end{table}
Enjoy!
 






4 comments: