Generalmultiple line cell

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
montezumist
Posts: 3
Joined: Tue Aug 05, 2008 5:03 pm

multiple line cell

Post by montezumist »

Hi,

How can I create a table with cells that have several lines? So far, whenever I enter text in a cell, the text fits in a line - but there's always a hbox error.

I tried \linebreak but it's not working.

Thanks.

Recommended reading 2024:

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

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

Stefan Kottwitz
Site Admin
Posts: 10345
Joined: Mon Mar 10, 2008 9:44 pm

multiple line cell

Post by Stefan Kottwitz »

Hi,

you could use p columns, try this small example:

Code: Select all

\documentclass[a4paper,12pt]{article}
\begin{document}
\begin{tabular}{lp{0.75cm}}
1 & long text \\
\hline
2 & text
\end{tabular}
\end{document}
The array package provides more features, also regarding multiline cells.

Stefan
LaTeX.org admin
montezumist
Posts: 3
Joined: Tue Aug 05, 2008 5:03 pm

Re: multiple line cell

Post by montezumist »

Not working: I tried this:
\begin{tabular}{lp{2in}}
\hline
\bf Power Station & & \bf 2001 & \bf 2002 & \bf 2003 & \bf 2004 & \bf 2005 \rm \\ %there's a empty cell between ' & &'
\hline
Existing Villa Hydropower & MWh & 28570000 & 20283000 & 36083700 & 36854200 & 37103900\\

\hline
Diesel Engine ``TV2'' & MWh & 143700 & 310300 & 80800 & 20900 &\\\hline
Diesel Engine ``TV3'' & MWh & 24175000 & 30067000 & 14850000 & 16007000 & 19679000\\\hline
Diesel Engine ``Private Fraise'' & MWh & & 741450 & 6354050 & 8041850 & 5349350\\\hline
Total & & \bf 52888700 & \bf 51401750 & \bf 57268550 & \bf 60923950 & \bf 62132250\\
\end{tabular}

Here is my error log:
! Extra alignment tab has been changed to \cr.

I have no idea what's going on.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10345
Joined: Mon Mar 10, 2008 9:44 pm

multiple line cell

Post by Stefan Kottwitz »

You declared only 2 columns. Try this modified example:

Code: Select all

\documentclass[a4paper,10pt]{article}
\usepackage[hmargin={1in,1in}]{geometry}
\begin{document}
\begin{tabular}{p{1in}rrrrrr}
\hline
\textbf{Power Station} & & \textbf{2001} & \textbf{2002} &
\textbf{2003} & \textbf{2004} & \textbf{2005} \\ %there's a empty cell between ' & &'
\hline
Existing Villa Hydropower & MWh & 28570000 & 20283000 & 36083700 & 36854200 & 37103900\\
\hline
Diesel Engine ``TV2'' & MWh & 143700 & 310300 & 80800 & 20900 &\\\hline
Diesel Engine ``TV3'' & MWh & 24175000 & 30067000 & 14850000 & 16007000 & 19679000\\\hline
Diesel Engine ``Private Fraise'' & MWh & & 741450 & 6354050 & 8041850 & 5349350\\\hline
Total & & \bf 52888700 & \bf 51401750 & \bf 57268550 & \bf 60923950 & \bf 62132250\\
\end{tabular}
\end{document}
though the tabular layout may still need some improvement.

Stefan
LaTeX.org admin
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

multiple line cell

Post by gmedina »

Hi,

you are trying to create a seven column table but you only declare two columns in the table format.

In the code below I made some modifications to your table using some features provided by the array and booktabs packages. Refer to the packages documentation for further information.

Code: Select all

\documentclass{article}
\usepackage{array}
\usepackage{booktabs}
\begin{document}

\begingroup
  \setlength\tabcolsep{3pt}
  \small
  \begin{tabular}{b{1in}crrrrr}
    \toprule
    {\bfseries Power Station} & & {\bfseries 2001} & {\bfseries 2002} & {\bfseries 2003} & {\bfseries 2004} & {\bfseries 2005} \\
    \midrule
    Existing Villa\newline Hydropower & MWh & 28570000 & 20283000 & 36083700 & 36854200 & 37103900\\
    \midrule
    Diesel Engine\newline ``TV2'' & MWh & 143700 & 310300 & 80800 & 20900 &\\\midrule
    Diesel Engine\newline ``TV3'' & MWh & 24175000 & 30067000 & 14850000 & 16007000 & 19679000\\\midrule
    Diesel Engine\newline ``Private Fraise'' & MWh & & 741450 & 6354050 & 8041850 & 5349350\\\midrule
    Total & & {\bfseries 52888700} & {\bfseries 51401750} & {\bfseries 57268550} & {\bfseries 60923950} & {\bfseries 62132250}\\
  \end{tabular}
\endgroup

\end{document}
Remarks: 1) I enclosed the tabular environment to keep some changes (such as the font size) local.
2) I suggest you not to use obsolete commands such as \bf (use \bfseries instead). The reasons and more useful information can be found in l2tabu.
3) Basic information about tables can be found here:
Getting to grips with LaTeX - Tables.
4) The table still admits improvements.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
Post Reply