Graphics, Figures & TablesA lot of text in the middle field in a table with line breaks

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
blasted
Posts: 3
Joined: Fri Apr 09, 2021 3:34 pm

A lot of text in the middle field in a table with line breaks

Post by blasted »

I am trying to a table where one of the fields has much more text then the ones left and right to it so it needs to be wider and also line breaks. I have made an example in Word how this should look, see the attached PDF
how_it_sould_be.pdf
(75.73 KiB) Downloaded 337 times
. Now I have tried to work this out with multicloumn and multirow but I just can't seem to get it right where I have normal fields on both sides, please help.

Recommended reading 2024:

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

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

Bartman
Posts: 369
Joined: Fri Jan 03, 2020 2:39 pm

A lot of text in the middle field in a table with line breaks

Post by Bartman »

Please give a Infominimal 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 the tabularx package, if the multi-line column has the permission to occupy the available space.
blasted
Posts: 3
Joined: Fri Apr 09, 2021 3:34 pm

A lot of text in the middle field in a table with line breaks

Post by blasted »

Bartman wrote:Please give a Infominimal 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 the tabularx package, if the multi-line column has the permission to occupy the available space.
This far I have tried something along these lines:

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}
unsurprisingly with an unsatisfying result:
Screenshot_2021-04-09 TryIt.png
Screenshot_2021-04-09 TryIt.png (2.34 KiB) Viewed 14563 times
(no idea why this code won't work here, it compiled on Overleaf, the screenshot is from there.)
But I found something which worked: the nested tabular solution from here, however it is cluncy is there a nicer way?
User avatar
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

Post by Stefan Kottwitz »

Hi!
blasted wrote:why this code won't work here, it compiled on Overleaf, the screenshot is from there
That's because the LaTeX.org compiler does it correctly, it gives an error message that a number is expected instead of the asterisk/star * 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 ;-) or you don't see the error message(s) there.

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}
In a 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}
With the 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}
Another point: by loading 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}
Stefan
LaTeX.org admin
blasted
Posts: 3
Joined: Fri Apr 09, 2021 3:34 pm

A lot of text in the middle field in a table with line breaks

Post by blasted »

Stefan Kottwitz wrote:Hi!
blasted wrote:why this code won't work here, it compiled on Overleaf, the screenshot is from there
That's because the LaTeX.org compiler does it correctly, it gives an error message that a number is expected instead of the asterisk/star * 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 ;-) or you don't see the error message(s) there.

...
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.
Post Reply