5

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}
  • 3
    Don't use table, it won't break across pages. lontable provides caption too. – user11232 Feb 10 '15 at 4:27
7

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

  • I am using longtable for the same purpose, but the problem is the line spacing and font size of the text inside my tables are now the same as the regular text. How can I fix it? – anna Jun 3 '15 at 12:34
0

As an aside, one routinely also reduces table font sizes to ensure that they fit within the page width for table environments. Though longtable supports captions, it will ignore font size commands, such as \tiny and \scriptsize. However, the longtable itself can be wrapped within a font size environment, thus ensuring desired output.

\begin{scriptsize}
    \begin{longtabu} to \textwidth {rllrrll}
        \caption[Largest cities by highest reduction target]{Each country's largest city, its location, and each countries highest promised reduction target.} \\
        \toprule
        \rowfont\bfseries & Country & Largest City & Latitude & Longitude & Highest   & Notes \\
        \rowfont\bfseries &         &              &          &           & Reduction & \\
        \rowfont\bfseries &         &              &          &           & Target    & \\ \midrule
        \endhead

        \\ \hline 
        \multicolumn{7}{r}{Continued on next page} \\
        \endfoot

        \bottomrule
        \endlastfoot

        1 & Cape Verde & Praia & 14.92 & -23.51 & 100\% & electricity sector \\ 
        2 & Papua New Guinea & Port Moresby & -9.48 & 147.15 & 100\% & for electricity generation \\ 
    \end{longtabu}
\end{scriptsize}
  • 2
    ...there exist no "font size environment". It works, but one typically use {\<fontsize> ... }, with a possible \par. – Werner Aug 16 '16 at 19:37
  • If it works, then it must exist, yes? Moreover, the {\fontsize} approach does not work within longtables; note the question asked by @anna on July 3rd 2015. I had the exact same problem, and successfully resolved it by using a font size environment as depicted above. – Mark Coletti Aug 18 '16 at 14:38

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.