GeneralWordWrap in tables

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
Dosihris
Posts: 4
Joined: Sat Mar 01, 2008 3:41 pm

WordWrap in tables

Post by Dosihris »

Hi, i didn't find a solution for my problem by searching for table and wrap.

I'm a latex newbie and i just want to wordwrap the columns. I want that every column has the same width. thats all. In my table i have 11 columns and the text are always two words. here is a small example how my table looks like (but with less columns)

Code: Select all

\begin{table}
	\centering
		\hspace*{0cm}
		\begin{tabular}{|l*{3}{|c}}
		\hline
		\textbf{\small Number}		&	\textbf{\small long word}	&	\textbf{\small need wordwrap} & \textbf{\small doesn't work...} \\		
		\hline
		one 								&	a & b& c \\
		\hline
		two									&	a & b& c \\
		\hline
		three									&	a & b& c \\		
		\hline
		four								&	a & b& c \\
		\hline
		five									&	a & b& c \\
		\hline
		\end{tabular}
\end{table}
Is there a way to calculate the width automatically?

Recommended reading 2024:

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

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

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

WordWrap in tables

Post by gmedina »

You can use the tabularx package. Read the pakcage documentation.
tabularx documentation wrote:The tabularx package defines an environment tabularx, which has an additional column designator, X, which creates a paragraph-like column whose width automatically expands to the declared width of the environment. (Two X columns together share out the available space between them, and so on.)
A simple example:

Code: Select all

\documentclass{article}
\usepackage{tabularx}

\newcolumntype{Y}{>{\tiny\raggedright\arraybackslash}X}

\begin{document}

\noindent\begin{tabularx}{\linewidth}{|Y|Y|Y|Y|Y|Y|Y|}\hline
    a & two words & two words & two words & two words & two words & two words \\
    a & two words & two words & two words & two words & two words & two words \\
    a & two words & two words & two words & two words & two words & two words \\
    a & two words & two words & two words & two words & two words & two words \\ \hline
\end{tabularx}

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
Dosihris
Posts: 4
Joined: Sat Mar 01, 2008 3:41 pm

WordWrap in tables

Post by Dosihris »

Hi,

thanks for that information but it doesn't work totally... i have used the tabularyx package like this

Code: Select all

\newcolumntype{Y}{>{\tiny\raggedright\arraybackslash}X}

\noindent\begin{tabularx}{\linewidth}{|l*{11}{|c}}\hline
		Utility		&	 array/pointer	&	 NCRC &  input functions &  interaction effects &  bad error handler & signed characters & race condition & no source code & unknown \\		
\hline
    a & two words & two words & two words & two words & two words & two words \\
    a & two words & two words & two words & two words & two words & two words \\
    a & two words & two words & two words & two words & two words & two words \\
    a & two words & two words & two words & two words & two words & two words \\ \hline
\end{tabularx}
But it is not wraped. i want words like "input functions" or "array/pointer" wraped into two lines so that this table would fit on one page.
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

WordWrap in tables

Post by gmedina »

Then it could be enough to use the p{<width>} declaration in the format of the table:

Code: Select all

\documentclass{article}
\usepackage{array}
\usepackage{graphicx}

\begin{document}

\begin{center}
\addtolength{\tabcolsep}{-4pt}
\scalebox{0.7}{%
  \begin{tabular}{*{10}{>{\scriptsize}p{1.4cm}}}\hline
    Utility & array &  NCRC &  input &  interaction &  bad error & signed & race & no source & unknown \\      
    &    /pointer   && functions & effects & handler & characters & condition && \\ \hline
    a & two words & two words & two words & two words & two words & two words & two words & two words & two words \\ \hline
  \end{tabular}}
\end{center}

\end{document}
Remarks: 1) I reduced the separation between columns and scaled your table using the \scalebox command provided by the graphicx package.
2) Since your actual table seems to be bigger than the one used above, you could consider rotating it, using the rotating or the hvfloat package.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
Post Reply