ENH: Correct use of longtable, label, and caption with LaTeX (original) (raw)

Currently DataFrame.to_latex() has a longtable argument, which writes the DataFrame to latex using the syntax of the longtable package. When used with the label and caption arguments, it writes a latex table with all of these features.

However, the caption and label are both placed between a \begin{longtable} line and \endhead. As a consequence, the table appears twice in the listoftables and leads to warnings about the reference being multiply defined

Describe the solution you'd like

the .to_latex() method when longtable=True and caption=X and label=Y should print out as follows

\begin{longtable}{...}% alignment characters \caption{Caption here}\label{label-here} \endfirsthead \caption{Caption here} \endhead

This will prevent a separate label from being defined multiple times in latex (once for each page the caption is printed on). See page 5 of http://www.texdoc.net/texmf-dist/doc/latex/tools/longtable.pdf.

API breaking implications

None

Describe alternatives you've considered

An alternative is to set the caption and label to False, and to add them myself. This works but defeats the purpose of calling the .to_latex() API