## LaTeX forum ⇒ Graphics, Figures & Tables ⇒ Cells with text paragraphs in a table Topic is solved

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

### Cells with text paragraphs in a table

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

Here's a MWE showing my troubles :
1. \documentclass[11pt,letterpaper,twoside]{article}
3.
4. \begin{document}
5.
6. \begin{table}
7. \centering
8. \renewcommand{\arraystretch}{1.5}
9. \begin{tabular}{cccc}
10. \hline
11. \multicolumn{1}{|c|}{\textsf{Sections}}
12. & \multicolumn{1}{c|}{\textsf{Questions}}
13. & \multicolumn{1}{c|}{\textsf{Exercices}}
14. & \multicolumn{1}{c|}{\textsf{Problems}}
15. \\ \hline
16. % Chapter 1 :
17. \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,}
18. \\[-5pt]
19. \multicolumn{1}{|l|}{ } & \multicolumn{1}{l|}{ } & \multicolumn{1}{l|}{21, 32} & \multicolumn{1}{l|}{8, 13}
20. \\ \hline
21. % Chapter 2 :
22. \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}
23. \\[-5pt]
24. \multicolumn{1}{|l|}{ } & \multicolumn{1}{l|}{ } & \multicolumn{1}{l|}{23, 27, 29, 32, 35, 36, 38} & \multicolumn{1}{l|}{ }
25. \\ \hline
26. % Chapter 3 :
27. \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}
28. \\ \hline
29. \end{tabular}
30. \end{table}
31.
32. \end{document}

A preview of what this code is doing, with the troubles shown in red :
troubletable.jpg (31.91 KiB) Viewed 281 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.

Stefan Kottwitz
Posts: 8534
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:
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:

1. \documentclass[11pt,letterpaper,twoside]{article}
3. \usepackage{tabularx}
4. \begin{document}
5. \begin{table}
6. \centering
7. \renewcommand{\arraystretch}{1.5}
8. \begin{tabularx}{\textwidth}{|X|l|p{4cm}|l|}
9. \hline
10. \multicolumn{1}{|c|}{\textsf{Sections}}
11. & \multicolumn{1}{c|}{\textsf{Questions}}
12. & \multicolumn{1}{c|}{\textsf{Exercices}}
13. & \multicolumn{1}{c|}{\textsf{Problems}}
14. \\ \hline
15. % Chapter 1 :
16. 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
17. \\ \hline
18. % Chapter 2 :
19. 2.1 to 2.10 & 1, 8 & 1, 3, 5, 9, 10, 14, 17, 19, 20, 23, 27, 29, 32, 35, 36, 38 & 4
20. \\ \hline
21. % Chapter 3 :
22. 3.1 to 3.5 & 4, 10 & 1, 3, 4, 5 & 3, 6, 9
23. \\ \hline
24. \end{tabularx}
25. \end{table}
26. \end{document}

It can further be improved, I think. Let me know what you think.

Stefan

Cham
Posts: 819
Joined: Sat Apr 02, 2011 4:06 pm
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 ?

1. \usepackage[T1]{fontenc}
2. \usepackage[utf8]{inputenc}
3. \usepackage[french]{babel}

For example, just change "Problems" to "Problèmes", and it kills the compilation.

Stefan Kottwitz
Posts: 8534
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:
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

Cham
Posts: 819
Joined: Sat Apr 02, 2011 4:06 pm
Stefan,

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

I need to tweak the table size. I think that the following command line isn't properly set :
1. \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 (18.74 KiB) Viewed 238 times

Here's the code which reproduces the gap problem :
1. \documentclass[11pt,letterpaper,twoside]{article}
2. \usepackage{lmodern}
3. \usepackage{textcomp}
4. \raggedbottom
5. \usepackage{microtype}
7. \usepackage{tabularx}
8.
9. \begin{document}
10.
11. \begin{table}
12. \centering
13. \renewcommand{\arraystretch}{1.5}
14. \begin{tabularx}{\textwidth}{|p{3.5cm}|p{2cm}|p{6cm}|p{2cm}|}
15. \hline
16. \multicolumn{1}{|c|}{\textsf{Sections}}
17. & \multicolumn{1}{c|}{\textsf{Questions}}
18. & \multicolumn{1}{c|}{\textsf{Exercices}}
19. & \multicolumn{1}{c|}{\textsf{Problems}}
20. \\ \hline
21. 1.1 to 1.3, 1.4* \newline Some comments
22. & 2, 3, 14
23. & 1, 5, 7, 8, 11, 12, 15, 17, 20, 21, 32
24. & 1, 5, 6, 8*, 13*
25. \\ \hline
26. 2.1 to 2.10
27. & 1, 8
28. & 1, 3, 5, 9, 10, 14, 17, 19, 20, 23, 27, 29, 32, 35, 36, 38
29. & 4
30. \\ \hline
31. 3.1 to 3.5
32. & 4, 10
33. & 1, 3, 4, 5, 12, 13, 15, 22, 23, 25, 29, 33, 36, 41
34. & 3*, 6, 9
35. \\ \hline
36. \end{tabularx}
37. \end{table}
38.
39. \end{document}

Cham
Posts: 819
Joined: Sat Apr 02, 2011 4:06 pm
Stefan,

I still don't understand that line in the table code :
1. \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 ?

Stefan Kottwitz
Posts: 8534
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:
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

Cham
Posts: 819
Joined: Sat Apr 02, 2011 4:06 pm
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 ?

Stefan Kottwitz
Posts: 8534
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:
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

Cham
Posts: 819
Joined: Sat Apr 02, 2011 4:06 pm
Ok, thanks Stefan. My issue appears to be solved.

### Who is online

Users browsing this forum: No registered users and 8 guests