14

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   |
+---------+
1
  • 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. Jan 4, 2014 at 16:20

2 Answers 2

17

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}
4
  • If I have more than one column, should I use \raggedright}p{2cm}| multiple times?
    – mahmood
    Jan 4, 2014 at 12:44
  • You want to add \arraybackslash right after \raggedright in the column declaration. (Try adding a column before the existing one.) Jan 4, 2014 at 12:44
  • @mahmood: see my updated answer.
    – rainer
    Jan 4, 2014 at 12:56
  • @TorbjørnT.: Thanks, I completely forgot about that! Answer updated.
    – rainer
    Jan 4, 2014 at 12:56
6

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

1
  • 4
    \raggedright is essential anyway.
    – egreg
    Jan 4, 2014 at 13:38

You must log in to answer this question.

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