4

I am using the excel to latex add in to generate tables from excel, however, when the table is too long it is not split automatically between 2 (or more pages). Is there any easy way to do this? I have found the longtable package, however, I am not managing to use it with the following code produced by LateX

% Table generated by Excel2LaTeX from sheet 'Sheet1'
\begin{table}[htbp]
  \centering
  \caption{Trial}
    \begin{tabular}{|r|r|r|r|r}
\cline{1-4}    Hello & How   & Are   & You   &  \bigstrut\\
\cline{1-4}    \multicolumn{1}{|c|}{\multirow{2}[4]{*}{1}} & 2     & \multicolumn{1}{c|}{\multirow{2}[4]{*}{4}} & \multicolumn{1}{c|}{\multirow{2}[4]{*}{5}} &  \bigstrut\\
\cline{2-2}    \multicolumn{1}{|c|}{} & 3     & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} &  \bigstrut\\
\cline{1-4}    6     & 7     & 8     & 9     &  \bigstrut\\
\cline{1-4}    \multicolumn{1}{r}{} & \multicolumn{1}{r}{} & \multicolumn{1}{r}{} & \multicolumn{1}{r}{} &  \bigstrut[t]\\
    \multicolumn{1}{r}{} & \multicolumn{1}{r}{} & \multicolumn{1}{r}{} & \multicolumn{1}{r}{} &  \\
    \end{tabular}%
  \label{tab:addlabel}%
\end{table}%

I am using a simple example to not get confused with the code here (this example does not span over 2 pages) but if it did, what changes are necessary?

enter image description here

2 Answers 2

3

As you've noted, in order to make LaTeX split a long table across pages, it's necessary to use a longtable environment instead of nested table and tabular environments.

The following example first shows the table produced by your code (with the code simplified here and there), followed by the same table produced using a longtable environment. The four key differences are:

  • The longtable environment takes as its argument the material that would be specified as the argument of the tabular environment. Do not try to nest a longtable inside a table.

  • Insert \\ (line break directive) after \caption.

  • Since the table's headers and footers may occur repeatedly, specify the repeated material explicitly. Header and footer material is delimited with instructions named \endfirsthead, \endhead, \endfoot, and \endlastfoot.

  • No need to provide a \centering instruction; the longtable is centered by default.

enter image description here

\documentclass{article}
\usepackage{multirow,longtable,bigstrut,caption}
\begin{document}

\begin{table}[h!]
\centering
\caption{A Trial} \label{tab:addlabel}
\begin{tabular}{|r|r|r|r|}
\hline 
Hello & How & Are & You \bigstrut\\
\hline 
\multicolumn{1}{|c|}{\multirow{2}[4]{*}{1}} & 2 
& \multicolumn{1}{ c|}{\multirow{2}[4]{*}{4}} 
& \multicolumn{1}{ c|}{\multirow{2}[4]{*}{5}} \bigstrut\\
\cline{2-2} 
\multicolumn{1}{|c|}{} & 3 & & \bigstrut\\
\hline 
6 & 7 & 8 & 9 \bigstrut\\
\hline 
\end{tabular}
\end{table}

\begin{longtable}{|r|r|r|r|}
\caption{Another Trial} \label{tab:addanotherlabel}\\
   \hline 
   Hello & How & Are & You \bigstrut\\
   \hline
   \endhead  % delimiter for header
   \hline
   \endfoot  % delimiter for footer
\multicolumn{1}{|c|}{\multirow{2}[4]{*}{1}} & 2 
& \multicolumn{1}{ c|}{\multirow{2}[4]{*}{4}} 
& \multicolumn{1}{ c|}{\multirow{2}[4]{*}{5}} \bigstrut\\
\cline{2-2} 
\multicolumn{1}{|c|}{} & 3 & & \bigstrut\\
\hline 
6 & 7 & 8 & 9 \bigstrut \\
\end{longtable}

\end{document}
5
  • why this construct in the longtable example : \multicolumn{1}{|c|}{\multirow{2}[4]{*}{1}} ? I tried it without nesting, i.e. \multirow{2}[4]{*}{1} and it works the same.
    – Amar
    Commented May 21, 2015 at 15:40
  • 1
    @Amar - Your assertion isn't correct: The OP's code, which I carried over to my answer, centers the material. Without the \multicolumn{1}{|c|}{...} "wrapper", the material would be right-justified. (I did delete all \multicolumn{1}{r}{} and \multicolumn{1}{c|}{} directives. I also deleted the redundant fifth column and replaced \cline{1-4} with \hline.)
    – Mico
    Commented May 21, 2015 at 15:44
  • I have posted my approach, since It is not possible to put the entire MWE in comment.
    – Amar
    Commented May 21, 2015 at 15:54
  • 1
    @Amar - The two versions will give the same result in your example, but that's because you've changed the default column type from r to c, right? Anyway, I believe the OP's main concern is with what he/she needs to do to switch from a nested \table/tabular environment to a longtable environment. I suspect the particulars of the table material are probably of little practical relevance.
    – Mico
    Commented May 21, 2015 at 15:58
  • yep! I agree with your observations.
    – Amar
    Commented May 21, 2015 at 16:09
0

A slightly modified longtable, booktabs, & multirow -based solution:

\documentclass[11pt,a4paper]{article}
\usepackage{multirow}
\usepackage{booktabs} % To nicely typeset tabular material
\usepackage{longtable}  % For multipage tables!
\begin{document}
% Table generated by Excel2LaTeX from sheet 'Sheet1'
\begin{longtable}{ccccc}
\caption[Trial]{Trial\label{tab:addlabel}}\\
%
\toprule
Hello & How & Are & You \\
\midrule
\multirow{2}[4]{*}{1} & 2 & \multirow{2}[4]{*}{4} & \multirow{2}[4]{*}{5} \\
\cmidrule{2-2}
 & 3 &  &  \\
\midrule
6 & 7 & 8 & 9 \\
\bottomrule
\end{longtable}
\end{document}

It looks like this:

enter image description here

1
  • oops! my bad! I've removed them from the code now :) thanks @Mico !
    – Amar
    Commented May 21, 2015 at 16:08

You must log in to answer this question.

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