# Longtable of size \textwidth exceeds right margin

I try to create a table with the longtable package that fits into the whole text width available. Here's my code:

\documentclass{article}
\usepackage{longtable}
\usepackage{amsmath}

\title{Raccolta delle formule}
\author{}
\date{}
\begin{document}
\maketitle
\begin{longtable}{p{.33\columnwidth} | p{.33\columnwidth} | p{.33\columnwidth} }
\Large Descrizione & \Large Formula & \Large Esempio\\
\hline

Prodotto di due polinomi &
{
\begin{align*}
(a+b)(c+d)&=a(c+d)+b(c+d)\\
\end{align*}
}&
{
\begin{align*}
(2x+3)(3y-5)&=6xy-10x+9y-15
\end{align*}
}\\
\end{longtable}

\end{document}


The result is, that the table exceeds the right margin (see picture). What is wrong with the code?

• You are forgetting the space before and between the columns and the rule (in the sum 6*\tabcolsep + 2*\arrayrulewidth). – Ulrike Fischer Jul 21 at 9:03
• To make your table column narrow enough to make the table fit inot the textwidth, you can add the calc package to your preamble and replace all occurences of .33\columnwidth with .33\columnwidth-2\tabcolsep. Apart from that, and as you have already noticed, your equations are too wide to fit into the table columns. – leandriis Jul 21 at 9:07
• You can use package xltabular and the environment of the same name and the column specifier X – user187802 Jul 21 at 9:26
• @Red-Cloud: While I generally like this suggestion as it is more automatic and convenient than manually calculating the required column widths this will not solve the problem of the too wide equations. – leandriis Jul 21 at 9:29
• Will all first column cells be one-lined? – Bernard Jul 21 at 9:47

Here is a possible solution, using the xltabular environment, which brings the functionalities of longtable to tabularx, playing with the value of the inter-column spacing, changing the alignment point:

\documentclass{article}
\usepackage{showframe}
\renewcommand{\ShowFrameLinethickness}{0.3pt}
\usepackage{longtable}
\usepackage{xltabular}
\usepackage{mathtools}

\title{Raccolta delle formule}
\author{Geppetto}
\date{}

\begin{document}
\maketitle

\setlength{\tabcolsep}{4pt}
\setlength{\extrarowheight}{4pt}
\begin{xltabular}{\linewidth}{l|X|X}
\Large Descrizione & \Large Formula & \Large Esempio\\
\hline

Prodotto di due polinomi
& \begin{aligned}[t] (a&+b)(c+d) \\ & =a(c+d)+b(c+d)\\ &=ac+ad+bc+bd \end{aligned}
& \begin{aligned}[t] &(2x +3)(3y-5)\\ & =6xy-10x+9y-15 \end{aligned} \\
\end{xltabular}

\end{document}


I found the problem: the formulas in the columns are too wide and latex has expanded the column sizes.

EDIT LaTeX does nothing on it own, it is you that did not take into consideration what a tabular consist of. Try the following preamble in addition to narrow you formulas:

\begin{longtable}{@{}
p{\dimexpr(\columnwidth)*33/100-\tabcolsep-(2\arrayrulewidth/3)\relax} | % First colum
p{\dimexpr(\columnwidth)*33/100-2\tabcolsep-(2\arrayrulewidth/3)\relax} |
p{\dimexpr(\columnwidth)*33/100-\tabcolsep-(2\arrayrulewidth/3)\relax}
@{}
}

• Read leandriis comment, where the two problems are described, too wide formulas and wrong calculation of column widths. You should also subtract at least two tabcolseps from each of the columns. Then it will only be 0.8 pt too wide (two arrayrulewidths). – Sveinung Jul 21 at 10:59

I recommend tabu (2011-2019) which also combines longtable, tabularx, and other tabular functionalities. The same commands as above apply with only a change in the preamble:

\usepackage{tabu}
\begin{tabu} to \linewidth{l|X|X}
...
\end{tabu}


Edit: tabu now releases emergency bugfixes only and has had problems with longtable and colors and so I do not recommend it for these purposes.

• From the readme: "The package is not being actively maintained however any major required fixes may be reported to the repository and volunteers there (currently members of the LaTeX3 Team) will attempt to update the package with any fixes required." The point is that they are fixing breaks as much as possible now. Granted it's patching a leaky ship, but for personal work and one-time compilations I think it's fair to offer the suggestion. But I'll concede that in most cases -- especially longtables and colors -- it should not be recommended -- so I withdraw my suggestion here. – Kompootor Jul 23 at 14:05