Graphics, Figures & Tableshow to nicely wrap and center text in table cell?

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
crobar
Posts: 12
Joined: Mon Nov 16, 2009 4:41 pm

how to nicely wrap and center text in table cell?

Post by crobar »

Hello,

Can anyone tell me how to horizontally center the wrapped text in the second header cell of the table in the following example without screwing up the row height or otherwise making it look ugly? I just can't figure it out. In general, is it possible wrap text in one table cell, center it vertically and horizontally in the cell, and also have text in the other cells of the same row vertically centred as well?

Code: Select all


\documentclass{article}

\usepackage{booktabs}

\begin{document}

\begin{table}
		\begin{tabular}{c c p{6cm}} \toprule 
			\multicolumn{1}{c}{Name} 
			& \multicolumn{1}{p{2.3cm}}{Variable Form In Code} 
			& \multicolumn{1}{c}{Meaning} \\ 
			\midrule
			x	&	\verb|x|	&	Thickness of the back iron on part 1 \\
		  x	&	\verb|x|	&	Thickness of the magnet on part 1 \\
			\bottomrule
		\end{tabular}
\end{table} 

\end{document}
Thanks!

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
Stefan Kottwitz
Site Admin
Posts: 10321
Joined: Mon Mar 10, 2008 9:44 pm

how to nicely wrap and center text in table cell?

Post by Stefan Kottwitz »

Hi crobar,

you could use m-columns with the array package and \centering in cells, like:

Code: Select all

\documentclass{article}

\usepackage{array}
\usepackage{booktabs}

\begin{document}

\begin{table}
      \begin{tabular}{c c p{6cm}} \toprule
         \multicolumn{1}{m{2cm}}{\centering Name}
         & \multicolumn{1}{m{2.3cm}}{\centering Variable Form In Code}
         & \multicolumn{1}{m{6cm}}{\centering Meaning} \\
         \midrule
         x   &   \verb|x|   &   Thickness of the back iron on part 1 \\
        x   &   \verb|x|   &   Thickness of the magnet on part 1 \\
         \bottomrule
      \end{tabular}
\end{table}

\end{document}
Stefan
LaTeX.org admin
crobar
Posts: 12
Joined: Mon Nov 16, 2009 4:41 pm

Re: how to nicely wrap and center text in table cell?

Post by crobar »

Thank you! That's great.
Post Reply