# How to Repeat Cell Content on Next Page for \longtable using \multirow

I've created a LaTEX document that has several large tables that span multiple pages using long table. The table has several Topics (column 1) and Topics (column 2). The issue is that on the second page I have an empty column 1, but the subtopics are listed nicely in column 2. Can you tell me how to get "Topic 1" on to page 2 as well?

Here's some example code :

\documentclass[11pt]{article}
\usepackage{lipsum} % for dummy text only
\usepackage{multirow}
\usepackage{longtable}%Setup to allow tables to span multiple pages and repeat headers on
%next page
\setlength{\arrayrulewidth}{1.2pt}% Set all table lines thicker than their 0.4pt default

%--------------------Setup Margins ---------------------
\textheight 9in
\usepackage[hmargin=2cm,vmargin=4cm]{geometry}
\setlength{\headsep}{1pt}
\setlength{\footskip}{25pt} % 25pts

\begin{document}
\begin{centering}
\begin{longtable}{|p{3.2cm}|p{2.3cm}|p{10.5cm}|} \hline
\endfoot % force a footer here
\hline
\bf{Head 1} & \bf{Head 2} & \bf{Head 3}\\[5ex]   \hline \endhead

\multirow{3}{*}{Topic 1}
&Subtopic A  & There is some text here but could be small   \\ \cline{2-3}
& Subtopic B & \lipsum[1-3]  \\  \cline{2-3}
&  Subtopic C & \lipsum[1-2]   \\  \cline{2-3}
\hline
\multirow{3}{*}{Topic 2}
&Subtopic A  & \lipsum[1-3]   \\ \cline{2-3}
& Subtopic B & \lipsum[1-2]  \\  \cline{2-3}
&  Subtopic C & \lipsum[1-2]   \\  \cline{2-3}
\hline
\end{longtable}
\end{centering}
\end{document}

-
Welcome to TeX.sx! A tip: If you indent lines by 4 spaces, they'll be marked as a code sample. You can also highlight the code and click the "code" button (with "{}" on it). –  percusse Oct 26 '12 at 7:51
add comment

## 1 Answer

The following example implements the logic: Each subtopic has a topic. If a subtopic of a topic follows a subtopic of the same topic on the same page, then the topic is suppressed. That means that the topic is repeated for the first subtopic on a new page, if necessary.

Each topic in the first column is set by \topic{...}. Repeated topics can be abbreviated by \lasttopic. The formatting of the topic can be changed by redefining \topicformat.

\documentclass[11pt]{article}
\usepackage{lipsum} % for dummy text only
\usepackage{longtable}
\setlength{\arrayrulewidth}{1.2pt}

\usepackage[
hmargin=2cm,
vmargin=4cm,
headsep=1pt,
footskip=25pt,
]{geometry}

\usepackage{zref-abspage}[2010/03/26]

\makeatletter
% Counter topic@label' for automatic generation of label names
\newcounter{topic@label}
\renewcommand*{\thetopic@label}{topic@\the\value{topic@label}}

% \topic@previous: Macro for remembering the previous topic
\global\let\topic@previous\relax
\global\let\lasttopic\relax
\newcommand*{\topic}[1]{%
\begingroup
\def\topic@put{\topicformat{#1}}%
% Remember label name of previous topic
\edef\topic@previouslabel{\thetopic@label}%
% Set label to remember the page position
\stepcounter{topic@label}%
\zref@labelbyprops{\thetopic@label}{abspage}%
% Compare topic with previous topic
\def\topic@current{#1}%
\ifx\topic@current\topic@previous
% Check, whether is the previous topic with same name is
% on the same page.
\zifrefundefined{\topic@previouslabel}{%
\topic@put
}{%
\zifrefundefined{\thetopic@label}{%
\topic@put
}{%
\ifnum\zref@extractdefault{\topic@previouslabel}{abspage}{0}=%
\zref@extractdefault{\thetopic@label}{abspage}\relax
\else
\topic@put
\fi
}%
}%
\else
% New topic is always set
\topic@put
\fi
% Remember this topic as previous topic for next topic
\global\let\topic@previous\topic@current
\endgroup
\gdef\lasttopic{\topic{#1}}%
}
% Macro \topicformat formats the topic
\newcommand*{\topicformat}[1]{#1}
\makeatother

\begin{document}
\begin{longtable}{|p{3.2cm}|p{2.3cm}|p{10.5cm}|}
\hline
\endfoot
\hline
\bfseries Head 1 & \bfseries Head 2 & \bfseries Head 3\\[5ex]
\hline
\endhead
\topic{Topic 1}
& Subtopic A  & There is some text here but could be small \\ \cline{2-3}
\lasttopic
& Subtopic B & \lipsum[1-3]  \\  \cline{2-3}
\lasttopic
& Subtopic C & \lipsum[1-2]   \\  \cline{2-3}
\hline
\topic{Topic 2}
& Subtopic A  & \lipsum[1-3]   \\ \cline{2-3}
\lasttopic
& Subtopic B & \lipsum[1-2]  \\  \cline{2-3}
\lasttopic
&  Subtopic C & \lipsum[1-2]   \\  \cline{2-3}
\hline
\end{longtable}
\end{document}


Remarks:

• Setting of \textheight is overwritten by the page layout setting via geometry.
• \bf is outdated (LaTeX2.09), LaTeX (LaTeX2e) uses \bfseries or \textbf. Both \bf and \bfseries do not have an argument. The setting is active until the current group (table cell in this case) ends.
• center is an environment, \centering is a command. In this case both are not needed, because the longtable is centered horizontally by default.
• I have removed multirow`, the positioning of the topic somewhere between the lines looked too odd, IMHO.
-
Thanks so much this is awesome, exactly what I was trying to achieve. I do notice I had to compile twice to get rid of Topic 1 on top of Topic 1 (i.e. it put it twice in page 1). So it must use the aux file to create the counter. Is there a way around this (different Chunk size for example) that would get it sorted on the first compile? The only reason I ask this is because this is run off an online server and it is a little bit slow to compile twice.... Thank you very much for this - it's a huge help! Dan –  Dan Oct 26 '12 at 17:09
@Dan Yes, references always need two runs. There is no way around, because, when the table row is typeset, it is not known, on which page the table row might be put. –  Heiko Oberdiek Oct 26 '12 at 18:58
add comment