Skip to content

Commit d207679

Browse files
chris-b1jreback
authored andcommitted
BUG: option to avoid latex in qtconsole
closes #12182 closes #12275
1 parent 358da56 commit d207679

File tree

5 files changed

+33
-3
lines changed

5 files changed

+33
-3
lines changed

doc/source/options.rst

+3
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,9 @@ 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.repr False Whether to produce a latex DataFrame
311+
representation for jupyter frontends
312+
that support it.
310313
display.latex.escape True Escapes special caracters in Dataframes, when
311314
using the to_latex method.
312315
display.latex.longtable False Specifies if the to_latex method of a Dataframe

doc/source/whatsnew/v0.18.0.txt

+12-1
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,18 @@ See the `xarray full-documentation here <http://xarray.pydata.org/en/stable/>`__
344344
* major_axis (major_axis) int64 0 1 2
345345
* minor_axis (minor_axis) int64 0 1 2 3
346346

347+
Latex Representation
348+
^^^^^^^^^^^^^^^^^^^^
349+
350+
``DataFrame`` has gained a ``._repr_latex_()`` method in order to allow for conversion to latex in a ipython/jupyter notebook using nbconvert. (:issue:`11778`)
351+
352+
Note that this must be activated by setting the option ``display.latex.repr`` to ``True`` (issue:`12182`)
353+
354+
For example, if you have a jupyter notebook you plan to convert to latex using nbconvert, place the statement ``pd.set_option('display.latex.repr', True)`` in the first cell to have the contained DataFrame output also stored as latex.
355+
356+
Options ``display.latex.escape`` and ``display.latex.longtable`` have also been added to the configuration and are used automatically by the ``to_latex``
357+
method. See the :ref:`options documentation<options>` for more info.
358+
347359
.. _whatsnew_0180.enhancements.other:
348360

349361
Other enhancements
@@ -355,7 +367,6 @@ Other enhancements
355367
- add support for ``AWS_S3_HOST`` env variable when reading from s3 (:issue:`12198`)
356368
- A simple version of ``Panel.round()`` is now implemented (:issue:`11763`)
357369
- For Python 3.x, ``round(DataFrame)``, ``round(Series)``, ``round(Panel)`` will work (:issue:`11763`)
358-
- ``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`)
359370
- ``sys.getsizeof(obj)`` returns the memory usage of a pandas object, including the
360371
values it contains (:issue:`11597`)
361372
- ``Series`` gained an ``is_unique`` attribute (:issue:`11946`)

pandas/core/config_init.py

+9
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,13 @@
155155
(default: False)
156156
"""
157157

158+
pc_latex_repr_doc = """
159+
: boolean
160+
Whether to produce a latex DataFrame representation for jupyter
161+
environments that support it.
162+
(default: False)
163+
"""
164+
158165
pc_line_width_deprecation_warning = """\
159166
line_width has been deprecated, use display.width instead (currently both are
160167
identical)
@@ -314,6 +321,8 @@ def mpl_style_cb(key):
314321
pc_east_asian_width_doc, validator=is_bool)
315322
cf.register_option('unicode.ambiguous_as_wide', False,
316323
pc_east_asian_width_doc, validator=is_bool)
324+
cf.register_option('latex.repr', False,
325+
pc_latex_repr_doc, validator=is_bool)
317326
cf.register_option('latex.escape', True, pc_latex_escape,
318327
validator=is_bool)
319328
cf.register_option('latex.longtable', False, pc_latex_longtable,

pandas/core/frame.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,10 @@ def _repr_latex_(self):
574574
Returns a LaTeX representation for a particular Dataframe.
575575
Mainly for use with nbconvert (jupyter notebook conversion to pdf).
576576
"""
577-
return self.to_latex()
577+
if get_option('display.latex.repr'):
578+
return self.to_latex()
579+
else:
580+
return None
578581

579582
@property
580583
def style(self):

pandas/tests/frame/test_repr_info.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,14 @@ def test_latex_repr(self):
186186
\bottomrule
187187
\end{tabular}
188188
"""
189-
with option_context("display.latex.escape", False):
189+
with option_context("display.latex.escape", False,
190+
'display.latex.repr', True):
190191
df = DataFrame([[r'$\alpha$', 'b', 'c'], [1, 2, 3]])
191192
self.assertEqual(result, df._repr_latex_())
192193

194+
# GH 12182
195+
self.assertIsNone(df._repr_latex_())
196+
193197
def test_info(self):
194198
io = StringIO()
195199
self.frame.info(buf=io)

0 commit comments

Comments
 (0)