4

Sorry, I know that other questions similar to this one were answered but I was not able to figure out how to split a long table in multiple lines.

I have the following code for a table but I need to split it in two pages.

\begin{document}

\begin{table}[h]

\centering
\caption{Comparação dos resultados obtidos.}
\label{comp}
\begin{tabular}{ccccccc}
    \hline
    Teste & \begin{tabular}[c]{@{}c@{}}Maior\\ resultado (\%)\end{tabular} & 

\begin{tabular}[c]{@{}c@{}}N\\ (NoC)\end{tabular}  & Tipo & \begin{tabular}[c]{@{}c@{}}Característca\\ extraída\end{tabular} & \begin{tabular}[c]{@{}c@{}}Rotina\\de ML\end{tabular}& \begin{tabular}[c]{@{}l@{}}Conversão para\\ níveis de cinza\end{tabular} \\
\hline
1 & 67,5000 & 6 (24) & Haar & Energia & \begin{tabular}[c]{@{}c@{}}IBk\\ \textit{cross-validation}\end{tabular} & eq. \ref{eq:pratt}\\ 
        2 & 67,5000 & 6 (24) & Haar & Energia & \begin{tabular}[c]{@{}c@{}}IBk\\ \textit{cross-validation}\end{tabular} & eq. \ref{eq:customeq} \\  
        3 & 65,9167 & \begin{tabular}[c]{@{}c@{}} 3 (12)\\ 4 (16) \end{tabular} & db4 & Energia & \begin{tabular}[c]{@{}c@{}}IBk\\ \textit{cross-validation}\end{tabular} & eq. \ref{eq:pratt}\\
        4 & 66,8333 & 4 (16) & db4 & Energia & \begin{tabular}[c]{@{}c@{}}IBk\\ \textit{cross-validation}\end{tabular} & eq. \ref{eq:customeq}\\
        5 & 67,5833 & 5 (40) & Haar & Energia e Std & \begin{tabular}[c]{@{}c@{}}IBk\\ \textit{cross-validation}\end{tabular} & eq. \ref{eq:pratt}\\
        6 & 67,5000 & \begin{tabular}[c]{@{}c@{}} 4 (32)\\ 5 (40) \end{tabular} & Haar & Energia e Std & \begin{tabular}[c]{@{}c@{}}IBk\\  \textit{cross-validation}\end{tabular} & eq. \ref{eq:customeq}\\
        7 & 68,6667 & 2 (16) & db4 & Energia e Std  & \begin{tabular}[c]{@{}c@{}}IBk\\ 
        \textit{percentage split}\end{tabular}& eq. \ref{eq:pratt}\\
        8 & 68,3333 & \begin{tabular}[c]{@{}c@{}} 2 (16)\\ 3 (24) \end{tabular} & db4 & Energia e Std & \begin{tabular}[c]{@{}c@{}}IBk\\ \textit{cross-validation}\\ \textit{percentage split}\end{tabular} & eq. \ref{eq:customeq}\\
        9 & 71,0000 & 3 (33) & db16 & \begin{tabular}[c]{@{}c@{}}Energia, Std\\e entropia\end{tabular}  & \begin{tabular}[c]{@{}c@{}}IBk\\ \textit{cross-validation}\end{tabular} & eq. \ref{eq:pratt} \\
        10 & 71,0833 & 3 (33) & db16 & \begin{tabular}[c]{@{}c@{}}Energia, Std\\e entropia\end{tabular}  & \begin{tabular}[c]{@{}c@{}}IBk\\ \textit{cross-validation}\end{tabular} & eq. \ref{eq:customeq}\\

\hline

\end{tabular}
\end{table}
\end{document}
2
  • 2
    Welcome to TeX.SX! Please help us help you and add a minimal working example (MWE) that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass{...} and ending with \end{document}. You may want to have a look at the longtable package. Commented Jul 29, 2017 at 18:59
  • You shouldn't specify [h] as the only placement option for a float. Instead you should use something like [htbp] which will try h but if that doesn't work out has reasonable fallbacks. With h only you might end up with pretty bad placement (e.g. at the end of the document/chapter).
    – Skillmon
    Commented Jul 29, 2017 at 19:31

2 Answers 2

3
  • your table (which code you provide) is small enough that can fit on one page, so there is no need to use longtable :)
  • maybe you will expand it later ... in this case the following mwe can serve as framework
  • i slightly modify your table code. in this i employ packages booktabs (for horizontal rules) and makecell (for multi line cells)

\documentclass{article}
\usepackage{geometry}

\usepackage[utf8]{inputenc}
\usepackage{booktabs, makecell, longtable}

\usepackage{lipsum}
\begin{document}
\lipsum[1-3]

\begin{longtable}{cc cc cc c}
    \caption{Comparação dos resultados obtidos.}
    \label{comp}        \\
    \toprule
Teste
    &   \makecell[b]{Maior\\ resultado (\%)}
        &   \makecell[b]{N\\ (NoC)}    &   Tipo
            &   \makecell[b]{Característca\\ extraída}
                &   \makecell[b]{Rotina\\de ML}
                    &   \makecell[b]{Conversão para\\ níveis de cinza}  \\
    \midrule
\endfirsthead
    \caption[]{Comparação dos resultados obtidos (cont.).}    \\
    \toprule
Teste
    &   \makecell[b]{Maior\\ resultado (\%)}
        &   \makecell[b]{N\\ (NoC)}    &   Tipo
            &   \makecell[b]{Característca\\ extraída}
                &   \makecell[b]{Rotina\\de ML}
                    &   \makecell[b]{Conversão para\\ níveis de cinza}  \\
    \midrule
\endhead
    \midrule
    \multicolumn{7}{r}{\footnotesize\itshape Continue on the next page}
\endfoot
    \bottomrule
\endlastfoot
1 & 67,5000 & 6 (24) & Haar & Energia & \makecell{IBk\\ \textit{cross-validation}} & eq. \ref{eq:pratt}
    \\  \addlinespace[5pt]
2 & 67,5000 & 6 (24) & Haar & Energia & \makecell{IBk\\ \textit{cross-validation}} & eq. \ref{eq:customeq}
    \\  \addlinespace[5pt]
3 & 65,9167 & \makecell{3 (12)\\ 4 (16)} & db4 & Energia & \makecell{IBk\\ \textit{cross-validation}} & eq. \ref{eq:pratt}
    \\  \addlinespace[5pt]
4 & 66,8333 & 4 (16) & db4 & Energia & \makecell{IBk\\ \textit{cross-validation}} & eq. \ref{eq:customeq}
    \\  \addlinespace[5pt]
5 & 67,5833 & 5 (40) & Haar & Energia e Std & \makecell{IBk\\ \textit{cross-validation}} & eq. \ref{eq:pratt}
    \\  \addlinespace[5pt]
6 & 67,5000 & \makecell{4 (32)\\ 5 (40)} & Haar & Energia e Std & \makecell{IBk\\  \textit{cross-validation}} & eq. \ref{eq:customeq}
    \\  \addlinespace[5pt]
7 & 68,6667 & 2 (16) & db4 & Energia e Std  & \makecell{IBk\\ \textit{percentage split}} & eq. \ref{eq:pratt}
    \\    \addlinespace[5pt]
8 & 68,3333 & \makecell{ 2 (16)\\ 3 (24)} & db4 & Energia e Std & \makecell{IBk\\ \textit{cross-validation}\\ \textit{percentage split}} & eq. \ref{eq:customeq}
    \\  \addlinespace[5pt]
9 & 71,0000 & 3 (33) & db16 & \makecell{Energia, Std\\e entropia}  & \makecell{IBk\\ \textit{cross-validation}} & eq. \ref{eq:pratt}
    \\    \addlinespace[5pt]
10 & 71,0833 & 3 (33) & db16 & \makecell{Energia, Std\\e entropia}  &
\makecell{IBk\\ \textit{cross-validation}} & eq. \ref{eq:customeq}
    \\
\end{longtable}
\end{document}

enter image description here enter image description here

3
  • Thanks for the answer. I really appreciate it.It worked right. Commented Jul 31, 2017 at 3:41
  • The second \label{comp} (right after \caption{Comparação dos resultados obtidos (cont.).}) causes comp to be defined multiple times.
    – Leone
    Commented Aug 5, 2021 at 19:18
  • 1
    @Leone, you are partly right. Probably I forgot to erase it (after copy-past the first table head) as well to add to caption option[] . Corrected now.
    – Zarko
    Commented Aug 5, 2021 at 20:22
1

Here it is, simplified and improved by the loading of makecell (allows for line breaks in standard cells and a common formatting) and of booktabs (variable width horizontal lines with some padding). I added some spacing around the rows for clarity and a better readability.

I added some text to introduce the second part of the table on the next page but, as I don't speak portuguese, I'm sorry I couldn't translate it.

\documentclass[portuguese]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage{array, longtable, booktabs, makecell}
\renewcommand\theadfont{\normalsize}

\begin{document}

\vspace*{8cm}

\setlength\extrarowheight{12pt}
\begin{longtable}{*{7}{c}}%
  \caption{Comparação dos resultados obtidos.}
  \label{comp} \\
  \toprule\addlinespace[-2pt]
  Teste & \thead{Maior \\ resultado (\%)} & \thead{N\\ (NoC)} & Tipo & \thead{Característca\\ extraída} & \thead{Rotina\\de ML}& \thead{Conversão para\\ níveis de cinza} \\
  \midrule\addlinespace[-2pt]
  \endfirsthead
  \multicolumn{7}{c}{\tablename~\thetable~ (continued)} \\
  \addlinespace
  \toprule\addlinespace[-2pt]
  Teste & \thead{Maior \\ resultado (\%)} & \thead{N\\ (NoC)} & Tipo & \thead{Característca\\ extraída} & \thead{Rotina\\de ML}& \thead{Conversão para\\ níveis de cinza} \\
  \midrule\addlinespace[-1pt]
  \endhead
  \midrule
  \addlinespace[-8pt]
  \multicolumn{7}{r@{}}{\footnotesize To be continued}
  \endfoot
  \bottomrule
  \endlastfoot
  1 & 67,5000 & 6 (24) & Haar & Energia & \makecell{IBk \\ \textit{cross-validation}} & eq. \ref{eq:pratt}\\
  2 & 67,5000 & 6 (24) & Haar & Energia & \makecell{IBk \\ \textit{cross-validation}} & eq. \ref{eq:customeq} \\
  3 & 65,9167 & \makecell{ 3 (12) \\ 4 (16) } & db4 & Energia & \makecell{IBk\\ \textit{cross-validation}} & eq. \ref{eq:pratt}\\
  4 & 66,8333 & 4 (16) & db4 & Energia & \makecell{IBk \\ \textit{cross-validation}} & eq. \ref{eq:customeq}\\
  5 & 67,5833 & 5 (40) & Haar & Energia e Std & \makecell{IBk \\ \textit{cross-validation}} & eq. \ref{eq:pratt}\\
  6 & 67,5000 & \makecell{ 4 (32) \\ 5 (40) } & Haar & Energia e Std & \makecell{IBk\\ \textit{cross-validation}} & eq. \ref{eq:customeq}\\
  7 & 68,6667 & 2 (16) & db4 & Energia e Std & \makecell{IBk \\ \textit{percentage split}} & eq. \ref{eq:pratt}\\
  8 & 68,3333 & \makecell{ 2 (16) \\ 3 (24) } & db4 & Energia e Std & \makecell{IBk\\ \textit{cross-validation}\\ \textit{percentage split}} & eq. \ref{eq:customeq}\\
  9 & 71,0000 & 3 (33) & db16 & \makecell{Energia, Std \\e entropia} & \makecell{IBk\\ \textit{cross-validation}} & eq. \ref{eq:pratt} \\
  10 & 71,0833 & 3 (33) & db16 & \ \makecell{Energia, Std \\e entropia} & \makecell{IBk\\ \textit{cross-validation}} & eq. \ref{eq:customeq}
\end{longtable}

\end{document}

enter image description here enter image description here

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .