-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: Enable short_caption in to_latex #35668
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
Changes from 9 commits
13d07ae
8912c81
3f71b46
22d2ca4
01152c1
6adb05c
4725d73
a7b64c0
a2216a1
6c93de4
45de6eb
e70aafa
a0e3f53
6725ca8
8466421
0cc0664
c60a705
27891a3
e43b52f
fbea9eb
29b37e2
ed0132c
ed4b705
3453d43
16884bc
6fd52ca
5acfc44
ae1babe
ddec116
15227b3
336bfb5
b30d2d7
f1781f6
f4b18ff
a18c85d
e957c37
09d9c85
559ca2a
0fd73e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2887,7 +2887,8 @@ def to_latex( | |||||
multicolumn=None, | ||||||
multicolumn_format=None, | ||||||
multirow=None, | ||||||
caption=None, | ||||||
caption: Optional[Union[str, Tuple[str, str]]] = None, | ||||||
short_caption=None, | ||||||
label=None, | ||||||
position=None, | ||||||
): | ||||||
|
@@ -2965,11 +2966,12 @@ def to_latex( | |||||
centered labels (instead of top-aligned) across the contained | ||||||
rows, separating groups via clines. The default will be read | ||||||
from the pandas config module. | ||||||
caption : str, optional | ||||||
The LaTeX caption to be placed inside ``\caption{}`` in the output. | ||||||
caption : str, tuple, optional | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. str or tuple, optional (change the signature as well) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. I updated typing on caption in DataFrameFormatter.to_latex() and LatexFormatter. |
||||||
Tuple (short_caption, full_caption), | ||||||
which results in \caption[short_caption]{caption}; | ||||||
if a single string is passed, no short caption will be set. | ||||||
|
||||||
.. versionadded:: 1.0.0 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But I think I should keep There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure. can have both. and add a one-liner describing the changes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added these tags for caption, position (PR #35284) and |
||||||
|
||||||
label : str, optional | ||||||
The LaTeX label to be placed inside ``\label{}`` in the output. | ||||||
This is used with ``\ref{}`` in the main ``.tex`` file. | ||||||
|
@@ -3014,6 +3016,18 @@ def to_latex( | |||||
if multirow is None: | ||||||
multirow = config.get_option("display.latex.multirow") | ||||||
|
||||||
if caption: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of doing this here, can you do it in the formatter itself (in the constructor is ok) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||||||
if isinstance(caption, str): | ||||||
short_caption = "" | ||||||
else: | ||||||
try: | ||||||
caption, short_caption = caption | ||||||
except ValueError as err: | ||||||
msg = "caption must be either str or tuple of two strings" | ||||||
raise ValueError(msg) from err | ||||||
else: | ||||||
short_caption = None | ||||||
|
||||||
formatter = DataFrameFormatter( | ||||||
self, | ||||||
columns=columns, | ||||||
|
@@ -3038,6 +3052,7 @@ def to_latex( | |||||
multicolumn_format=multicolumn_format, | ||||||
multirow=multirow, | ||||||
caption=caption, | ||||||
short_caption=short_caption, | ||||||
label=label, | ||||||
position=position, | ||||||
) | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -930,6 +930,7 @@ def to_latex( | |
multicolumn_format: Optional[str] = None, | ||
multirow: bool = False, | ||
caption: Optional[str] = None, | ||
short_caption: Optional[str] = None, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
label: Optional[str] = None, | ||
position: Optional[str] = None, | ||
) -> Optional[str]: | ||
|
@@ -946,6 +947,7 @@ def to_latex( | |
multicolumn_format=multicolumn_format, | ||
multirow=multirow, | ||
caption=caption, | ||
short_caption=short_caption, | ||
label=label, | ||
position=position, | ||
).get_result(buf=buf, encoding=encoding) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -441,6 +441,7 @@ def test_to_latex_longtable(self): | |
def test_to_latex_caption_label(self): | ||
# GH 25436 | ||
the_caption = "a table in a \\texttt{table/tabular} environment" | ||
the_short_caption = "a table" | ||
the_label = "tab:table_tabular" | ||
|
||
df = DataFrame({"a": [1, 2], "b": ["b1", "b2"]}) | ||
|
@@ -497,12 +498,73 @@ def test_to_latex_caption_label(self): | |
\bottomrule | ||
\end{tabular} | ||
\end{table} | ||
""" | ||
assert result_cl == expected_cl | ||
|
||
# test when the short_caption is provided alongside caption | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you split each comment here into a separate test? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did that. |
||
result_cl = df.to_latex(caption=(the_caption, the_short_caption)) | ||
|
||
expected_cl = r"""\begin{table} | ||
\centering | ||
\caption[a table]{a table in a \texttt{table/tabular} environment} | ||
\begin{tabular}{lrl} | ||
\toprule | ||
{} & a & b \\ | ||
\midrule | ||
0 & 1 & b1 \\ | ||
1 & 2 & b2 \\ | ||
\bottomrule | ||
\end{tabular} | ||
\end{table} | ||
""" | ||
assert result_cl == expected_cl | ||
|
||
# test when the short_caption is provided alongside caption and label | ||
result_cl = df.to_latex( | ||
caption=(the_caption, the_short_caption), label=the_label, | ||
) | ||
|
||
expected_cl = r"""\begin{table} | ||
\centering | ||
\caption[a table]{a table in a \texttt{table/tabular} environment} | ||
\label{tab:table_tabular} | ||
\begin{tabular}{lrl} | ||
\toprule | ||
{} & a & b \\ | ||
\midrule | ||
0 & 1 & b1 \\ | ||
1 & 2 & b2 \\ | ||
\bottomrule | ||
\end{tabular} | ||
\end{table} | ||
""" | ||
assert result_cl == expected_cl | ||
|
||
# test that wrong number of params is raised | ||
with pytest.raises(ValueError): | ||
df.to_latex(caption=(the_caption, the_short_caption, "extra_string")) | ||
|
||
# test that two chars caption is handled correctly | ||
result_cl = df.to_latex(caption="xy") | ||
expected_cl = r"""\begin{table} | ||
\centering | ||
\caption{xy} | ||
\begin{tabular}{lrl} | ||
\toprule | ||
{} & a & b \\ | ||
\midrule | ||
0 & 1 & b1 \\ | ||
1 & 2 & b2 \\ | ||
\bottomrule | ||
\end{tabular} | ||
\end{table} | ||
""" | ||
assert result_cl == expected_cl | ||
|
||
def test_to_latex_longtable_caption_label(self): | ||
# GH 25436 | ||
the_caption = "a table in a \\texttt{longtable} environment" | ||
the_short_caption = "a table" | ||
the_label = "tab:longtable" | ||
|
||
df = DataFrame({"a": [1, 2], "b": ["b1", "b2"]}) | ||
|
@@ -566,6 +628,31 @@ def test_to_latex_longtable_caption_label(self): | |
\midrule | ||
\endfoot | ||
|
||
\bottomrule | ||
\endlastfoot | ||
0 & 1 & b1 \\ | ||
1 & 2 & b2 \\ | ||
\end{longtable} | ||
""" | ||
assert result_cl == expected_cl | ||
|
||
# test when the caption, the short_caption and the label are provided | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment here - modular tests are much easier to debug in case of future issues There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I absolutely agree. I would prefer this #36528 to be merged first, and then rebase on top of that with the separate tests. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Split tests. |
||
result_cl = df.to_latex( | ||
longtable=True, caption=(the_caption, the_short_caption), label=the_label, | ||
) | ||
|
||
expected_cl = r"""\begin{longtable}{lrl} | ||
\caption[a table]{a table in a \texttt{longtable} environment} | ||
\label{tab:longtable}\\ | ||
\toprule | ||
{} & a & b \\ | ||
\midrule | ||
\endhead | ||
\midrule | ||
\multicolumn{3}{r}{{Continued on next page}} \\ | ||
\midrule | ||
\endfoot | ||
|
||
\bottomrule | ||
\endlastfoot | ||
0 & 1 & b1 \\ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be removed from the signature right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did that, but the build failed due to exceeding time of 1 hour.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
restarted
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Failure of
TestRegistration.test_pandas_plots_register
, which does not seem to be related to the commits.