Skip to content

Commit 3eb433b

Browse files
committed
reverted to methodology of commit 911a491 (pandas-dev#25436)
1 parent 961aed3 commit 3eb433b

File tree

1 file changed

+63
-71
lines changed

1 file changed

+63
-71
lines changed

pandas/io/formats/latex.py

+63-71
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,10 @@ def pad_empties(x):
102102
raise AssertionError('column_format must be str or unicode, '
103103
'not {typ}'.format(typ=type(column_format)))
104104

105-
buf.write(self._build_latex_begin_env(column_format))
105+
if self.longtable:
106+
self._write_longtable_begin(buf, column_format)
107+
else:
108+
self._write_tabular_begin(buf, column_format)
106109

107110
buf.write('\\toprule\n')
108111

@@ -155,7 +158,10 @@ def pad_empties(x):
155158
if self.multirow and i < len(strrows) - 1:
156159
self._print_cline(buf, i, len(strcols))
157160

158-
buf.write(self._build_latex_end_env())
161+
if self.longtable:
162+
self._write_longtable_end(buf)
163+
else:
164+
self._write_tabular_end(buf)
159165

160166
def _format_multicolumn(self, row, ilevels):
161167
r"""
@@ -232,87 +238,73 @@ def _print_cline(self, buf, i, icol):
232238
# remove entries that have been written to buffer
233239
self.clinebuf = [x for x in self.clinebuf if x[0] != i]
234240

235-
def _build_latex_begin_env(self, column_format):
241+
def _write_tabular_begin(self, buf, column_format):
236242
"""
237-
Write the beginning of the latex environment.
238-
239-
This can be a tabular environment or nested table/tabular environments
240-
including caption/label depending on the arguments passed to
241-
`LatexFormatter.__init__()`.
242-
243-
Returns
244-
-------
245-
string
246-
to be written to `buf`
243+
write the beginning of a tabular environment or
244+
nested table/tabular environments including caption and label
247245
"""
248-
str_ = ''
249-
if self.longtable:
250-
str_ += '\\begin{{longtable}}{{{fmt}}}\n'.format(fmt=column_format)
251-
252-
if self.caption is None and self.label is None:
253-
pass
254-
else:
255-
if self.caption is None:
256-
pass
257-
else:
258-
str_ += '\\caption{{{}}}'.format(self.caption)
259-
260-
if self.label is None:
261-
pass
262-
else:
263-
str_ += '\\label{{{}}}'.format(self.label)
264-
265-
# a double-backslash is required at the end of the line
266-
# as discussed here:
267-
# https://tex.stackexchange.com/questions/219138
268-
str_ += '\\\\\n'
246+
if self.caption is None and self.label is None:
247+
# then write output only in a tabular environment
248+
pass
269249
else:
270-
if self.caption is None and self.label is None:
271-
# then write output only in a tabular environment
272-
pass
250+
# then write output in a nested table/tabular environment
251+
if self.caption is None:
252+
caption_ = ''
273253
else:
274-
# then write output in a nested table/tabular environment
275-
if self.caption is None:
276-
caption_ = ''
277-
else:
278-
caption_ = '\n\\caption{{{}}}'.format(self.caption)
279-
280-
if self.label is None:
281-
label_ = ''
282-
else:
283-
label_ = '\n\\label{{{}}}'.format(self.label)
254+
caption_ = '\n\\caption{{{}}}'.format(self.caption)
284255

285-
str_ += '\\begin{{table}}\n\\centering{}{}\n'.format(
286-
caption_,
287-
label_
288-
)
256+
if self.label is None:
257+
label_ = ''
258+
else:
259+
label_ = '\n\\label{{{}}}'.format(self.label)
289260

290-
str_ += '\\begin{{tabular}}{{{fmt}}}\n'.format(fmt=column_format)
261+
buf.write('\\begin{{table}}\n\\centering{}{}\n'.format(
262+
caption_,
263+
label_
264+
))
291265

292-
return str_
266+
buf.write('\\begin{{tabular}}{{{fmt}}}\n'.format(fmt=column_format))
293267

294-
def _build_latex_end_env(self):
268+
def _write_longtable_begin(self, buf, column_format):
295269
"""
296-
Write the end of the latex environment.
297-
298-
This can be a tabular environment or nested table/tabular environments
299-
depending on the arguments passed to `LatexFormatter.__init__()`.
300-
301-
Returns
302-
-------
303-
string
304-
to be written to `buf`
270+
write the beginning of a longtable environment including caption and
271+
label if provided by user
305272
"""
306-
str_ = ''
273+
buf.write('\\begin{{longtable}}{{{fmt}}}\n'.format(fmt=column_format))
307274

308-
if self.longtable:
309-
str_ += '\\end{longtable}\n'
275+
if self.caption is None and self.label is None:
276+
pass
310277
else:
311-
str_ += '\\bottomrule\n'
312-
str_ += '\\end{tabular}\n'
313-
if self.caption is None and self.label is None:
278+
if self.caption is None:
314279
pass
315280
else:
316-
str_ += '\\end{table}\n'
281+
buf.write('\\caption{{{}}}'.format(self.caption))
282+
283+
if self.label is None:
284+
pass
285+
else:
286+
buf.write('\\label{{{}}}'.format(self.label))
287+
288+
# a double-backslash is required at the end of the line
289+
# as discussed here:
290+
# https://tex.stackexchange.com/questions/219138
291+
buf.write('\\\\\n')
317292

318-
return str_
293+
def _write_tabular_end(self, buf):
294+
"""
295+
write the end of a tabular environment or nested table/tabular
296+
environment
297+
"""
298+
buf.write('\\bottomrule\n')
299+
buf.write('\\end{tabular}\n')
300+
if self.caption is None and self.label is None:
301+
pass
302+
else:
303+
buf.write('\\end{table}\n')
304+
305+
@staticmethod
306+
def _write_longtable_end(buf):
307+
"""
308+
write the end of a longtable environment
309+
"""
310+
buf.write('\\end{longtable}\n')

0 commit comments

Comments
 (0)