Skip to content

Commit b56b1e4

Browse files
committed
Merge pull request #11778 from bkasel/master
Missing _repr_latex_ method for latex/pdf conversion in jupyter notebooks
2 parents c02756a + b4d820a commit b56b1e4

File tree

5 files changed

+57
-5
lines changed

5 files changed

+57
-5
lines changed

doc/source/options.rst

+6
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,10 @@ display.large_repr truncate For DataFrames exceeding max_rows/max_co
307307
or switch to the view from df.info()
308308
(the behaviour in earlier versions of pandas).
309309
allowable settings, ['truncate', 'info']
310+
display.latex.escape True Escapes special caracters in Dataframes, when
311+
using the to_latex method.
312+
display.latex.longtable False Specifies if the to_latex method of a Dataframe
313+
uses the longtable format.
310314
display.line_width 80 Deprecated. Use `display.width` instead.
311315
display.max_columns 20 max_rows and max_columns are used
312316
in __repr__() methods to decide if
@@ -498,3 +502,5 @@ Enabling ``display.unicode.ambiguous_as_wide`` lets pandas to figure these chara
498502
499503
pd.set_option('display.unicode.east_asian_width', False)
500504
pd.set_option('display.unicode.ambiguous_as_wide', False)
505+
506+

doc/source/whatsnew/v0.18.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ Check the :ref:`API Changes <whatsnew_0180.api>` and :ref:`deprecations <whatsne
2626
New features
2727
~~~~~~~~~~~~
2828

29-
3029
.. _whatsnew_0180.enhancements.moments:
3130

3231
Window functions are now methods
@@ -108,6 +107,7 @@ Other enhancements
108107
- ``read_excel`` now supports s3 urls of the format ``s3://bucketname/filename`` (:issue:`11447`)
109108
- A simple version of ``Panel.round()`` is now implemented (:issue:`11763`)
110109
- For Python 3.x, ``round(DataFrame)``, ``round(Series)``, ``round(Panel)`` will work (:issue:`11763`)
110+
- ``Dataframe`` has gained a ``_repr_latex_`` method in order to allow for automatic conversion to latex in a ipython/jupyter notebook using nbconvert. Options ``display.latex.escape`` and ``display.latex.longtable`` have been added to the configuration and are used automatically by the ``to_latex`` method.(:issue:`11778`)
111111

112112
.. _whatsnew_0180.enhancements.rounding:
113113

pandas/core/config_init.py

+17
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,19 @@
220220
df.info() is called. Valid values True,False,'deep'
221221
"""
222222

223+
pc_latex_escape = """
224+
: bool
225+
This specifies if the to_latex method of a Dataframe uses escapes special
226+
characters.
227+
method. Valid values: False,True
228+
"""
229+
230+
pc_latex_longtable = """
231+
:bool
232+
This specifies if the to_latex method of a Dataframe uses the longtable format.
233+
method. Valid values: False,True
234+
"""
235+
223236
style_backup = dict()
224237

225238

@@ -297,6 +310,10 @@ def mpl_style_cb(key):
297310
pc_east_asian_width_doc, validator=is_bool)
298311
cf.register_option('unicode.ambiguous_as_wide', False,
299312
pc_east_asian_width_doc, validator=is_bool)
313+
cf.register_option('latex.escape',True, pc_latex_escape,
314+
validator=is_bool)
315+
cf.register_option('latex.longtable',False,pc_latex_longtable,
316+
validator=is_bool)
300317

301318
cf.deprecate_option('display.line_width',
302319
msg=pc_line_width_deprecation_warning,

pandas/core/frame.py

+18-4
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,13 @@ def _repr_html_(self):
576576
else:
577577
return None
578578

579+
def _repr_latex_(self):
580+
"""
581+
Returns a LaTeX representation for a particular Dataframe.
582+
Mainly for use with nbconvert (jupyter notebook conversion to pdf).
583+
"""
584+
return self.to_latex()
585+
579586
@property
580587
def style(self):
581588
"""
@@ -1540,7 +1547,7 @@ def to_latex(self, buf=None, columns=None, col_space=None, colSpace=None,
15401547
header=True, index=True, na_rep='NaN', formatters=None,
15411548
float_format=None, sparsify=None, index_names=True,
15421549
bold_rows=True, column_format=None,
1543-
longtable=False, escape=True):
1550+
longtable=None, escape=None):
15441551
"""
15451552
Render a DataFrame to a tabular environment table. You can splice
15461553
this into a LaTeX document. Requires \\usepackage{booktabs}.
@@ -1552,10 +1559,12 @@ def to_latex(self, buf=None, columns=None, col_space=None, colSpace=None,
15521559
column_format : str, default None
15531560
The columns format as specified in `LaTeX table format
15541561
<https://en.wikibooks.org/wiki/LaTeX/Tables>`__ e.g 'rcl' for 3 columns
1555-
longtable : boolean, default False
1562+
longtable : boolean, default will be read from the pandas config module
1563+
default: False
15561564
Use a longtable environment instead of tabular. Requires adding
15571565
a \\usepackage{longtable} to your LaTeX preamble.
1558-
escape : boolean, default True
1566+
escape : boolean, default will be read from the pandas config module
1567+
default: True
15591568
When set to False prevents from escaping latex special
15601569
characters in column names.
15611570
@@ -1565,7 +1574,12 @@ def to_latex(self, buf=None, columns=None, col_space=None, colSpace=None,
15651574
warnings.warn("colSpace is deprecated, use col_space",
15661575
FutureWarning, stacklevel=2)
15671576
col_space = colSpace
1568-
1577+
# Get defaults from the pandas config
1578+
if longtable is None:
1579+
longtable = get_option("display.latex.longtable")
1580+
if escape is None:
1581+
escape = get_option("display.latex.escape")
1582+
15691583
formatter = fmt.DataFrameFormatter(self, buf=buf, columns=columns,
15701584
col_space=col_space, na_rep=na_rep,
15711585
header=header, index=index,

pandas/tests/test_frame.py

+15
Original file line numberDiff line numberDiff line change
@@ -4752,6 +4752,21 @@ def test_to_dict(self):
47524752
for k2, v2 in compat.iteritems(v):
47534753
self.assertEqual(v2, recons_data[k2][k])
47544754

4755+
def test_latex_repr(self):
4756+
result=r"""\begin{tabular}{llll}
4757+
\toprule
4758+
{} & 0 & 1 & 2 \\
4759+
\midrule
4760+
0 & $\alpha$ & b & c \\
4761+
1 & 1 & 2 & 3 \\
4762+
\bottomrule
4763+
\end{tabular}
4764+
"""
4765+
with option_context("display.latex.escape",False):
4766+
df=DataFrame([[r'$\alpha$','b','c'],[1,2,3]])
4767+
self.assertEqual(result,df._repr_latex_())
4768+
4769+
47554770
def test_to_dict_timestamp(self):
47564771

47574772
# GH11247

0 commit comments

Comments
 (0)