3

I created a table in LaTeX with longtable, but there are some problems. The table doesn't fit well between the two pages plus vertical lines are discontinued. Here is the table code:

\begin{longtable}[H]{|p{4cm}|p{3cm}|c|}
    %\begin{tabular}{|p{4cm}|p{3cm}|c|}
    \toprule
 & \textbf{Catégorie des  caractéristiques} & \textbf{Caractéristiques} \\
    \midrule
    \multirow{10}[20]{*}{\parbox{4cm}{Analyses sur World of Warcraft [WoW]}} & \multirow{5}[10]{*}{\textbf{Composition}} & Nombre de membres \\
\cmidrule{3-3} & & Entropie de distribution \\
\cmidrule{3-3} & & Pourcentage des classes présentes \\
\cmidrule{3-3} & & Longueur d’existence \\
\cmidrule{3-3} & & Écart-type des effectifs \\
\cmidrule{2-3} & \multirow{2}[4]{*}{\textbf{Stats du jeu}} & Temps de collaboration moyen \\
\cmidrule{3-3} & & Temps moyen de jeu dans un groupe \\
\cmidrule{2-3} & \multirow{3}[6]{*}{\textbf{Structurelles}} & Entropie de distribution du degré \\
\cmidrule{3-3} & & Coefficient moyen de Clustering dans un groupe \\
\cmidrule{3-3} & & Coefficient moyen de Clustering \\
    \midrule
    \multirow{10}[20]{*}{\textbf{Analyses sur DBLP}} & \multirow{2}[4]{*}{\textbf{Groupe}} & Nombre de membres \\
\cmidrule{3-3} & & Prolificité moyenne \\
\cmidrule{2-3} & \multirow{7}[14]{*}{\textbf{Activités}} & Nombre total des publications dans un groupe \\
\cmidrule{3-3} & & Nombre total des collaborations dans un groupe \\
\cmidrule{3-3} & & Nombre total des publications dans un groupe \\
\cmidrule{3-3} & & Nombre total des  publications en dehors un groupe \\
\cmidrule{3-3} & & Nombre moyen des collaborations dans un groupe \\
\cmidrule{3-3} & & Nombre total des collaborations en dehors un groupe \\
\cmidrule{3-3} & & Coefficient moyen de fidélité des membres \\
\cmidrule{2-3} & \textbf{Structurelles} & Entropie de la distribution des degrés \\
    \bottomrule
%\end{table}%
\end{longtable}

Here is the result that I'm getting:

enter image description here

I want the table to be on two pages but with complete cells on each page (no cell divided). And with continued vertical lines.

  • 6
    please read the documentation of the booktabs package that you are using. by design it is incompatible with vertical rules and the package author explains at some length why you should not use vertical rules in tables. – David Carlisle Jan 4 '17 at 18:37
  • 3
    As far as I know longtable cannot have page breaks inside cells, so that part is not really a problem. – Torbjørn T. Jan 4 '17 at 18:41
  • How do you explain the result I got then? And more importantly, how can I fix it? – Joker 00 Jan 4 '17 at 18:42
  • @TorbjørnT. I think he means the combined cells on the left side of the table. They are cells that are created using the multirow command as far as I can tell. – Dr. Manuel Kuehner Jan 6 '17 at 10:28
  • 1
    @Dr.ManuelKuehner Yes, I understood as much from one of the answers. I didn't see the multirows. – Torbjørn T. Jan 6 '17 at 10:31
6

The star form \\* can be used in longtable to prevent page breaks after the row. However, lines are still breakpoints. Therefore and because of a nicer layout, the following example only uses horizontal lines sparingly. The main sections are separated by a \midrule, but the sub sections of the table are separated by space \addlinespace.

Then the remaining breakpoints are right behind the horizontal lines. The example takes some effort to the page break. \midrule has a space \aboverulesep above and \belowrulesep below the rule. Since the page break is supposed to be at \midrule, the header and footers are adjusted accordingly.

Full example:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{lipsum}
\usepackage[hmargin=2cm]{geometry}

\usepackage{array}
\usepackage{booktabs}
\usepackage{longtable}

\begin{document}
\vspace*{.6\textheight}

% Multi-line left-aligned text with manual line breaks.
% The base line of the whole is at the top row.
\newcommand*{\tstack}[1]{%
  \begingroup
    \renewcommand*{\arraystretch}{1}%
    \begin{tabular}[t]{@{}l@{}}#1\end{tabular}%
  \endgroup
}
\begin{longtable}{>{\bfseries}l>{\bfseries}ll}
  \caption{Légende de table}\\
  \toprule
 & \tstack{Catégorie des \\ caractéristiques} & \bfseries Caractéristiques \\
  \midrule
\endfirsthead
  \noalign{\kern-\aboverulesep}
  \midrule
\endhead
  \noalign{\kern-\belowrulesep}
\endfoot
  \bottomrule
\endlastfoot
  \smash{\tstack{Analyses sur World \\ of Warcraft [WoW]}}
  & Composition & Nombre de membres \\*
  & & Entropie de distribution \\*
  & & Pourcentage des classes présentes \\*
  & & Longueur d’existence \\*
  & & Écart-type des effectifs \\*
  \addlinespace
  & Stats du jeu & Temps de collaboration moyen \\*
  & & Temps moyen de jeu dans un groupe \\*
  \addlinespace
  & Structurelles & Entropie de distribution du degré \\*
  & & Coefficient moyen de Clustering dans un groupe \\*
  & & Coefficient moyen de Clustering \\*
  \midrule
  Analyses sur DBLP & Groupe & Nombre de membres \\*
  & & Prolificité moyenne \\*
  \addlinespace
  & Activités & Nombre total des publications dans un groupe \\*
  & & Nombre total des collaborations dans un groupe \\*
  & & Nombre total des publications dans un groupe \\*
  & & Nombre total des  publications en dehors un groupe \\*
  & & Nombre moyen des collaborations dans un groupe \\*
  & & Nombre total des collaborations en dehors un groupe \\*
  & & Coefficient moyen de fidélité des membres \\*
  \addlinespace
  & Structurelles & Entropie de la distribution des degrés \\*
\end{longtable}
\end{document}

First page

Page 1

and second page

Page 2

Caption below the table

Usually table captions are read first and the tables are usually in reading order. Therefore, table captions are put above the table. Also, longtable assumes this order.

Putting the table at the end of a multi-page table is much more cumbersome:

  • Vertical spacing around the caption needs to be fixed.
  • The entry in the list of tables should show to the first page of the table. When it points to the the caption of the table, then the reader would be directed to the end of the table. The example adds a workaround to get the page number of the first page into the list of tables.
  • Similar problems are the references. The example uses two labels at the first and last page of the table.

Full example:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{lipsum}
\usepackage[hmargin=2cm]{geometry}

\usepackage{array}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{multirow}

\begin{document}
\listoftables
\section{First section}
Table~\ref{tab:ref:MyLabel} starts at page~\pageref{tab:page:MyLabel}
and ends at page~\pageref{tab:ref:MyLabel}.
\newpage

\vspace*{.6\textheight}

\newcommand*{\tstack}[1]{%
  \begingroup
    \renewcommand*{\arraystretch}{1}%
    \begin{tabular}[t]{@{}l@{}}#1\end{tabular}%
  \endgroup
}
\xdef\BaseLineSkip{\the\baselineskip}
\def\TableCaption{Légende de table}
\begin{longtable}{>{\bfseries}l>{\bfseries}ll}
  % \caption{Légende de table}\\
  \noalign{%
    \label{tab:page:MyLabel}%
    \addcontentsline{lot}{table}{\numberline{\thetable}\TableCaption}%
  }%
  \toprule
 & \tstack{Catégorie des \\ caractéristiques} & \bfseries Caractéristiques \\
  \midrule
\endfirsthead
  \noalign{\kern-\aboverulesep}
  \midrule
\endhead
  \noalign{\kern-\belowrulesep}
\endfoot
  \bottomrule
  % Caption with workaround for the vertical spacing
  \noalign{\vskip\BaseLineSkip}
  \caption[]{\TableCaption}\label{tab:ref:MyLabel}\\[-\BaseLineSkip]
\endlastfoot
  \smash{\tstack{Analyses sur World \\ of Warcraft [WoW]}}
  & Composition & Nombre de membres \\*
  & & Entropie de distribution \\*
  & & Pourcentage des classes présentes \\*
  & & Longueur d’existence \\*
  & & Écart-type des effectifs \\*
  \addlinespace
  & Stats du jeu & Temps de collaboration moyen \\*
  & & Temps moyen de jeu dans un groupe \\*
  \addlinespace
  & Structurelles & Entropie de distribution du degré \\*
  & & Coefficient moyen de Clustering dans un groupe \\*
  & & Coefficient moyen de Clustering \\*
  \midrule
  Analyses sur DBLP & Groupe & Nombre de membres \\*
  & & Prolificité moyenne \\*
  \addlinespace
  & Activités & Nombre total des publications dans un groupe \\*
  & & Nombre total des collaborations dans un groupe \\*
  & & Nombre total des publications dans un groupe \\*
  & & Nombre total des  publications en dehors un groupe \\*
  & & Nombre moyen des collaborations dans un groupe \\*
  & & Nombre total des collaborations en dehors un groupe \\*
  & & Coefficient moyen de fidélité des membres \\*
  \addlinespace
  & Structurelles & Entropie de la distribution des degrés \\*
\end{longtable}
\end{document}

First page

Page 1

second page

Page 2

and third page

Page 3

  • But this is how the table should look like! :-) – Sveinung Jan 4 '17 at 20:28
  • This is awesome. Would you please tell me how to add a caption under the table? I am adding it using \caption but it shows inside the table. – Joker 00 Jan 4 '17 at 20:51
  • @Joker00 Done. Usually table captions are put above the table, because usually tables are read in the normal reading order and the caption is read first. – Heiko Oberdiek Jan 4 '17 at 20:53
  • I know but our reports captions are usually placed below. Isn't there a way? – Joker 00 Jan 4 '17 at 20:55
  • @Joker00 See updated answer. – Heiko Oberdiek Jan 4 '17 at 21:25
2

Your table occupy aproximately 2/3 of page, so it not need to be set as long table. As longtable you have problems with broke multirow cells which can result with spurious empty cells on the next page and not centered content position an previous page.

Regarding use of rules from booktas package you should consider above comments, however you can use it if delete extra verticals pace above and below of rules and additional space define by \makegapedcells macro from `makecell˙package.

In multi line multirow is not necessary to have parbox. Recent version of multirow package offer option {=} which in multirow cells perserve column formatting.

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{array,booktabs,makecell,multirow,longtable}
\renewcommand\theadfont{\bfseries\normalsize}

\begin{document}
\begin{table}[htb]
    \setlength\aboverulesep{0pt}
    \setlength\belowrulesep{0pt}
    \setcellgapes{3pt}
    \makegapedcells
\begin{tabular}{|>{\bfseries}p{3cm}|>{\bfseries}p{3cm}|c|}
    \toprule
               & \thead[b]{Catégorie des\\  caractéristiques} 
                 & \thead[b]{Caractéristiques} \\
    \midrule
\multirow{10}{=}[-3em]{Analyses sur World of Warcraft [WoW]}
               & \multirow{5}{=}[-2em]{Composition} 
                 & Nombre de membres \\
\cmidrule{3-3} & & Entropie de distribution \\
\cmidrule{3-3} & & Pourcentage des classes présentes \\
\cmidrule{3-3} & & Longueur d’existence \\
\cmidrule{3-3} & & Écart-type des effectifs \\
\cmidrule{2-3} & \multirow{2}{=}[-1ex]{Stats du jeu} 
                 & Temps de collaboration moyen \\
\cmidrule{3-3} & & Temps moyen de jeu dans un groupe \\
\cmidrule{2-3} & \multirow{3}{=}[-1em]{Structurelles} 
                 & Entropie de distribution du degré \\
\cmidrule{3-3} & & Coefficient moyen de Clustering dans un groupe \\
\cmidrule{3-3} & & Coefficient moyen de Clustering \\
    \midrule
\multirow{10}{=}[-4em]{Analyses sur DBLP} 
               & \multirow{2}{=}[-1ex]{Groupe} 
                  & Nombre de membres \\
\cmidrule{3-3} & & Prolificité moyenne \\
\cmidrule{2-3} & \multirow{7}{=}[-3em]{Activités} 
                 & Nombre total des publications dans un groupe \\
\cmidrule{3-3} & & Nombre total des collaborations dans un groupe \\
\cmidrule{3-3} & & Nombre total des publications dans un groupe \\
\cmidrule{3-3} & & Nombre total des  publications en dehors un groupe \\
\cmidrule{3-3} & & Nombre moyen des collaborations dans un groupe \\
\cmidrule{3-3} & & Nombre total des collaborations en dehors un groupe \\
\cmidrule{3-3} & & Coefficient moyen de fidélité des membres \\
\cmidrule{2-3} & Structurelles
                 & Entropie de la distribution des degrés \\
    \bottomrule
\end{tabular}
    \end{table}
\end{document}

enter image description here

Addendum: In case, that you persist to have longtable and accept empty vertical space when manual breaking of table (as suggested in Sveinung answer), then the MWE is

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{array,booktabs,makecell,multirow,longtable}
\renewcommand\theadfont{\bfseries\normalsize}
\usepackage{lipsum}

\begin{document}
\lipsum[1]
{
\setlength\aboverulesep{0pt}
\setlength\belowrulesep{0pt}
\setcellgapes{3pt}
\makegapedcells
    \begin{longtable}{|>{\bfseries}p{3cm}|>{\bfseries}p{3cm}|c|}
\caption{Example of long table. For final form of table it have to be complied at list twice.}
\label{table1}                                                      \\
    \toprule
               & \thead[b]{Catégorie des\\  caractéristiques} 
                 & \thead[b]{Caractéristiques} \\
    \midrule
\endfirsthead
    \toprule
               & \thead[b]{Catégorie des\\  caractéristiques}
                 & \thead[b]{Caractéristiques} \\
\endhead
    \bottomrule
\endfoot
    \bottomrule
\endlastfoot
\multirow{10}{=}[-3em]{Analyses sur World of Warcraft [WoW]}
               & \multirow{5}{=}[-2em]{Composition} 
                 & Nombre de membres \\
\cmidrule{3-3} & & Entropie de distribution \\
\cmidrule{3-3} & & Pourcentage des classes présentes \\
\cmidrule{3-3} & & Longueur d’existence \\
\cmidrule{3-3} & & Écart-type des effectifs \\
\cmidrule{2-3} & \multirow{2}{=}[-1ex]{Stats du jeu} 
                 & Temps de collaboration moyen \\
\cmidrule{3-3} & & Temps moyen de jeu dans un groupe \\
\cmidrule{2-3} & \multirow{3}{=}[-1em]{Structurelles} 
                 & Entropie de distribution du degré \\
\cmidrule{3-3} & & Coefficient moyen de Clustering dans un groupe \\
\cmidrule{3-3} & & Coefficient moyen de Clustering \\
    \pagebreak
\multirow{10}{=}[-4em]{Analyses sur DBLP} 
               & \multirow{2}{=}[-1ex]{Groupe} 
                  & Nombre de membres \\
\cmidrule{3-3} & & Prolificité moyenne \\
\cmidrule{2-3} & \multirow{7}{=}[-3em]{Activités} 
                 & Nombre total des publications dans un groupe \\
\cmidrule{3-3} & & Nombre total des collaborations dans un groupe \\
\cmidrule{3-3} & & Nombre total des publications dans un groupe \\
\cmidrule{3-3} & & Nombre total des  publications en dehors un groupe \\
\cmidrule{3-3} & & Nombre moyen des collaborations dans un groupe \\
\cmidrule{3-3} & & Nombre total des collaborations en dehors un groupe \\
\cmidrule{3-3} & & Coefficient moyen de fidélité des membres \\
\cmidrule{2-3} & Structurelles
                 & Entropie de la distribution des degrés \\
    \end{longtable}
\end{document}

which (seeing by my eyes) gives ugly result:

enter image description here

  • You have to assume that Joker00 has some text before the table, so his intention is to place the table at a specific place, not as a float, which longtabledoes. And then you need either two tables, or a longtable – Sveinung Jan 4 '17 at 19:41
  • @Sveinung, I like to discourage OP to use longtable if it can be placed on less than 2/3 of page text area. )I'm aware of possibilities of manual breaking long table, but this is, to my opinion of course, rude solution. What can happen with it, I show in addendum to my answer (beside of proper settings of longtable). To my opinion is better to move table on the next page than have empty vertical space after point of table breaking. Of course, if table is longer than one page, than you had to stick with long table with all shoved consequences. – Zarko Jan 4 '17 at 19:58
  • From typographical view, I agree. However, you should inform of such decisions, because the question was regarding longtable. And the ubliness depends of how much text goes before the table. – Sveinung Jan 4 '17 at 20:10
  • Thank you @Zarko and @Sveinung. I don't know why your code gives multiple errors about missing number: treated like zero and text is not well aligned. 4.1m.yt/tCutmV2.png 3.1m.yt/qAPRzIn.png – Joker 00 Jan 4 '17 at 20:18
2
  1. Start by removing booktabs from your preamble. If you are going to make Word like tabulars, booktabsis of no use at all.
  2. Remove the [H] as optional parametre to longtabs (it is wrong)
  3. Remove all the booktab specific rule commands in your tabular (toprule, midrule, cmdrule etc). Replace with the standard hrule, crule from tabular
  4. Place a \pagebreak command before the second \multirow command to force a break before the vertically spanned cells. When you are using multirow, what looks like one large cell, is not one large cell, but the number of cells that multirow spans.

Here is an MWE, which gives a ugly word-like tabular with the vertical lines preserved. The message is: Do not use vertical lines in a tabular, it is unnecessary and ugly:

\documentclass{scrartcl}
\usepackage{longtable,multirow}

\begin{document}

This is a text with a lot of meaning, but you probably will not understand that. This is a text with a lot of meaning, but you probably will not understand that.
This is a text with a lot of meaning, but you probably will not understand that.
This is a text with a lot of meaning, but you probably will not understand that.


This is a text with a lot of meaning, but you probably will not understand that.
This is a text with a lot of meaning, but you probably will not understand that. This is a text with a lot of meaning, but you probably will not understand that. This is a text with a lot of meaning, but you probably will not understand that.

This is a text with a lot of meaning, but you probably will not understand that. This is a text with a lot of meaning, but you probably will not understand that. This is a text with a lot of meaning, but you probably will not understand that. This is a text with a lot of meaning, but you probably will not understand that.

This is a text with a lot of meaning, but you probably will not understand that. This is a text with a lot of meaning, but you probably will not understand that. This is a text with a lot of meaning, but you probably will not understand that. This is a text with a lot of meaning, but you probably will not understand that.

This is a text with a lot of meaning, but you probably will not understand that. This is a text with a lot of meaning, but you probably will not understand that. This is a text with a lot of meaning, but you probably will not understand that. This is a text with a lot of meaning, but you probably will not understand that.

This is a text with a lot of meaning, but you probably will not understand that. This is a text with a lot of meaning, but you probably will not understand that. This is a text with a lot of meaning, but you probably will not understand that. This is a text with a lot of meaning, but you probably will not understand that. 

This is a text with a lot of meaning, but you probably will not understand that. This is a text with a lot of meaning, but you probably will not understand that. This is a text with a lot of meaning, but you probably will not understand that. This is a text with a lot of meaning, but you probably will not understand that.

\begin{longtable}[c]{|p{4cm}|p{3cm}|c|}
    %\begin{tabular}{|p{4cm}|p{3cm}|c|}

\hline
 & \textbf{Catégorie des  caractéristiques} & \textbf{Caractéristiques} \\
    \hline
    \multirow{10}[20]{*}{\parbox{4cm}{Analyses sur World of Warcraft [WoW]}} & \multirow{5}[10]{*}{\textbf{Composition}} & Nombre de membres \\
\cline{3-3} & & Entropie de distribution \\
\cline{3-3} & & Pourcentage des classes présentes \\
\cline{3-3} & & Longueur d’existence \\
\cline{3-3} & & Écart-type des effectifs \\
\cline{2-3} & \multirow{2}[4]{*}{\textbf{Stats du jeu}} & Temps de collaboration moyen \\
\cline{3-3} & & Temps moyen de jeu dans un groupe \\
\cline{2-3} & \multirow{3}[6]{*}{\textbf{Structurelles}} & Entropie de distribution du degré \\
\cline{3-3} & & Coefficient moyen de Clustering dans un groupe \\
\cline{3-3} & & Coefficient moyen de Clustering \\
    \hline\pagebreak\hline
    \multirow{10}[20]{*}{\textbf{Analyses sur DBLP}} & \multirow{2}[4]{*}{\textbf{Groupe}} & Nombre de membres \\
\cline{3-3} & & Prolificité moyenne \\
\cline{2-3} & \multirow{7}[14]{*}{\textbf{Activités}} & Nombre total des publications dans un groupe \\
\cline{3-3} & & Nombre total des collaborations dans un groupe \\
\cline{3-3} & & Nombre total des publications dans un groupe \\
\cline{3-3} & & Nombre total des  publications en dehors un groupe \\
\cline{3-3} & & Nombre moyen des collaborations dans un groupe \\
\cline{3-3} & & Nombre total des collaborations en dehors un groupe \\
\cline{3-3} & & Coefficient moyen de fidélité des membres \\
\cline{2-3} & \textbf{Structurelles} & Entropie de la distribution des degrés \\
    \hline
%\end{table}%
\end{longtable}
\end{document}
  • @Joker00 Have a look at Zarko's answer. He has made your table looking nice, howerver, it will not span multiple pages. – Sveinung Jan 4 '17 at 19:39
  • @Joker00 Thank you, but I suggest you use Heiko Oberdiek’s solution because it is a much better solution and gives more readable and better looking tables. – Sveinung Jan 5 '17 at 9:56

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.