32

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?

2
  • 1
    I changed your title to reflect what you are actually asking. Oct 30, 2010 at 11:55
  • I reworded a sentence from your question to make it a little clearer. Feel free to revert if you prefer the previous version. Oct 31, 2010 at 12:07

3 Answers 3

33

You can do it with varwidth:

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

\begin{document}

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

Table with short rows:

\begin{tabular}{|M|}
hello hello hello
\end{tabular}

Same table, with long row:

\begin{tabular}{|M|}
hello hello hello hello hello hello 
\end{tabular}
\end{document}

Which compiles to:

alt text

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

3
  • Thank you, this is exactly what I was looking for! And thanks for the quick answer!
    – kamenyev
    Oct 30, 2010 at 12:47
  • I'm really surprised that there isn't any of the tabular packages that supports maximum width columns out of the box.
    – Florian
    Jul 30, 2013 at 20:46
  • Fails with multiple lines and cols. gives: Extra alignment tab has been changed to \cr. e.g. \begin{tabular}{MM} hello hello hellohello& hello hellohello hello hello\\ hello hello hello hello hello hellohello hello& hellohello hello hello hello &hello hello\\ \end{tabular}
    – borgr
    Jul 17, 2022 at 12:52
1

I suggest to use the column type V of varwidth. The output is slightly different from the ouput of Yossi Farjoun (see that question).

\documentclass{article}    
\usepackage{array}
\usepackage{varwidth} % must be loaded after array

\begin{document}

Table with short rows:

\begin{tabular}{|V{4cm}|}
hello hello hello
\end{tabular}


\bigskip
Same table, with long row:

\begin{tabular}{|V{4cm}|}
hello hello hello hello hello hello 
\end{tabular}
\end{document}

Output of the above code

0

you can also do:

\documentclass{article}    
\usepackage{array} % for defining a new column type

\begin{document}

Table with short rows:

\begin{tabular}{|p{4in}|}
hello hello hello
\end{tabular}

Same table, with long row:

\begin{tabular}{|p{2in}|}
hello hello hello hello hello hello 
\end{tabular}
\end{document}
1
  • 6
    I don't think that this answers the question. You specify a fixed column width, not a maximal one. Dec 10, 2010 at 15:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .