Friday, January 22, 2010

Wrapping column text in a table in LaTeX

Let's say you have the following table in LaTeX:

1
2
3
4
5
6
\begin{tabular}{|l|l|}
  \hline
  This is a very very long cell name.  Very long indeed 
  & This is a very, very, very long line that keeps on going even outside of the page!!!!! \\
  \hline
\end{tabular}

The output generated from the above tabular environment is as follows:



As you can see from the above output, the text does not wrap but just keeps going off out of the page.

Introducing the TabularX package


To fix this problem, we will use the tabularx package and use the tabularx environment:

1
\usepackage{tabularx}

1
2
3
4
5
6
\begin{tabularx}{\linewidth}{|X|X|}
  \hline
  This is a very very long cell name.  Very long indeed 
  & This is a very, very, very long line that keeps on going even outside of the page!!!!! \\
  \hline
\end{tabularx}

The generic template of the tabularx environment is as follows:

1
\begin{tabularx}{w}{\X\X . . .\}

where:
  • w : the desired width of the table
  • X : marks each column for which the width is to be adjusted

This is now the output with the tabularx environment:




What I was listening to

Comments (4)

Loading... Logging you in...
  • Logged in as
Only thing that worked for me!
You have shared an informative post here. I am able to learn how to wrap column text in a table in LaTeX from this post. Actually you have done an appreciable job. Keep sharing such interesting and informative post here!
GANESH MANJHI's avatar

GANESH MANJHI · 426 weeks ago

How the command work if the size of the column are not same
satish salunkhe's avatar

satish salunkhe · 426 weeks ago

begin{table}[]
centering
caption{My caption}
label{my-label}
begin{tabularx}{linewidth}{|X|X|X|X|}
toprule
Similarity measure & Posssibility alpha-value & Number of clusters & {Clusters with elements}, different clusters are separated by ';' \ midrule
Bart Kosko & 1 & 5 & {{1,2,4,5,10,11,12,13,14,15,18};{3,9};{6,7,8,17};{16,20};{19}} \
& 0.7818 & 1 & {{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}} \
Correlation & 1 & 5 & {{1,2,4,5,10,11,12,13,14,15,18};{3,9};{6,7,8,17};{16,20};{19}} \
& 0.8944 & 4 & {{1,2,4,5,10,11,12,13,14,15,18};{3,9};{6,7,8,16,17,20};{19}} \
& 0.8839 & 3 & {{1,2,4,5,6,7,8,10,11,12,13,14,15,16,17,18,20};{3,9};{19}} \
& 0.8677 & 2 & {{1,2,4,5,6,7,8,10,11,12,13,14,15,16,17,18,19,20};{3,9}} \
& 0.8416 & 1 & {{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}} \
Koczy & 1 & 5 & {{1,2,4,5,10,11,12,13,14,15,18};{3,9};{6,7,8,17};{16,20};{19}} \
& 0.8209 & 1 & {{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}} \
William & 1 & 5 & {{1,2,4,5,10,11,12,13,14,15,18};{3,9};{6,7,8,17};{16,20};{19}} \
& 0.804 & 1 & {{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}} \ bottomrule
end{tabularx}
end{table}

I am not getting the last column wrapped. please help me.

Post a new comment

Comments by