ENH: .to_latex(longtable=True) latex caption and label support #25339
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
git diff upstream/master -u -- "*.py" | flake8 --diffWhen creating a latex table with
DataFrame.to_latex(longtable=False)the output is written inside a latextabularenvironment and stored in some file likepandas_tabular.tex; the user can conveniently typeset the table in a mainreport.texfile complete with caption and label as follows:This is good because the
pandas_tabular.texfile can be re-created and the mainreport.texsimply needs to be recompiled to get the updated output.The problem when creating a latex longtable with
DataFrame.to_latex(longtable=True)is the caption and label need to go inside the latexlongtableenvironment which is stored in a some file likepandas_longtable.tex. The latexlongtableenvironment does not go inside atableenvironment like thetabularenvironment does; this means that setting the caption and label requires the user to edit thepandas_longtable.texfile after its creation. This does not support an automated workflow like we have with thetabularenvironment.This PR adds caption and label support to
DataFrame.to_latex(longtable=True)with the argumentslt_captionandlt_label. Example usage is described below.The following python code creates some data in a
DataFrameand writes it to disk intabularandlongtablelatex environments:The following latex code is contained in a main
report.texand is used to typset both tables:Using
DataFrame.to_latex(longtable=True)with the new argumentslt_captionandlt_labelmeans we don't have to editpandas_longtable.texafter its creation to get the caption and label working. This functionality also works withSeries.to_latex(longtable=True).PDF output is shown below: