# Grouping table rows while having page breaks in a long table in Latex

I have a long table spread over a few pages, made of groups of rows. I use the packages longtable, booktabs, multirow and multicol, and have read their documentation, but could not find answers to my problem. For mwe, see below.

I have two (related) problems with this, all about the page breaks:

1. The page break should not happen within a group of rows, as happens now on pages 1 and 2. I have a workaround of putting \nopagebreak behind every line, but I hope that there is a smarter solution.
2. I separate my groups or rows with \midrule, but if these groups are already separated by a page break, I don't need \midrule anymore, it makes an ugly extra line, see the top of page 4.

Is there a way to "group" my rows in a way that latex understands, so page breaks do not fall in the middle of a multirow, and that midrules separating these multirows are not used around pagebreaks?

\documentclass[a5paper,12pt]{article}% smaller paper size to show the problem
\usepackage{longtable}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{multicol}
\begin{document}
This is a test.
\begin{longtable}{lll}
\caption{Table to show problem}\\
\toprule
Year & Category & Used\\
\midrule
\multicolumn{3}{c}%
{\tablename\ \thetable\ -- \textit{Continuation of previous page}} \\
\toprule
Year & Category & Used \\
\midrule
\hline \multicolumn{3}{r}{\textit{Table is continued on next page}} \\
\endfoot
\bottomrule
\endlastfoot
1933--1939 & 1 & Yes\\
\midrule
\multirow{2}{*}{1947--1948}
& 1 & Yes\\
& 2 & Some\\
\midrule
\multirow{3}{*}{1949--1961}
& 1 & Yes\\
& 2 & No\\
& 3 & No\\
\midrule
\multirow{4}{*}{1962--1970}
& 1 & Yes\\
& 2 & Some\\
& 3 & Some\\
& 4 & Yes\\
\midrule
\multirow{4}{*}{1971--1974}
& 1 & No\\
& 2 & Some\\
& 3 & No\\
& 4 & Yes\\
\midrule
\multirow{5}{*}{1975--1979}
& HC & No\\
& 1 & No\\
& 2 & Yes\\
& 3 & Some\\
& 4 & Yes\\
\midrule
\multirow{5}{*}{1980--1982}
& HC & Yes\\
& 1 & Yes\\
& 2 & Some\\
& 3 & Yes\\
& 4 & Yes\\
\midrule
\multirow{5}{*}{1983--1984}
& HC & Yes\\
& 1 & No\\
& 2 & Some\\
& 3 & Yes\\
& 4 & Yes\\
\midrule
\multirow{5}{*}{1985--1986}
& HC & Some\\
& 1 & Yes\\
& 2 & Yes\\
& 3 & Yes\\
& 4 & Yes\\
\midrule
\multirow{5}{*}{1987--1989}
& HC & Yes\\
& 1 & Some\\
& 2 & Yes\\
& 3 & Yes\\
& 4 & Yes\\
\midrule
\multirow{5}{*}{1990--2003}
& HC & Yes\\
& 1 & Some\\
& 2 & Yes\\
& 3 & Yes\\
& 4 & Yes\\
\midrule
\multirow{5}{*}{2004--2010}
& HC & Yes\\
& 1 & Some\\
& 2 & Yes\\
& 3 & Yes\\
& 4 & Yes\\
\midrule
\multirow{5}{*}{2011}
& HC & Yes\\
& 1 & Yes\\
& 2 & Some\\
& 3 & Yes\\
& 4 & Yes\\
\midrule
\multirow{5}{*}{2012--2016}
& HC & Yes\\
& 1 & Yes\\
& 2 & Yes\\
& 3 & Yes\\
& 4 & Some\\
\midrule
\multirow{5}{*}{2017--2018}
& HC & Yes\\
& 1 & Yes\\
& 2 & Some\\
& 3 & Some\\
& 4 & Yes\\
\end{longtable}
\end{document}

• you can use \\* to suppress page breaks (on a good day) – David Carlisle Jan 4 at 16:39
• @DavidCarlisle: It is apparently a good day, your comment put me on the way to a solution for my problem. I will self-answer shortly. – user178854 Jan 4 at 19:32

My first problem was solved by putting \\* after every row in the table, as suggested in the comments. My second problem now became that every "Table is continued on next page" had two lines above them, and this was easily solved by removing the \hline above this text.

This gave the following code, which did exactly what I wanted:

\documentclass[a5paper,12pt]{article}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{multicol}
\begin{document}
This is a test.
\begin{longtable}{lll}
\caption{Table to show solution}\\
\toprule
Year & Category & Used\\
\midrule
\multicolumn{3}{c}%
{\tablename\ \thetable\ -- \textit{Continuation of previous page}} \\
\toprule
Year & Category & Used \\
\midrule
\multicolumn{3}{r}{\textit{Table is continued on next page}} \\
\endfoot
\bottomrule
\endlastfoot
1933--1939 & 1 & Yes\\*
\midrule
\multirow{2}{*}{1947--1948}
& 1 & Yes\\*
& 2 & Some\\*
\midrule
\multirow{3}{*}{1949--1961}
& 1 & Yes\\*
& 2 & No\\*
& 3 & No\\*
\midrule
\multirow{4}{*}{1962--1970}
& 1 & Yes\\*
& 2 & Some\\*
& 3 & Some\\*
& 4 & Yes\\*
\midrule
\multirow{4}{*}{1971--1974}
& 1 & No\\*
& 2 & Some\\*
& 3 & No\\*
& 4 & Yes\\*
\midrule
\multirow{5}{*}{1975--1979}
& HC & No\\*
& 1 & No\\*
& 2 & Yes\\*
& 3 & Some\\*
& 4 & Yes\\*
\midrule
\multirow{5}{*}{1980--1982}
& HC & Yes\\*
& 1 & Yes\\*
& 2 & Some\\*
& 3 & Yes\\*
& 4 & Yes\\*
\midrule
\multirow{5}{*}{1983--1984}
& HC & Yes\\*
& 1 & No\\*
& 2 & Some\\*
& 3 & Yes\\*
& 4 & Yes\\*
\midrule
\multirow{5}{*}{1985--1986}
& HC & Some\\*
& 1 & Yes\\*
& 2 & Yes\\*
& 3 & Yes\\*
& 4 & Yes\\*
\midrule
\multirow{5}{*}{1987--1989}
& HC & Yes\\*
& 1 & Some\\*
& 2 & Yes\\*
& 3 & Yes\\*
& 4 & Yes\\*
\midrule
\multirow{5}{*}{1990--2003}
& HC & Yes\\*
& 1 & Some\\*
& 2 & Yes\\*
& 3 & Yes\\*
& 4 & Yes\\*
\midrule
\multirow{5}{*}{2004--2010}
& HC & Yes\\*
& 1 & Some\\*
& 2 & Yes\\*
& 3 & Yes\\*
& 4 & Yes\\*
\midrule
\multirow{5}{*}{2011}
& HC & Yes\\*
& 1 & Yes\\*
& 2 & Some\\*
& 3 & Yes\\*
& 4 & Yes\\*
\midrule
\multirow{5}{*}{2012--2016}
& HC & Yes\\*
& 1 & Yes\\*
& 2 & Yes\\*
& 3 & Yes\\*
& 4 & Some\\*
\midrule
\multirow{5}{*}{2017--2018}
& HC & Yes\\*
& 1 & Yes\\*
& 2 & Some\\*
& 3 & Some\\*
& 4 & Yes\\*
\end{longtable}
\end{document}