@@ -110,56 +110,12 @@ 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
- 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 )
140
115
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 )
151
117
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 ' )
163
119
164
120
ilevels = self .frame .index .nlevels
165
121
clevels = self .frame .columns .nlevels
@@ -210,15 +166,10 @@ def pad_empties(x):
210
166
if self .multirow and i < len (strrows ) - 1 :
211
167
self ._print_cline (buf , i , len (strcols ))
212
168
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 )
220
171
else :
221
- buf . write ( ' \\ end{longtable} \n ' )
172
+ self . _write_tabular_end ( buf )
222
173
223
174
def _format_multicolumn (self , row , ilevels ):
224
175
r"""
@@ -294,3 +245,74 @@ def _print_cline(self, buf, i, icol):
294
245
.format (cl = cl [1 ], icol = icol ))
295
246
# remove entries that have been written to buffer
296
247
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