[Tex/LaTex] How to set a maximum column width


My problem is that I have many tables in my document with the same layout, and I want to specify the maximum width of a specific column to be 5 cm. I tried with p{5cm} but it make the column fix 5 cm wide and it looks odd with only a short word in it. I want it to be as narrow as possible, and split it into two lines, if it's wider than 5 cm. Is there any solution for my problem?

Best Answer

You can do it with varwidth:

\usepackage{array} % for defining a new column type
\usepackage{varwidth} %for the varwidth minipage environment


\newcolumntype{M}{>{\begin{varwidth}{4cm}}l<{\end{varwidth}}} %M is for Maximal column

Table with short rows:

hello hello hello

Same table, with long row:

hello hello hello hello hello hello 

Which compiles to:

alt text

Thank-you for inspiring me to look for this answer. It is a nice question!

Related Question