Graphics, Figures & TablesForce line break in table cell

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
masher
Posts: 9
Joined: Fri Oct 23, 2009 4:24 am

Force line break in table cell

Post by masher »

Hi all

I have the following table (minimised) in my document:

Code: Select all

\begin{table}[htbp]
\begin{tabular}{ccccc}
 & Diamond size ($\mu$m) & Revolution speed (rpm) & Load (N) & Time (min)\tabularnewline
Grinding & 20 & 200 & 26 & 0.5\tabularnewline
\end{tabular}
\end{table}

It appears in the document as:

Code: Select all

           Diamond size (µm)  Revolution speed (rpm)  Load (N)  Time (min)
Grinding          20                   200               26         0.5
which is too wide.

How can I force a line break in each of the top cells such that the table appears as:

Code: Select all

           Diamond size  Revolution speed  Load  Time 
               (µm)           (rpm)         (N)  (min)
Grinding        20             200          26    0.5
?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Force line break in table cell

Post by localghost »

Just put the units in their own row and separate this head from the table body.

Code: Select all

\documentclass[11pt,a4paper,english]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage[font=small,labelfont=bf,tableposition=top]{caption}
\usepackage{geometry}
\usepackage{booktabs}
\usepackage{mathcomp,textcomp}

\begin{document}
  \begin{table}[!ht]
    \caption{Table caption}\label{tab:dummy}
    \centering
    \begin{tabular}{ccccc}\toprule
      & Diamond size & Revolution speed & Load & Time \\
      & (\textmu m) & (rpm) & (N) & (min) \\ \cmidrule{2-5}
      Grinding & 20 & 200 & 26 & 0.5 \\ \bottomrule
    \end{tabular}
  \end{table}
\end{document}
You can find more information to the used packages and their manuals on CTAN.
masher
Posts: 9
Joined: Fri Oct 23, 2009 4:24 am

Re: Force line break in table cell

Post by masher »

ta muchly! :)
NotDifficult
Posts: 1
Joined: Sun Feb 20, 2011 10:19 am

Force line break in table cell

Post by NotDifficult »

Here is a real and easy solution to the question of how to force a line break IN a table cell (for those cases where just adding a new row is not an option):

Code: Select all

\usepackage{pbox}

Code: Select all

\begin{tabular}{|l|l|} \hline
	\pbox{20cm}{This is the first \\ cell} & second \\ \hline
	3rd & and the last cell \\ \hline
\end{tabular}
Note that the width supplied to \pbox is a maximum width. If the content is shorter the length of the longest line is taken.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Force line break in table cell

Post by localghost »

NotDifficult wrote:Here is a real and easy solution to the question of how to force a line break IN a table cell (for those cases where just adding a new row is not an option): […] Note that the width supplied to \pbox is a maximum width. If the content is shorter the length of the longest line is taken.
A complete and instantly compilable example would have been nice. Nevertheless many thanks for bringing this package to our attention (though it is not really applicable in this case). Reminds me a little bit of the varwidth package. The only disavantage I see is that you have to force the line break manually if you don't want to exploit the full given width.


Best regards and welcome to the board
Thorsten
Post Reply