[Tex/LaTex] Longtabu still not able to shrink text to textwidth even after using X columns

longtablelongtabumulticolumnwidth

My problem is very similar to this one:

Longtabu not able to shrink text to textwidth

where the table goes outside the page, even after I explicitly set the width to \textwidth. I also followed the solution from David Carlisle and used X columns to allow line breaking within the cell, but the problem still persists.

I suspect that the problem is whenever using the \multicolumn command, since the other \longtabu tables on my document work just fine.

Here is a MWE:

\documentclass[a4paper,10pt,oneside,openany]{book} 

\usepackage[utf8]{inputenc} % allow specify input encoding
\usepackage[margin=1in]{geometry} % page dimensions
\usepackage{tabu} % tables that take more than 1 page 
\usepackage{longtable} % tabu needs this to be loaded 

\begin{document} 

\begin{longtabu} to \textwidth {| X[1,c,m]| X[1,c,m]| X[1,c,m]| X[1,c,m]| X[1,c,m]| X[1,c,m]| X[1,c,m]| X[1,c,m]| X[1,c,m]| X[1,c,m]| X[1,c,m]| X[1,c,m]| X[1,c,m]| X[1,c,m]| X[1,c,m]| X[1,c,m]|} 
\hline 
\multicolumn{16}{|c|}{ \bf Title of table goes here  } \endhead 
\hline 
\multicolumn{8}{| c |}{ Text  } & 
\multicolumn{2}{| c |}{ Some long text here  } & 
\multicolumn{2}{| c |}{ Some long text here  } & 
\multicolumn{2}{| c |}{ Some long text here  } & 
\multicolumn{2}{| c |}{ Some long text here  } \\
\hline 
{ 15  } & 
{ 14  } & 
{ 13  } & 
{ 12  } & 
{ 11  } & 
{ 10  } & 
{ 9  } & 
{ 8  } & 
{ 7  } & 
{ 6  } & 
{ 5  } & 
{ 4  } & 
{ 3  } & 
{ 2  } & 
{ 1  } & 
{ 0  } \\ 
\hline 
\end{longtabu} 


\end{document} 

Any help is really appreciated! Thanks.

Best Answer

It's easier to see the required width for the spanning entries if you calculate the columns widths directly. In which case you don't need X at allm so can use longtable rather than the wrapper provided by tabu.

Also, don't use \bf and only use | to the left of the first column. If you use two | in all the \multicolumn entries then you get two adjacent rules, one in the right of a column and a touching one at the left of the next column.

The arithmetic just takes account that a p column takes up space equal to the supplied width plus \tabcolsep space either side of the column, plus the width of any vertical rules.

enter image description here

\documentclass[a4paper,10pt,oneside,openany]{book} 

\usepackage[utf8]{inputenc} % allow specify input encoding
\usepackage[margin=1in]{geometry} % page dimensions

\usepackage{array,longtable} 
\newlength\mylength
\setlength\mylength{\dimexpr(\textwidth-17\arrayrulewidth-32\tabcolsep)/16\relax}
\begin{document} 

\begin{longtable}{|*{16}{>{\centering\arraybackslash}m{\mylength}|}}
\hline 
\multicolumn{16}{|c|}{\textbf{Title of table goes here}}
\endhead 
\hline 
\multicolumn{8}{| c |}{ Text  } & 
\multicolumn{2}{>{\centering\arraybackslash}m{\dimexpr2\mylength+2\tabcolsep+\arrayrulewidth}|}{ Some long text here  } & 
\multicolumn{2}{>{\centering\arraybackslash}m{\dimexpr2\mylength+2\tabcolsep+\arrayrulewidth}|}{ Some long text here  } & 
\multicolumn{2}{>{\centering\arraybackslash}m{\dimexpr2\mylength+2\tabcolsep+\arrayrulewidth}|}{ Some long text here  } & 
\multicolumn{2}{>{\centering\arraybackslash}m{\dimexpr2\mylength+2\tabcolsep+\arrayrulewidth}|}{ Some long text here  }\\
\hline 
{ 15  } & 
{ 14  } & 
{ 13  } & 
{ 12  } & 
{ 11  } & 
{ 10  } & 
{ 9  } & 
{ 8  } & 
{ 7  } & 
{ 6  } & 
{ 5  } & 
{ 4  } & 
{ 3  } & 
{ 2  } & 
{ 1  } & 
{ 0  } \\ 
\hline 
\end{longtable} 


\end{document} 
Related Question