Graphics, Figures & Tables ⇒ A lot of text in the middle field in a table with line breaks
A lot of text in the middle field in a table with line breaks
NEW: TikZ book now 40% off at Amazon.com for a short time.
And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p
A lot of text in the middle field in a table with line breaks

I don't see any need to use the
\multicolumn
or \multirow
commands to create the table in the attached file. In the LaTeX introductions, column types should be named and explained that allow multi-line cells. You may also be interested in the tabularx
package, if the multi-line column has the permission to occupy the available space.A lot of text in the middle field in a table with line breaks
This far I have tried something along these lines:Bartman wrote:Please give aminimal working example of what you have tried so far.
I don't see any need to use the\multicolumn
or\multirow
commands to create the table in the attached file. In the LaTeX introductions, column types should be named and explained that allow multi-line cells. You may also be interested in thetabularx
package, if the multi-line column has the permission to occupy the available space.
Code: Select all
\documentclass[11pt]{article}
\usepackage{multirow}
\begin{document}
\begin{tabular}{|c|c|c|}
\hline
A & \multicolumn{*}{2}{X1 \\
X2} & B \\
\hline
\end{tabular}
\end{document}
But I found something which worked: the nested tabular solution from here, however it is cluncy is there a nicer way?
- Stefan Kottwitz
- Site Admin
- Posts: 10335
- Joined: Mon Mar 10, 2008 9:44 pm
A lot of text in the middle field in a table with line breaks
That's because the LaTeX.org compiler does it correctly, it gives an error message that a number is expected instead of the asterisk/starblasted wrote:why this code won't work here, it compiled on Overleaf, the screenshot is from there
*
that is wrong in this \multicolumn
argument. It should be the number of columns. Overleaf, in contrast, translates it for you anyway and it looks wrong. It prints the asterisk/star - did you want this? Looks like Overleaf does silently things wrong 
Ok, let's fix it.
First, as Bartman said, you don't need
\multicolumn
or \multirow
here. LaTeX does the line break in table cells for you automatically. You can take a p
-type column. That's a column with paragraph cells. You just need to tell LaTeX how wide it is, so it knows where to automatically break the lines for you. For demonstrating, I use the blindtext
package with its \blindtext
command to produce some sample text.Code: Select all
\documentclass[11pt]{article}
\usepackage{blindtext}
\begin{document}
\begin{tabular}{|c|p{8cm}|c|}
\hline
A & \blindtext & B \\
\hline
\end{tabular}
\end{document}
p
column cell, you could even to manual line breaks, for example:Code: Select all
\documentclass[11pt]{article}
\begin{document}
\begin{tabular}{|c|p{1cm}|c|}
\hline
A & X1 \newline X2 & B \\
\hline
\end{tabular}
\end{document}
tabularx
package and the table environment with the same name, you can have such colums even with automatically adjusted width. Just tell it how wide the table is, as a whole. Such as \textwidth, the whole width of the text. I even use \newline
here, again. Not \\
, since that marks the end of a row in a table.Code: Select all
\documentclass[11pt]{article}
\usepackage{tabularx}
\usepackage{blindtext}
\begin{document}
\noindent
\begin{tabularx}{\textwidth}{|c|X|c|}
\hline
A & \blindtext \newline and more & B \\
\hline
\end{tabularx}
\end{document}
microtype
, it looks much better. Compare the output of the next example with the previous example output, no hyphenation needed anymore:Code: Select all
\documentclass[11pt]{article}
\usepackage{tabularx}
\usepackage{blindtext}
\usepackage{microtype}
\begin{document}
\noindent
\begin{tabularx}{\textwidth}{|c|X|c|}
\hline
A & \blindtext \newline and more & B \\
\hline
\end{tabularx}
\end{document}
A lot of text in the middle field in a table with line breaks
There were error message, but there was also an output, so I though well not so bad. But the rest of the answer quiet helped thanks.Stefan Kottwitz wrote:Hi!
That's because the LaTeX.org compiler does it correctly, it gives an error message that a number is expected instead of the asterisk/starblasted wrote:why this code won't work here, it compiled on Overleaf, the screenshot is from there*
that is wrong in this\multicolumn
argument. It should be the number of columns. Overleaf, in contrast, translates it for you anyway and it looks wrong. It prints the asterisk/star - did you want this? Looks like Overleaf does silently things wrongor you don't see the error message(s) there.
...