Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Correct use of longtable, label, and caption with LaTeX #34360

Closed
jdossgollin opened this issue May 25, 2020 · 5 comments · Fixed by #36297
Closed

ENH: Correct use of longtable, label, and caption with LaTeX #34360

jdossgollin opened this issue May 25, 2020 · 5 comments · Fixed by #36297
Assignees
Labels
Milestone

Comments

@jdossgollin
Copy link

jdossgollin commented May 25, 2020

Is your feature request related to a problem?

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

@jdossgollin jdossgollin added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels May 25, 2020
@jdossgollin jdossgollin changed the title ENH: Correct use of longtable, ` with LaTeX ENH: Correct use of longtable, label, and caption with LaTeX May 25, 2020
@TomAugspurger
Copy link
Contributor

cc @SylvainLan and @jeschwar if either of you have thoughts.

@jdossgollin it'd be helpful if you provide a minimal example constructing a small DataFrame and showing the output http://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-report.

@TomAugspurger TomAugspurger added IO LaTeX to_latex Bug and removed Needs Triage Issue that has not been reviewed by a pandas team member Enhancement labels Sep 4, 2020
@jdossgollin
Copy link
Author

Sure thing.

import pandas as pd
df = pd.DataFrame({"a": range(5), "b": range(5)})
print(df.to_latex(longtable=True, caption="longtable data frame", label="tab:demo"))

gives

\begin{longtable}{lrr}
\caption{longtable data frame}\label{tab:demo}\\
\toprule
{} &  a &  b \\
\midrule
\endhead
\midrule
\multicolumn{3}{r}{{Continued on next page}} \\
\midrule
\endfoot

\bottomrule
\endlastfoot
0 &  0 &  0 \\
1 &  1 &  1 \\
2 &  2 &  2 \\
3 &  3 &  3 \\
4 &  4 &  4 \\
\end{longtable}

I would like instead for the table to show

\begin{longtable}{lrr}
\caption{longtable data frame}\label{tab:demo}\\
\toprule
{} &   a &   b \\
\midrule
\endfirsthead
% now repeat everything from above, but add a [] to the caption and don't print the label
\caption[]{Desired behavior with a very long longtable}\\ % the [] here keeps it from showing up again in \listoftables
\toprule % identical
{} &   a &   b \\ % identical
\midrule % identical
\endhead
...

Here's a live example of how the difference plays out in a document: https://www.overleaf.com/read/wyypsqptyqvb

@jeschwar
Copy link
Contributor

jeschwar commented Sep 4, 2020

@jdossgollin thanks for identifying this. I didn't give the repeated entries in the List of Tables much thought initially because the table is indeed occupying multiple pages. I can take this and revise such that the List of Tables looks cleaner. Thanks for doing all the hard work!

@jeschwar
Copy link
Contributor

jeschwar commented Sep 4, 2020

take

@jdossgollin
Copy link
Author

thanks for taking this on!

jeschwar added a commit to jeschwar/pandas that referenced this issue Sep 14, 2020
jeschwar added a commit to jeschwar/pandas that referenced this issue Sep 17, 2020
jeschwar added a commit to jeschwar/pandas that referenced this issue Sep 17, 2020
@jreback jreback added this to the 1.2 milestone Sep 19, 2020
ivanovmg added a commit to ivanovmg/pandas that referenced this issue Sep 22, 2020
Test for longtable with shorcaption was changed
to reflect the recent changes on master branch.
The changes in longtable environment were introduced recently,
see GH pandas-dev#34360.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants