diff --git a/doc/source/options.rst b/doc/source/options.rst index 0b2ba03a441a2..d00ba047a56a6 100644 --- a/doc/source/options.rst +++ b/doc/source/options.rst @@ -307,6 +307,9 @@ display.large_repr truncate For DataFrames exceeding max_rows/max_co or switch to the view from df.info() (the behaviour in earlier versions of pandas). allowable settings, ['truncate', 'info'] +display.latex.repr False Whether to produce a latex DataFrame + representation for jupyter frontends + that support it. display.latex.escape True Escapes special caracters in Dataframes, when using the to_latex method. display.latex.longtable False Specifies if the to_latex method of a Dataframe diff --git a/doc/source/whatsnew/v0.18.0.txt b/doc/source/whatsnew/v0.18.0.txt index d30c0321568bc..793565bd54581 100644 --- a/doc/source/whatsnew/v0.18.0.txt +++ b/doc/source/whatsnew/v0.18.0.txt @@ -312,6 +312,23 @@ New Behavior: s.index print(s.to_csv(path=None)) +Latex Represenation +^^^^^^^^^^^^^^^^^^^ + +``DataFrame`` has gained a ``_repr_latex_`` method in order to allow +for conversion to latex in a ipython/jupyter notebook using +nbconvert. (:issue:`11778`) + +Note that this must be activated by setting the option ``display.latex.repr`` +to ``True`` (issue:`12182`) + +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. + +Options ``display.latex.escape`` and ``display.latex.longtable`` have also been +added to the configuration and are used automatically by the ``to_latex`` +method. See the :ref:`options documentation` for more info. .. _whatsnew_0180.enhancements.other: @@ -324,7 +341,6 @@ Other enhancements - add support for ``AWS_S3_HOST`` env variable when reading from s3 (:issue:`12198`) - A simple version of ``Panel.round()`` is now implemented (:issue:`11763`) - For Python 3.x, ``round(DataFrame)``, ``round(Series)``, ``round(Panel)`` will work (:issue:`11763`) -- ``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`) - ``sys.getsizeof(obj)`` returns the memory usage of a pandas object, including the values it contains (:issue:`11597`) - ``Series`` gained an ``is_unique`` attribute (:issue:`11946`) diff --git a/pandas/core/config_init.py b/pandas/core/config_init.py index 01a39583001c1..93a6968be8602 100644 --- a/pandas/core/config_init.py +++ b/pandas/core/config_init.py @@ -155,6 +155,13 @@ (default: False) """ +pc_latex_repr_doc = """ +: boolean + Whether to produce a latex DataFrame representation for jupyter + environments that support it. + (default: False) +""" + pc_line_width_deprecation_warning = """\ line_width has been deprecated, use display.width instead (currently both are identical) @@ -314,6 +321,8 @@ def mpl_style_cb(key): pc_east_asian_width_doc, validator=is_bool) cf.register_option('unicode.ambiguous_as_wide', False, pc_east_asian_width_doc, validator=is_bool) + cf.register_option('latex.repr', False, + pc_latex_repr_doc, validator=is_bool) cf.register_option('latex.escape', True, pc_latex_escape, validator=is_bool) cf.register_option('latex.longtable', False, pc_latex_longtable, diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 27e932cb54b95..324f30ed00bed 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -574,7 +574,10 @@ def _repr_latex_(self): Returns a LaTeX representation for a particular Dataframe. Mainly for use with nbconvert (jupyter notebook conversion to pdf). """ - return self.to_latex() + if get_option('display.latex.repr'): + return self.to_latex() + else: + return None @property def style(self): diff --git a/pandas/tests/frame/test_repr_info.py b/pandas/tests/frame/test_repr_info.py index 2dda4a37e6449..07446d32c55fb 100644 --- a/pandas/tests/frame/test_repr_info.py +++ b/pandas/tests/frame/test_repr_info.py @@ -186,10 +186,14 @@ def test_latex_repr(self): \bottomrule \end{tabular} """ - with option_context("display.latex.escape", False): + with option_context("display.latex.escape", False, + 'display.latex.repr', True): df = DataFrame([[r'$\alpha$', 'b', 'c'], [1, 2, 3]]) self.assertEqual(result, df._repr_latex_()) + # GH 12182 + self.assertIsNone(df._repr_latex_()) + def test_info(self): io = StringIO() self.frame.info(buf=io)