-
Notifications
You must be signed in to change notification settings - Fork 0
/
latex_interfacing.py
185 lines (140 loc) · 5.05 KB
/
latex_interfacing.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
import os
# LaTeX Interfacing Classes
# by Jaron Cui
# 8/17/2021
# This file contains classes for interfacing with common features of LaTeX.
# Documents can be exported as .tex and .pdf.
# (As long as your computer knows how to compile .tex to .pdf automatically)
# Generic document class with minimal packages
class Document(object):
# Constructor
def __init__(self):
self.preamble = set()
self.margin = 1
self.elements = []
self.usepackage("{fancyhdr}")
# Adds some text to the technical preamble
def add_to_preamble(self, text):
for line in str.split(text, "\n"):
self.preamble.add(line)
# Adds a \usepackage statement to the preamble to import a package
def usepackage(self, package):
self.add_to_preamble("\\usepackage" + package)
# Sets the margin of the document
def set_margin(self, amount):
self.usepackage("[margin=" + amount + "]{geometry}")
# Adds a document element
def add_element(self, element):
self.elements.append(element)
# Adds raw LaTeX script to the document
def add_raw(self, text):
self.add_element(Text(text))
# Exports the document as a .tex and .pdf
def export(self, name):
filename = name + ".tex"
out = open(filename, "w")
def writelines(lines):
newlines = []
for line in lines:
newlines.append(line + "\n")
out.writelines(newlines)
out.write("\\documentclass{article}\n")
writelines(self.preamble)
out.write("\\begin{document}\n")
for element in self.elements:
print(type(element.get_lines()))
writelines(element.get_lines())
out.write("\\end{document}\n")
out.close()
os.system("pdflatex " + filename)
# Represents a document with additional presets used often for academic worksheets
class AcademicDocument(Document):
def __init__(self):
super().__init__()
self.set_margin("1in")
self.usepackage("[english]{babel}")
self.usepackage("[utf8]{inputenc}")
self.usepackage("{array}")
self.usepackage("{rotating}")
# DOCUMENT ELEMENTS
# A textual document element
class Text:
def __init__(self, text):
if isinstance(text, str):
self.lines = str.split(text, "\n")
else:
self.lines = text
# Adds a line of text to the element
def add_line(self, line):
self.lines.append(line)
# Returns a list of text lines
def get_lines(self):
return self.lines
# A document element that ends the current page and starts a new one
class NewPage(Text):
def __init__(self):
super().__init__("\\newpage")
# A document element that begins a new section
class Section(Text):
def __init__(self, name):
super().__init__("\\section*{" + name + "}")
# A generic bounded element in LaTeX (i.e. uses \begin and \end)
class BoundedElement(Text):
def __init__(self, name, parameters, *args):
contents = Text("") if len(args) == 0 else args[0]
super().__init__(contents.get_lines())
self.begin = "\\begin{" + name + "}" + parameters
self.end = "\\end{" + name + "}"
# Wraps the lines in the appropriate \begin and \end statements
def get_lines(self):
return [self.begin] + Text.get_lines(self) + [self.end]
# A document element that aligns its contents to the left
class FlushLeft(BoundedElement):
def __init__(self, contents):
super().__init__("flushleft", "", contents)
# A document element that centers its contents
class Center(BoundedElement):
def __init__(self, contents):
super().__init__("center", "", contents)
# A document element that aligns its contents to the right
class FlushRight(BoundedElement):
def __init__(self, contents):
super().__init__("flushright", "", contents)
# A document element that creates a configurable table
class Table(BoundedElement):
def __init__(self, column_count, column_width, row_spacing, outlined):
spacer = "|" if outlined else " "
config = spacer
for n in range(column_count):
config += "m{" + column_width + "}" + spacer
config = "{" + config + "}"
super().__init__("tabular", config)
self.column_count = column_count
self.row_spacing = row_spacing
# Sets the spacing between rows
def set_row_spacing(self, spacing):
self.row_spacing = spacing
# Sets the document elements contained in the table
def set_items(self, items):
def last_in_column(n):
return n % self.column_count == self.column_count - 1
self.lines = []
for n in range(len(items)):
item = items[n]
self.lines += item.get_lines()
self.lines.append(" \\\\[" + self.row_spacing + "]" \
if last_in_column(n) else " & ")
# A document element that sets the style of the current page
class SetStyle(Text):
def __init__(self):
super().__init__("")
def set_type(self, style_type):
self.add_line("\\pagestyle" + style_type)
def get_lines(self):
unique_id = str(self.__hash__())
return ["\\fancypagestyle{" + unique_id + "}{"] + Text.get_lines(self) \
+ ["}\\thispagestyle{" + unique_id + "}"]
# A document element that disables page numbers
class PageNumbersOff(Text):
def __init__(self):
super().__init__("\\pagenumbering{gobble}")