Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Is there any option in tabular that can move the whole word to the next line whenever the length of the word is greater than the fixed cell size?

Currently I use

\begin{tabular}{ | p{2cm} | }

And that results in this format

+---------+
| First t-|
| hing    |
+---------+

However I am looking for this shape

+---------+
| First   |
| thing   |
+---------+
share|improve this question
    
the answers recommending \raggedright are on target. but i find the hyphenation you show to be very strange indeed! if that actually happens (as opposed to being simply a made-up example), the hyphenation in the rest of your document must also be questionable. –  barbara beeton Jan 4 at 16:20
add comment

2 Answers 2

up vote 18 down vote accepted

You can use \raggedright:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[american]{babel}
\usepackage{array}

\begin{document}

\begin{tabular}{|p{2cm}|p{2cm}|}
  First longerword & First longerword \\
  Second Thing & Second Thing \\
\end{tabular}

\vspace{\baselineskip}

\begin{tabular}{|>{\raggedright}p{2cm}|>{\raggedright\arraybackslash}p{2cm}|}
  First longerword & First longerword \\
  Second Thing & Second Thing \\
\end{tabular}

\end{document}

The second tabular will include a \raggedright in each cell of the p column, which will avoid the hyphenation.

Note that you need to load the array package for this to work. You also need \arraybackslash in at least the last column to have \\ correctly end the table row.

Output:

output

If you need this for just a few cells, you can also use \raggedright in these cells:

\begin{tabular}{|p{2cm}|p{2cm}|}
  \raggedright First longerword & \raggedright\arraybackslash First longerword \\
  Second Thing & Second Thing \\
\end{tabular}

If it's more than one column, you can also define your own column type:

\newcolumntype{R}{>{\raggedright\arraybackslash}p{2cm}}

\begin{tabular}{|R|R|}
  First longerword & First longerword \\
  Second Thing & Second Thing \\
\end{tabular}
share|improve this answer
    
If I have more than one column, should I use \raggedright}p{2cm}| multiple times? –  mahmood Jan 4 at 12:44
    
You want to add \arraybackslash right after \raggedright in the column declaration. (Try adding a column before the existing one.) –  Torbjørn T. Jan 4 at 12:44
    
@mahmood: see my updated answer. –  rainer Jan 4 at 12:56
    
@TorbjørnT.: Thanks, I completely forgot about that! Answer updated. –  rainer Jan 4 at 12:56
add comment

Another possibility is to avoid hypehenation by setting \hyphenpenalty=10000. I have defined a new column with this setting. You can add it in individual cells too with p columns.

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[american]{babel}
\usepackage{array}
\newcolumntype{D}[1]{>{\hyphenpenalty=10000}p{#1}}


\begin{document}

\begin{tabular}{|p{2cm}|p{2cm}|}
  First longerword & First longerword \\
  Second Thing & Second Thing \\
\end{tabular}

\vspace{\baselineskip}

\begin{tabular}{|D{2cm}|D{2cm}|}    
  First longerword & First longerword \\
  Second Thing & Second Thing \\
\end{tabular}

\end{document}

enter image description here

share|improve this answer
4  
\raggedright is essential anyway. –  egreg Jan 4 at 13:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.