6

I'm new to tex and got a problem with a table that exceeds the page, because the content is too wide. I'd expect the table to wrap the text into a new line / new lines, if the text is too long for the tables width.

I guess that this is not a default behaviour. How do I turn it on?

This is my minimal tex code:

\documentclass[12pt,a4paper,oneside,fleqn]{report}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{geometry}
\geometry{a4paper,left=15mm,right=15mm, top=1cm, bottom=2cm}
\usepackage{euler} 
\usepackage{graphicx}
\parindent 0pt
\setlength{\abovedisplayskip}{0pt}
\begin{document}
\begin{tabular}[ht]{|l|c|l|}
\hline
  a & b & Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.\\
  \hline
\end{tabular}
\end{document}
3
  • Here's a relevant thread regarding text wrapping inside a table.
    – user6967
    Oct 25, 2011 at 18:51
  • 3
    Welcome to TeX.sx! Just on a side note, for future reference: While your post is nicely reduced (thanks for that!), it isn't really a minimal yet. Have a look at Werner's solution, he got rid of most of the stuff that doesn't contribute to the problem. Even 12pt and geometry aren't really necessary to produce the problem. There's more information at meta.tex.stackexchange.com/questions/228/…. Looking forward to seeing more posts by you!
    – doncherry
    Oct 25, 2011 at 19:52
  • Here is an answer posted previously. tex.stackexchange.com/a/194100/233680 Feb 24, 2021 at 17:53

3 Answers 3

12

You can either fix the column width by specifying a paragraph style p{<len>}, or you can use a different tabular environment that stretches according to the contents up to the maximum width of the text block. The latter is provided by the tabularx package which defines the environment tabularx with an additional width-of-table argument, as well as the stretchable X column specifier.

An example of the first suggestion:

enter image description here

\documentclass[12pt]{report}
\usepackage[a4paper,left=15mm,right=15mm, top=1cm, bottom=2cm]{geometry}% http://ctan.org/pkg/geometry
\begin{document}
\noindent\begin{tabular}{|l|c|p{0.7\linewidth}|}
  \hline
  a & b & Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.\\
  \hline
\end{tabular}
\end{document}​

An example of the second suggestion:

enter image description here

\documentclass[12pt]{report}
\usepackage[a4paper,left=15mm,right=15mm, top=1cm, bottom=2cm]{geometry}% http://ctan.org/pkg/geometry
\usepackage{tabularx}% http://ctan.org/pkg/tabularx
\begin{document}
\noindent\begin{tabularx}{\linewidth}{|l|c|X|}
  \hline
  a & b & Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.\\
  \hline
\end{tabularx}
\end{document}​

The tabularx approach is an easy way out since you don't have to worry about specifying a column width that exactly matches the remaining column space on the page. However, it is possible to calculate the appropriate value of <len> when using the p{<len>} column specifier.

9

LaTeX's algorithms for formatting tables have a few shortcomings. One is that it will not automatically wrap text in cells, even if it overruns the width of the page. For columns that you know will contain a certain amount of text, then it is recommended that you use the p attribute and specify the desired width of the column (although it may take some trial-and-error to get the result you want). Use the m attribute to have the lines aligned toward the middle of the box and the b attribute to align along the bottom of the box.

LaTeX/Tables - Wikibooks, open books for an open world

Use p{width} to set the column's width. Here is an example, based on yours:

\begin{tabular}[ht]{|l|c|p{5cm}|}
\hline
    a & b & Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.\\
\hline
\end{tabular}

If you're new to LaTeX you should use the Wikibooks LaTeX book (upper link), it has a lot of basic tips and tricks.

4

Wrapping around done for p{LENGTH} columns. Just change your |l| to |p{2cm}| (or whatever length you need)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .