@@ -110,10 +110,7 @@ def pad_empties(x):
110
110
raise AssertionError ('column_format must be str or unicode, '
111
111
'not {typ}' .format (typ = type (column_format )))
112
112
113
- if self .longtable :
114
- self ._write_longtable_begin (buf , column_format )
115
- else :
116
- self ._write_tabular_begin (buf , column_format )
113
+ buf .write (self ._build_latex_begin_env (column_format ))
117
114
118
115
buf .write ('\\ toprule\n ' )
119
116
@@ -166,10 +163,7 @@ def pad_empties(x):
166
163
if self .multirow and i < len (strrows ) - 1 :
167
164
self ._print_cline (buf , i , len (strcols ))
168
165
169
- if self .longtable :
170
- self ._write_longtable_end (buf )
171
- else :
172
- self ._write_tabular_end (buf )
166
+ buf .write (self ._build_latex_end_env ())
173
167
174
168
def _format_multicolumn (self , row , ilevels ):
175
169
r"""
@@ -246,73 +240,81 @@ def _print_cline(self, buf, i, icol):
246
240
# remove entries that have been written to buffer
247
241
self .clinebuf = [x for x in self .clinebuf if x [0 ] != i ]
248
242
249
- def _write_tabular_begin (self , buf , column_format ):
243
+ def _build_latex_begin_env (self , column_format ):
250
244
"""
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 )
245
+ Write the beginning of the latex environment.
263
246
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 ))
247
+ This can be a tabular environment or nested table/tabular environments
248
+ including caption/label depending on the arguments passed to
249
+ ``LatexFormatter.__init__()``.
275
250
276
- def _write_longtable_begin ( self , buf , column_format ):
251
+ :return: string to be written to ``buf``
277
252
"""
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 ))
253
+ str_ = ''
254
+ if self .longtable :
255
+ str_ += '\\ begin{{longtable}}{{{fmt}}}\n ' .format (fmt = column_format )
282
256
283
- if self .caption is None and self .label is None :
284
- pass
285
- else :
286
- if self .caption is None :
257
+ if self .caption is None and self .label is None :
287
258
pass
288
259
else :
289
- buf .write ('\\ caption{{{}}}' .format (self .caption ))
290
-
291
- if self .label is None :
260
+ if self .caption is None :
261
+ pass
262
+ else :
263
+ str_ += '\\ caption{{{}}}' .format (self .caption )
264
+
265
+ if self .label is None :
266
+ pass
267
+ else :
268
+ str_ += '\\ label{{{}}}' .format (self .label )
269
+
270
+ # a double-backslash is required at the end of the line
271
+ # as discussed here:
272
+ # https://tex.stackexchange.com/questions/219138
273
+ str_ += '\\ \\ \n '
274
+ else :
275
+ if self .caption is None and self .label is None :
276
+ # then write output only in a tabular environment
292
277
pass
293
278
else :
294
- buf .write ('\\ label{{{}}}' .format (self .label ))
279
+ # then write output in a nested table/tabular environment
280
+ if self .caption is None :
281
+ caption_ = ''
282
+ else :
283
+ caption_ = '\n \\ caption{{{}}}' .format (self .caption )
284
+
285
+ if self .label is None :
286
+ label_ = ''
287
+ else :
288
+ label_ = '\n \\ label{{{}}}' .format (self .label )
289
+
290
+ str_ += '\\ begin{{table}}\n \\ centering{}{}\n ' .format (
291
+ caption_ ,
292
+ label_
293
+ )
295
294
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 ' )
295
+ str_ += '\\ begin{{tabular}}{{{fmt}}}\n ' .format (fmt = column_format )
300
296
301
- def _write_tabular_end (self , buf ):
297
+ return str_
298
+
299
+ def _build_latex_end_env (self ):
302
300
"""
303
- write the end of a tabular environment or nested table/tabular
304
- environment
301
+ Write the end of the latex environment.
302
+
303
+ This can be a tabular environment or nested table/tabular environments
304
+ depending on the arguments passed to ``LatexFormatter.__init__()``.
305
+
306
+ :return: string to be written to ``buf``
305
307
"""
306
- buf . write ( ' \\ bottomrule \n ' )
307
- buf . write ( ' \\ end{tabular} \n ' )
308
- if self .caption is None and self . label is None :
309
- pass
308
+ str_ = ''
309
+
310
+ if self .longtable :
311
+ str_ += ' \\ end{longtable} \n '
310
312
else :
311
- buf .write ('\\ end{table}\n ' )
313
+ str_ += '\\ bottomrule\n '
314
+ str_ += '\\ end{tabular}\n '
315
+ if self .caption is None and self .label is None :
316
+ pass
317
+ else :
318
+ str_ += '\\ end{table}\n '
312
319
313
- @staticmethod
314
- def _write_longtable_end (buf ):
315
- """
316
- write the end of a longtable environment
317
- """
318
- buf .write ('\\ end{longtable}\n ' )
320
+ return str_
0 commit comments