Graphics, Figures & TablesCells with text paragraphs in a table

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Cells with text paragraphs in a table

Post by Cham »

I'm again having troubles with tables. I will never understand how tables are really working in LaTeX ! :oops:

Here's a MWE showing my troubles :

Code: Select all

\documentclass[11pt,letterpaper,twoside]{article}
\usepackage[total={6in,10in},left=1.5in,top=0.5in,includehead,includefoot]{geometry}

\begin{document}

\begin{table}
	\centering
	\renewcommand{\arraystretch}{1.5}
	\begin{tabular}{cccc}
	\hline
	\multicolumn{1}{|c|}{\textsf{Sections}}
	& \multicolumn{1}{c|}{\textsf{Questions}}
	& \multicolumn{1}{c|}{\textsf{Exercices}}
	& \multicolumn{1}{c|}{\textsf{Problems}}
	\\ \hline
	% Chapter 1 :
	\multicolumn{1}{|l|}{Read the whole chapter, except sections 1.5, which is optional.} & \multicolumn{1}{l|}{2, 3, 14} & \multicolumn{1}{l|}{1, 5, 7, 8, 11, 12, 15, 17, 20,} & \multicolumn{1}{l|}{1, 5, 6,}
	\\[-5pt]
	\multicolumn{1}{|l|}{ } & \multicolumn{1}{l|}{ } & \multicolumn{1}{l|}{21, 32} & \multicolumn{1}{l|}{8, 13}
	\\ \hline
	% Chapter 2 :
	\multicolumn{1}{|l|}{2.1 to 2.10} & \multicolumn{1}{l|}{1, 8} & \multicolumn{1}{l|}{1, 3, 5, 9, 10, 14, 17, 19, 20,} & \multicolumn{1}{l|}{4}
	\\[-5pt]
	\multicolumn{1}{|l|}{ } & \multicolumn{1}{l|}{ } & \multicolumn{1}{l|}{23, 27, 29, 32, 35, 36, 38} & \multicolumn{1}{l|}{ }
	\\ \hline
	% Chapter 3 :
	\multicolumn{1}{|l|}{3.1 to 3.5} & \multicolumn{1}{l|}{4, 10} & \multicolumn{1}{l|}{1, 3, 4, 5} & \multicolumn{1}{l|}{3, 6, 9}
	\\ \hline
	\end{tabular}
\end{table}

\end{document}
A preview of what this code is doing, with the troubles shown in red :
troubletable.jpg
troubletable.jpg (31.91 KiB) Viewed 22780 times
How can I write the text paragraph so it automatically fills the cell vertically, without extending the column's width ?

Currently, I had to define a second row to place the "Exercices" numbers on a second row. This is very ackward to edit and unnatural to code.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
TikZ book
User avatar
Stefan Kottwitz
Site Admin
Posts: 10314
Joined: Mon Mar 10, 2008 9:44 pm

Cells with text paragraphs in a table

Post by Stefan Kottwitz »

Hi Cham!

Normal l, c, or r cells are just aligned, but never wrapped, because there's nothing that tells LaTeX how wide it should be, to make a line break there. However, one could use p (paragraph) cells with specifying a width, then the cell is made to that width with line breaks. Furthermore, with the tabularx package, there's a similar X cell type, that automatically fits to the available width. For this, one defines the whole tabular width as a reference.

\multicolumn is intended for cells spanning multiple columns. It can also be used as way to define a different formatting for a certain cell, as you did with the headings. If neither multiple cells are spanned nor the format is changed, there's no need.

Here is an improved table:

Code: Select all

\documentclass[11pt,letterpaper,twoside]{article}
\usepackage[total={6in,10in},left=1.5in,top=0.5in,includehead,includefoot]{geometry}
\usepackage{tabularx}
\begin{document}
\begin{table}
	\centering
	\renewcommand{\arraystretch}{1.5}
	\begin{tabularx}{\textwidth}{|X|l|p{4cm}|l|}
	\hline
	\multicolumn{1}{|c|}{\textsf{Sections}}
	& \multicolumn{1}{c|}{\textsf{Questions}}
	& \multicolumn{1}{c|}{\textsf{Exercices}}
	& \multicolumn{1}{c|}{\textsf{Problems}}
	\\ \hline
	% Chapter 1 :
	Read the whole chapter, except sections 1.5, which is optional. & 2, 3, 14 & 1, 5, 7, 8, 11, 12, 15, 17, 20, 21, 32 & 1, 5, 6, 8, 13
	\\ \hline
	% Chapter 2 :
	2.1 to 2.10 & 1, 8 & 1, 3, 5, 9, 10, 14, 17, 19, 20, 23, 27, 29, 32, 35, 36, 38 & 4
	\\ \hline
	% Chapter 3 :
	3.1 to 3.5 & 4, 10 & 1, 3, 4, 5 & 3, 6, 9
	\\ \hline
	\end{tabularx}
\end{table}
\end{document}
It can further be improved, I think. Let me know what you think.

Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Cells with text paragraphs in a table

Post by Cham »

Stefan,

this table appears to be fine, except that French accents stops the compilation, even if I use the following commands in my preamble, why ?

Code: Select all

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
For example, just change "Problems" to "Problèmes", and it kills the compilation.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10314
Joined: Mon Mar 10, 2008 9:44 pm

Cells with text paragraphs in a table

Post by Stefan Kottwitz »

It works fine for me, with those three lines, and the word "Problèmes". There's no error and it's correctly displayed. Perhaps there's a conflict somewhere else in your code if you tried it in your complete code. Did you test it in the minimal example? Perhaps post the code that produces the error, with the error message just in case.

Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Cells with text paragraphs in a table

Post by Cham »

Stefan,

Ok, I solved the issue. The test file wasn't saved as an UTF8 text file. Doh ! :oops:

I need to tweak the table size. I think that the following command line isn't properly set :

Code: Select all

\begin{tabularx}{\textwidth}{|p{3.5cm}|p{2cm}|p{6cm}|p{2cm}|}
I need to have the table as large as the normal text, and the cells (rows) should have the same vertical height (for aesthetical reasons). Yet, the third column should be much larger than the other columns. How to defined this properly ?

Also, all the cells elements should use \raggedright. Where to add this command, as a global option in the table ?

EDIT : I also have a small gap on the lines of the table, on the right part :
gaps.jpg
gaps.jpg (18.74 KiB) Viewed 22737 times
Here's the code which reproduces the gap problem :

Code: Select all

\documentclass[11pt,letterpaper,twoside]{article}
\usepackage{lmodern}
\usepackage{textcomp}
\raggedbottom
\usepackage{microtype}
\usepackage[total={6in,10in},left=1.5in,top=0.5in,includehead,includefoot]{geometry}
\usepackage{tabularx}

\begin{document}

\begin{table}
	\centering
	\renewcommand{\arraystretch}{1.5}
	\begin{tabularx}{\textwidth}{|p{3.5cm}|p{2cm}|p{6cm}|p{2cm}|}
	\hline
	\multicolumn{1}{|c|}{\textsf{Sections}}
	& \multicolumn{1}{c|}{\textsf{Questions}}
	& \multicolumn{1}{c|}{\textsf{Exercices}}
	& \multicolumn{1}{c|}{\textsf{Problems}}
	\\ \hline
	1.1 to 1.3, 1.4* \newline Some comments
	& 2, 3, 14
	& 1, 5, 7, 8, 11, 12, 15, 17, 20, 21, 32
	& 1, 5, 6, 8*, 13*
	\\ \hline
	2.1 to 2.10
	& 1, 8
	& 1, 3, 5, 9, 10, 14, 17, 19, 20, 23, 27, 29, 32, 35, 36, 38
	& 4
	\\ \hline
	3.1 to 3.5
	& 4, 10
	& 1, 3, 4, 5, 12, 13, 15, 22, 23, 25, 29, 33, 36, 41
	& 3*, 6, 9
	\\ \hline
	\end{tabularx}
\end{table}

\end{document}
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Cells with text paragraphs in a table

Post by Cham »

Stefan,

I still don't understand that line in the table code :

Code: Select all

\begin{tabularx}{\textwidth}{|p{1.5cm}|p{10cm}|p{2.5cm}|}
Whatever which values I use, I still get some gaps on the lines at the right side of the table. How should I define the spacing in that line ?
User avatar
Stefan Kottwitz
Site Admin
Posts: 10314
Joined: Mon Mar 10, 2008 9:44 pm

Cells with text paragraphs in a table

Post by Stefan Kottwitz »

tabularx is designed to have at least one column, that automatically fits the remaining width. That's the X column specifier in my code. One uses some p columns with a certain width, and simply an X column to use as much space as possible. Usually I take the widest column s the "stretching" column. We can use several, then they share the space equally. That's why tabularx has a width option, in contrast to standard tabular.

Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Cells with text paragraphs in a table

Post by Cham »

Ah ! Well, that wasn't evident. But it indeed appears to solve the spacing and gaps issue. Thanks a lot Stefan !

EDIT : But is there a way to set \raggedright globally for the whole table, instead of using that command cell by cell ?
User avatar
Stefan Kottwitz
Site Admin
Posts: 10314
Joined: Mon Mar 10, 2008 9:44 pm

Cells with text paragraphs in a table

Post by Stefan Kottwitz »

I usually don't see an edit to a post that I already read, but with a new post I get notified by mail and see it in the active topics here.

You can define a ragged right column type that works like p with \raggedright:

\usepackage{array}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}


Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Cells with text paragraphs in a table

Post by Cham »

Ok, thanks Stefan. My issue appears to be solved.
Post Reply