forked from ryanvarley/latexpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjects.py
226 lines (169 loc) · 7.08 KB
/
objects.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import numpy as np
class LatexObject(object):
def __init__(self):
self.texLines = [] # the main list
self._caption = False
self._label = False
def _startObject(self):
return ['']
def _endObject(self):
return ['']
def _proccessTex(self):
return self.texLines
def addCaption(self, caption):
self._caption = '\\caption{' + caption + '}'
def addLabel(self, label):
self._label = '\\label{' + label + '}'
def output(self):
""" outputs tex as a string
"""
texFile = '\n'.join(self._startObject() + self.texLines + self._endObject())
return texFile
class MultiFigure(LatexObject):
""" Handles a multiple plots in a figure instance
"""
def __init__(self, pos='htbp', figsPerRow=2):
LatexObject.__init__(self)
self.pos = pos # position argument
self.centering = True
self.figsPerRow = figsPerRow
self.maxWidthUnit = '\\textwidth'
self.maxWidth = 1.0/figsPerRow
# internal variables for code only
self._figColNum = 1
def _getWidth(self):
return '{}{}'.format(self.maxWidth, self.maxWidthUnit)
def addFigure(self, path):
figColNum = self._figColNum
# TODO complicated code can be replaced by ' & '.join (possibly)
if len(self.texLines): # if we have a previous figure
if figColNum > 1: # not the first in the row
self.texLines[-1] += '&' # add endchar to previous figure now we are adding another fig
if figColNum < self.figsPerRow:
self._figColNum += 1
endChar = ''
elif figColNum == self.figsPerRow:
endChar = '\\\\' # add end row char if last in row
self._figColNum = 1
else:
raise ValueError('somethings wrong with the figure count')
figureTex = '\includegraphics[width={}]{}'.format(self._getWidth(), '{' + path + '}')
self.texLines.append(figureTex + endChar)
def _startObject(self):
startObject= ['\\begin{figure}[' + self.pos + ']']
if self._caption:
startObject.append(self._caption)
if self.centering:
startObject.append('\\centering')
startObject.append('\\begin{{tabular}}{{{}}}'.format('c' * self.figsPerRow))
return startObject
def _endObject(self):
endObject = ['\\end{tabular}']
if self._label:
endObject.append(self._label)
endObject.append('\\end{figure}')
return endObject
class Table(LatexObject):
""" Handles a table instance
"""
def __init__(self, columns):
LatexObject.__init__(self)
self.columns = columns
self.pos = 'htbp'
self.centering = True
self._header = False
self._layout = False # allows setting of manual layout
# Hooks
self.hook_BeforeTableStart = []
self._blankchar = '~'
def _startObject(self):
startObject= ['\\begin{table}[' + self.pos + ']']
if self._caption:
startObject.append(self._caption)
if self.centering:
startObject.append('\\centering')
if self.hook_BeforeTableStart: # Hook
startObject += self.hook_BeforeTableStart
if self._layout:
layout = self._layout
else:
layout = 'l' * self.columns
startObject.append('\\begin{tabular}{' + layout + '}')
if self._header:
startObject.append(self._header)
startObject.append('\hline')
return startObject
def addRow(self, rowList):
if len(rowList) != self.columns:
raise ValueError('You must give the exact number of columns each time (set {} got {}).'
' use np.nan for blanks'.format(self.columns, len(rowList)))
parsedRowList = [self._blankchar if str(val) == 'nan' else str(val) for val in rowList] # math.nan will fail is non float, np.nan only catches numpy nans
self.texLines.append(' & '.join(parsedRowList) + ' \\\\') # seperate values by & next cell char and lines by new line\\
def _endObject(self):
endObject = ['\\end{tabular}']
if self._label:
endObject.append(self._label)
endObject.append('\\end{table}')
return endObject
def addHeader(self, headerList):
""" Currently you can only have one header, it will be seperated from the rest by a line and will replace any
existing header
"""
# for ease and to avoid extra function calls just add row then pop it into the header
self.addRow(headerList)
self._header = self.texLines.pop()
class LongTable(Table):
""" Handles a long table instance, in this class header is the all pages header
"""
def __init__(self, columns):
Table.__init__(self, columns)
self._pageHeaderLabel = False
self._firstPageHeader = False
self._footer = False
self._lastPageFooter = False
def addFirstPageHeader(self, headerList):
""" Currently this is your caption + table headings
"""
raise NotImplementedError
def addPageHeaderLabel(self, headerLabel="\\tablename\ \\thetable\ -- \\textit{Continued from previous page}"):
""" i.e. continued from last page
"""
self._pageHeaderLabel = '\multicolumn{{{}}}{{l}}{{{}}}'.format(self.columns, headerLabel)
def addLastPageFooter(self, footertext):
self._lastPageFooter = footertext
def addFooter(self, footertext="\\textit{Continued on next page}"):
""" normally a label i.e. (continued on next page)
"""
self._footer = '\multicolumn{{{}}}{{l}}{{{}}}'.format(self.columns, footertext)
def _startObject(self):
if self._layout:
layout = self._layout
else:
layout = 'l' * self.columns
startObject = ['\\begin{longtable}[' + self.pos + ']{' + layout + '}']
# First Page Header
if self._caption:
startObject += [self._caption + '\\\\', '\\hline'] # caption has no linebreak by efault unlike addrow
if self._header:
startObject += [self._header, '\\hline']
startObject.append("\endfirsthead")
# Every other page Header
if self._header:
if self._pageHeaderLabel:
startObject.append(self._pageHeaderLabel + '\\\\')
startObject += ['\\hline', self._header, '\\hline', '\endhead']
elif self._pageHeaderLabel:
startObject += [self._pageHeaderLabel + '\\\\', '\\hline', '\endhead']
if self._footer:
startObject += [self._footer + '\\\\', '\endfoot']
if self._lastPageFooter:
startObject += ['\\hline', self._lastPageFooter + '\\\\', '\endlastfoot']
if self.hook_BeforeTableStart: # Hook
startObject += self.hook_BeforeTableStart
return startObject
def _endObject(self):
endObject = []
if self._label:
endObject.append(self._label)
endObject.append('\\end{longtable}')
return endObject