forked from JelteF/PyLaTeX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposition.py
178 lines (132 loc) · 4.75 KB
/
position.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
# -*- coding: utf-8 -*-
"""
This module implements the classes that deal with positioning.
Positions various elements on the page.
.. :copyright: (c) 2014 by Jelte Fennema.
:license: MIT, see License for more details.
"""
from .base_classes import Environment, SpecialOptions, Command, CommandBase
from .package import Package
from .utils import NoEscape
class HorizontalSpace(CommandBase):
"""Add/remove the amount of horizontal space between elements."""
_latex_name = 'hspace'
_repr_attributes_mapping = {
"size": "arguments"
}
def __init__(self, size, *, star=True):
"""
Args
----
size: str
The amount of space to add
star: bool
Use the star variant of the command. Enabling this makes sure the
space is also added where page breaking takes place.
"""
if star:
self.latex_name += '*'
super().__init__(arguments=size)
class VerticalSpace(HorizontalSpace):
"""Add the user specified amount of vertical space to the document."""
_latex_name = 'vspace'
class Center(Environment):
r"""Centered environment."""
packages = [Package('ragged2e')]
class FlushLeft(Center):
r"""Left-aligned environment."""
class FlushRight(Center):
r"""Right-aligned environment."""
class MiniPage(Environment):
r"""A class that allows the creation of minipages within document pages."""
packages = [Package('ragged2e')]
_repr_attributes_mapping = {
"width": "arguments",
"pos": "options",
"height": "options",
"content_pos": "options",
"align": "options"
}
def __init__(self, *, width=NoEscape(r'\textwidth'), pos=None,
height=None, content_pos=None, align=None, fontsize=None,
data=None):
r"""
Args
----
width: str
width of the minipage
pos: str
The vertical alignment of the minipage relative to the baseline
(center(c), top(t), bottom(b))
height: str
height of the minipage
content_pos: str
The position of the content inside the minipage (center(c),
bottom(b), top(t), spread(s))
align: str
alignment of the minibox
fontsize: str
The font size of the minipage
data: str or `~.LatexObject`
The data to place inside the MiniPage element
"""
options = []
if pos is not None:
options.append(pos)
if height is not None:
options.append(NoEscape(height))
if ((content_pos is not None) and (pos is not None) and
(height is not None)):
options.append(content_pos)
options = SpecialOptions(*options)
arguments = [NoEscape(str(width))]
extra_data = []
if align is not None:
if align == "l":
extra_data.append(Command(command="flushleft"))
elif align == "c":
extra_data.append(Command(command="centering"))
elif align == "r":
extra_data.append(Command(command="flushright"))
if fontsize is not None:
extra_data.append(Command(command=fontsize))
if data is not None:
if isinstance(data, list):
data = extra_data + data
else:
data = extra_data + [data]
else:
data = extra_data
super().__init__(arguments=arguments, options=options, data=data)
class TextBlock(Environment):
r"""A class that represents a textblock environment.
Make sure to set lengths of TPHorizModule and TPVertModule
"""
_repr_attributes_mapping = {
"width": "arguments"
}
packages = [Package('textpos')]
def __init__(self, width, horizontal_pos, vertical_pos, *,
indent=False, data=None):
r"""
Args
----
width: float
Width of the text block in the units specified by TPHorizModule
horizontal_pos: float
Horizontal position in units specified by the TPHorizModule
indent: bool
Determines whether the text block has an indent before it
vertical_pos: float
Vertical position in units specified by the TPVertModule
data: str or `~.LatexObject`
The data to place inside the TextBlock element
"""
arguments = width
self.horizontal_pos = horizontal_pos
self.vertical_pos = vertical_pos
super().__init__(arguments=arguments)
self.append("(%s, %s)" % (str(self.horizontal_pos),
str(self.vertical_pos)))
if not indent:
self.append(NoEscape(r'\noindent'))