3

I'm trying to fix a table in my latex file. The table is too long and few last rows go over the number of pages and also partially into the footnote. I thought of using longtable package to break the table and extend over the second page (see example 1). However, once I do it I run into another problem because the text in the right column goes beyond the \textwidth. No clue how to overcome this problem.

In the end, I would prefer to have the big table stretched over two pages with the header of the table also visible on the second page. I would appreciate if someone could help me out here.

Here is my code:

\usepackage{booktabs}   
\usepackage[table,xcdraw]{xcolor}
\usepackage{longtable}
\newlength{\asdf} 
\setlength{\asdf}{\textwidth}
\addtolength{\asdf}{-8\tabcolsep}

\usepackage{tabularx}
\usepackage{ltablex}
\usepackage{caption}
\usepackage{array}

\begin{tabularx}{\textwidth}{@{}lX@{}}
\toprule
\textbf{YYYYYYYYYYYY}  & \textbf{ZZZZZZZZZZZZZZ} \\
\midrule
0000 & aaaa $\&$ bbbbb; aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb; \\
0000 & aaaa $\&$ bbbbb; aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb; \\
0000 & aaaa $\&$ bbbbb; aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb; \\
0000 & aaaa $\&$ bbbbb; aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaa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aa $\&$ bbbbb;aaaa $\&$ bbbbb; \\
0000 & aaaa $\&$ bbbbb; aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb; \\
 0000 & aaaa $\&$ bbbbb; aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb; \\
\bottomrule
\end{tabularx}
\end{table} 

Example 1

5

Here you are. With ltablex, the syntax is the same as for longtable. In particular, you don't nest tabularx in a table environment, and the \caption is incorporated to the first head.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{ltablex}
\usepackage{caption, booktabs}

\begin{document}

\begin{tabularx}{\linewidth}{@{}lX@{}}
\caption{My caption} \\
\toprule
\textbf{YYYYYYYYYYYY} & \textbf{ZZZZZZZZZZZZZZ} \\
\midrule
\endfirsthead
\toprule
\textbf{YYYYYYYYYYYY} & \textbf{ZZZZZZZZZZZZZZ} \\
\midrule
\endhead
\midrule
\multicolumn{2}{r}{\footnotesize( To be continued)}
\endfoot
\bottomrule
\endlastfoot
0000 & aaaa $\&$ bbbbb; aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb; \\
0000 & aaaa $\&$ bbbbb; aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $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$\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb; \\
0000 & aaaa $\&$ bbbbb; aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb; \\
0000 & aaaa $\&$ bbbbb; aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb; \\
0000 & aaaa $\&$ bbbbb; aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb; \\
 0000 & aaaa $\&$ bbbbb; aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb; \\
 0000 & aaaa $\&$ bbbbb; aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;
\end{tabularx}

\end{document} 

enter image description here

  • The (To be continued) part that you included in the code is something that looks really good, to my surprise. Thank you for that – Jo-Achna Aug 20 '18 at 10:01
  • I just realized I might have another question. Since my table is incorporated in the text it looks like the float is not helping me to determine the position of the table. I tried to add [t] in order to start the table at the top of the page: \begin{tabularx}{\linewidth}[t]{@{}lcX@{}} but the table doesn't move an inch. Any idea why and what can I do? – Jo-Achna Aug 22 '18 at 12:48
  • @Jo-Achna: To have it float it must be inserted in a table environment: begin{table}[!t]\begin{tabularx}.... The [t] that goes with tabularx denotes the alignment w.r.t. the current line. – Bernard Aug 22 '18 at 12:57
  • I thought so but once doing it I receive the warning message Text page 14 contains only floats. It looks like once one problem is solved another one appears. – Jo-Achna Aug 22 '18 at 13:10
  • That's probably because you have too many floats on this page. Normally some should go to the next page. See if this link helps. – Bernard Aug 22 '18 at 13:18
4

A table that is inside a table environment can not be broken onto several pages, which is why the table in your example image is continued and overlaps with the footnote and page number.

In order to get a table that breaks across pages and has the variable with X type columns from the tabularx package, you could use the ltablex package as shown in the following example. To repeat the header on all following pages, you can use the \endhead command as shown in the example:

\documentclass{article}

\usepackage{booktabs}   
\usepackage{ltablex}


\begin{document}

\begin{tabularx}{\textwidth}{@{}lX@{}}
\toprule
\textbf{YYYYYYYYYYYY}  & \textbf{ZZZZZZZZZZZZZZ} \\
\midrule
\endhead
0000 & aaaa $\&$ bbbbb; aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb; \\
0000 & aaaa $\&$ bbbbb; aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb; \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 bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb; \\
0000 & aaaa $\&$ bbbbb; aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb; \\
0000 & aaaa $\&$ bbbbb; aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb; \\
 0000 & aaaa $\&$ bbbbb; aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb; \\
  0000 & aaaa $\&$ bbbbb; aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb;aaaa $\&$ bbbbb; \\
\bottomrule
\end{tabularx}


\end{document}

enter image description here

  • Very good. I not knew ltablex package. +1. – Sebastiano Aug 18 '18 at 20:56
  • The answer is exactly what I was looking for. Great! Thank you! – Jo-Achna Aug 20 '18 at 6:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.