[Tex/LaTex] Issue in using longtable within table environment

longtabletables

I have an issue making a long list with wrapped text. The table can be several page long. I use longtable and assign the width for each column but as soon as I put it in the \begin{table} \end{table}, it does not work. It clips the table. I want to make a label for the table so I put it in the table environment.

And is there a way to automatically adjust the width so that I don't need to assign the value manually?

Thank you.

Tung

\begin{table}
\centering
\begin{longtable}{|p{6.5cm}|p{2.5cm}|p{2.5cm}|p{2.5cm}|}
\hline
Name                                      & Company     &  Catalog Number   &   Comments    \\ \hline
Small DNA FRAG Extract Kit-100PR          & VWR         &   97060-558 & \\ \hline
Acrylamide 40\% solution 500 mL           & VWR         &   97064-522 &  \\ \hline
Bis-acrylamide 2\% (w/v) solution 500 mL  & VWR         &   97063-948 &  \\ \hline
GeneRuler 100 bp DNA Ladder, 100-1,000 bp & Fermentas   &   SM0241  &    \\ \hline
Mini Vertical PAGE System                 & VWR         &   89032-300  &  \\ \hline
\end{longtable}
\label{tab:list}
\end{table}

Best Answer

table encloses its contents in a box which will not break across pages, but which can "float" to a convenient location. longtable gets rid of this box, is able to break across pages, and does not float. Hence enclosing longtable inside a table defeats the entire purpose. Remove table environment.

longtable provides a \caption facility and hence you can add a \label too.

A sample

\documentclass{article}
\usepackage{longtable}
\begin{document}
\begin{longtable}{|p{6.5cm}|p{2.5cm}|p{2.5cm}|p{2.5cm}|}
\caption[An optional table caption]{A long table\label{tab:list}}\\
\hline
Name                                      & Company     &  Catalog Number   &   Comments    \\ \hline
Small DNA FRAG Extract Kit-100PR          & VWR         &   97060-558 & \\ \hline
Acrylamide 40\% solution 500 mL           & VWR         &   97064-522 &  \\ \hline
Bis-acrylamide 2\% (w/v) solution 500 mL  & VWR         &   97063-948 &  \\ \hline
GeneRuler 100 bp DNA Ladder, 100-1,000 bp & Fermentas   &   SM0241  &    \\ \hline
Mini Vertical PAGE System                 & VWR         &   89032-300  &  \\ \hline
\end{longtable}

A long table~\ref{tab:list} is here.

\end{document}

enter image description here

For more details read the longtable documentation (available if you run texdoc longtable from command prompt/terminal or from your tex editor's help menu.

Here is an improved version with better widths

\documentclass{article}
\usepackage{longtable,array,showframe}   %% Remove showframe
\begin{document}
\begin{longtable}{|>{\raggedright}m{0.4\textwidth}|
                   >{\raggedright}p{0.13\textwidth}|
                   >{\raggedright}p{0.15\textwidth}|
                   >{\raggedright\arraybackslash}p{0.13\textwidth}|}
\caption[An optional table caption]{A long table\label{tab:list}}\\
\hline
Name                                      & Company     &  Catalog Number   &   Comments    \\ \hline
Small DNA FRAG Extract Kit-100PR          & VWR         &   97060-558 & \\ \hline
Acrylamide 40\% solution 500 mL           & VWR         &   97064-522 &  \\ \hline
Bis-acrylamide 2\% (w/v) solution 500 mL  & VWR         &   97063-948 &  \\ \hline
GeneRuler 100 bp DNA Ladder, 100-1,000 bp & Fermentas   &   SM0241  &    \\ \hline
Mini Vertical PAGE System                 & VWR         &   89032-300  &  \\ \hline
\end{longtable}

A long table~\ref{tab:list} is here.

\end{document}

enter image description here

And this is obligatory booktabs version:

\documentclass{article}
\usepackage{longtable,array,booktabs,showframe}
\begin{document}
\begin{longtable}{>{\raggedright}m{0.4\textwidth}
                   >{\raggedright}p{0.13\textwidth}
                   >{\raggedright}p{0.15\textwidth}
                   >{\raggedright\arraybackslash}p{0.13\textwidth}}
\caption[An optional table caption]{A long table\label{tab:list}}\\
\toprule
Name                                      & Company     &  Catalog Number   &   Comments    \\ \midrule
Small DNA FRAG Extract Kit-100PR          & VWR         &   97060-558 & \\ 
Acrylamide 40\% solution 500 mL           & VWR         &   97064-522 &  \\ 
Bis-acrylamide 2\% (w/v) solution 500 mL  & VWR         &   97063-948 &  \\ 
GeneRuler 100 bp DNA Ladder, 100-1,000 bp & Fermentas   &   SM0241  &    \\ 
Mini Vertical PAGE System                 & VWR         &   89032-300  &  \\ \bottomrule
\end{longtable}

A long table~\ref{tab:list} is here.

\end{document}

enter image description here

Related Question