Skip to content

Commit 911a491

Browse files
committed
moved code for table beginning and end writing into functions (pandas-dev#25436)
1 parent 4e615d2 commit 911a491

File tree

2 files changed

+80
-56
lines changed

2 files changed

+80
-56
lines changed

pandas/core/generic.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2750,6 +2750,8 @@ def to_latex(self, buf=None, columns=None, col_space=None, header=True,
27502750
Render an object to a tabular environment table. You can splice
27512751
this into a LaTeX document. Requires \usepackage{booktabs}.
27522752
2753+
.. versionchanged:: 0.20.2
2754+
Added to Series.
27532755
.. versionchanged:: 0.25.0
27542756
Added caption and label arguments.
27552757

pandas/io/formats/latex.py

Lines changed: 78 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -110,56 +110,12 @@ def pad_empties(x):
110110
raise AssertionError('column_format must be str or unicode, '
111111
'not {typ}'.format(typ=type(column_format)))
112112

113-
use_table_env = False
114-
if not self.longtable:
115-
if self.caption is None and self.label is None:
116-
# then write output only in a tabular environment
117-
pass
118-
else:
119-
# then write output in a nested table/tabular environment
120-
use_table_env = True
121-
122-
if self.caption is None:
123-
caption_ = ''
124-
else:
125-
caption_ = '\n\\caption{{{}}}'.format(self.caption)
126-
127-
if self.label is None:
128-
label_ = ''
129-
else:
130-
label_ = '\n\\label{{{}}}'.format(self.label)
131-
132-
buf.write('\\begin{{table}}\n\\centering{}{}\n'.format(
133-
caption_,
134-
label_
135-
))
136-
137-
buf.write('\\begin{{tabular}}{{{fmt}}}\n'
138-
.format(fmt=column_format))
139-
buf.write('\\toprule\n')
113+
if self.longtable:
114+
self._write_longtable_begin(buf, column_format)
140115
else:
141-
buf.write('\\begin{{longtable}}{{{fmt}}}\n'
142-
.format(fmt=column_format))
143-
144-
if self.caption is None and self.label is None:
145-
pass
146-
else:
147-
if self.caption is None:
148-
pass
149-
else:
150-
buf.write('\\caption{{{}}}'.format(self.caption))
116+
self._write_tabular_begin(buf, column_format)
151117

152-
if self.label is None:
153-
pass
154-
else:
155-
buf.write('\\label{{{}}}'.format(self.label))
156-
157-
# a double-backslash is required at the end of the line
158-
# as discussed here:
159-
# https://tex.stackexchange.com/questions/219138
160-
buf.write('\\\\\n')
161-
162-
buf.write('\\toprule\n')
118+
buf.write('\\toprule\n')
163119

164120
ilevels = self.frame.index.nlevels
165121
clevels = self.frame.columns.nlevels
@@ -210,15 +166,10 @@ def pad_empties(x):
210166
if self.multirow and i < len(strrows) - 1:
211167
self._print_cline(buf, i, len(strcols))
212168

213-
if not self.longtable:
214-
buf.write('\\bottomrule\n')
215-
buf.write('\\end{tabular}\n')
216-
if use_table_env:
217-
buf.write('\\end{table}\n')
218-
else:
219-
pass
169+
if self.longtable:
170+
self._write_longtable_end(buf)
220171
else:
221-
buf.write('\\end{longtable}\n')
172+
self._write_tabular_end(buf)
222173

223174
def _format_multicolumn(self, row, ilevels):
224175
r"""
@@ -294,3 +245,74 @@ def _print_cline(self, buf, i, icol):
294245
.format(cl=cl[1], icol=icol))
295246
# remove entries that have been written to buffer
296247
self.clinebuf = [x for x in self.clinebuf if x[0] != i]
248+
249+
def _write_tabular_begin(self, buf, column_format):
250+
"""
251+
write the beginning of a tabular environment or
252+
nested table/tabular environments including caption and label
253+
"""
254+
if self.caption is None and self.label is None:
255+
# then write output only in a tabular environment
256+
pass
257+
else:
258+
# then write output in a nested table/tabular environment
259+
if self.caption is None:
260+
caption_ = ''
261+
else:
262+
caption_ = '\n\\caption{{{}}}'.format(self.caption)
263+
264+
if self.label is None:
265+
label_ = ''
266+
else:
267+
label_ = '\n\\label{{{}}}'.format(self.label)
268+
269+
buf.write('\\begin{{table}}\n\\centering{}{}\n'.format(
270+
caption_,
271+
label_
272+
))
273+
274+
buf.write('\\begin{{tabular}}{{{fmt}}}\n'.format(fmt=column_format))
275+
276+
def _write_longtable_begin(self, buf, column_format):
277+
"""
278+
write the beginning of a longtable environment including caption and
279+
label if provided by user
280+
"""
281+
buf.write('\\begin{{longtable}}{{{fmt}}}\n'.format(fmt=column_format))
282+
283+
if self.caption is None and self.label is None:
284+
pass
285+
else:
286+
if self.caption is None:
287+
pass
288+
else:
289+
buf.write('\\caption{{{}}}'.format(self.caption))
290+
291+
if self.label is None:
292+
pass
293+
else:
294+
buf.write('\\label{{{}}}'.format(self.label))
295+
296+
# a double-backslash is required at the end of the line
297+
# as discussed here:
298+
# https://tex.stackexchange.com/questions/219138
299+
buf.write('\\\\\n')
300+
301+
def _write_tabular_end(self, buf):
302+
"""
303+
write the end of a tabular environment or nested table/tabular
304+
environment
305+
"""
306+
buf.write('\\bottomrule\n')
307+
buf.write('\\end{tabular}\n')
308+
if self.caption is None and self.label is None:
309+
pass
310+
else:
311+
buf.write('\\end{table}\n')
312+
313+
@staticmethod
314+
def _write_longtable_end(buf):
315+
"""
316+
write the end of a longtable environment
317+
"""
318+
buf.write('\\end{longtable}\n')

0 commit comments

Comments
 (0)