0
# Configuration
1
2
PyLaTeX provides a configuration system that allows you to customize the behavior of document generation. The configuration affects global settings like indentation, table styling, typography enhancements, and default values.
3
4
## Core Imports
5
6
```python
7
import pylatex.config
8
from pylatex.config import Version1, Version2, Default, NextMajor
9
```
10
11
## Basic Usage
12
13
```python
14
import pylatex.config
15
from pylatex import Document, Table
16
17
# Use the default configuration
18
doc = Document()
19
20
# Change global configuration
21
pylatex.config.active = pylatex.config.Version2()
22
23
# Use configuration temporarily
24
with Version1(indent=False).use():
25
# Code here uses Version1 config with no indentation
26
doc = Document()
27
28
# Override specific attributes temporarily
29
with pylatex.config.active.change(booktabs=True, indent=False):
30
# Code here uses current config but with booktabs enabled and no indent
31
table = Table()
32
```
33
34
## Capabilities
35
36
### Configuration Classes
37
38
Configuration classes define sets of default behaviors for PyLaTeX document generation.
39
40
```python { .api }
41
class Version1:
42
"""Configuration for v1.x.y library behavior.
43
44
Default attributes:
45
- indent = True
46
- booktabs = False
47
- microtype = False
48
- row_height = None
49
"""
50
def __init__(self, **kwargs): ...
51
def use(self): ...
52
def change(self, **kwargs): ...
53
54
class Version2(Version1):
55
"""Configuration for v2.x.y library behavior.
56
57
Default attributes:
58
- indent = False
59
- booktabs = True
60
- microtype = True
61
- row_height = 1.3
62
"""
63
64
# Configuration aliases
65
Default = Version1 # Current default configuration
66
NextMajor = Version2 # Next major release configuration
67
```
68
69
### Configuration Attributes
70
71
Core configuration attributes that control PyLaTeX behavior:
72
73
- **indent**: `bool` - Whether to indent LaTeX code for readability
74
- **booktabs**: `bool` - Use booktabs package for professional table styling
75
- **microtype**: `bool` - Enable microtype package for improved typography
76
- **row_height**: `float | None` - Default row height multiplier for tables
77
78
### Context Managers
79
80
Temporarily override configuration settings using context managers.
81
82
```python { .api }
83
def use(self):
84
"""Use the configuration temporarily in a specific context.
85
86
Returns:
87
Context manager for temporary configuration switching
88
"""
89
90
def change(self, **kwargs):
91
"""Override specific attributes temporarily.
92
93
Args:
94
**kwargs: Configuration attributes to override
95
96
Returns:
97
Context manager that yields the modified configuration
98
"""
99
```
100
101
### Global Configuration Access
102
103
```python { .api }
104
# Global active configuration instance
105
active: Version1 # Current active configuration
106
```
107
108
## Configuration Examples
109
110
### Basic Configuration Switching
111
112
```python
113
import pylatex.config
114
from pylatex import Document
115
116
# Use Version1 (default) behavior
117
doc1 = Document() # Uses indentation, no booktabs
118
119
# Switch to Version2 globally
120
pylatex.config.active = pylatex.config.Version2()
121
doc2 = Document() # No indentation, uses booktabs
122
123
# Switch back to Version1
124
pylatex.config.active = pylatex.config.Version1()
125
```
126
127
### Temporary Configuration Changes
128
129
```python
130
import pylatex.config
131
from pylatex import Document, Table
132
133
# Temporarily use Version2 behavior
134
with pylatex.config.Version2().use():
135
doc = Document() # Uses Version2 settings
136
table = Table() # Uses booktabs=True, row_height=1.3
137
138
# Back to original configuration after context
139
doc2 = Document() # Uses original settings
140
```
141
142
### Custom Configuration Override
143
144
```python
145
import pylatex.config
146
from pylatex import Document
147
148
# Create custom configuration
149
custom_config = pylatex.config.Version1(
150
indent=False,
151
booktabs=True,
152
microtype=True
153
)
154
155
# Use custom configuration temporarily
156
with custom_config.use():
157
doc = Document() # Uses custom settings
158
```
159
160
### Attribute-Specific Override
161
162
```python
163
import pylatex.config
164
from pylatex import Table
165
166
# Override only specific attributes
167
with pylatex.config.active.change(booktabs=True, row_height=1.5):
168
table = Table() # Uses booktabs with custom row height
169
# Other settings remain unchanged
170
```
171
172
## Configuration Impact
173
174
Different configuration settings affect various aspects of document generation:
175
176
### Indentation Control
177
- `indent=True`: Generated LaTeX code is indented for readability
178
- `indent=False`: Compact LaTeX output without indentation
179
180
### Table Styling
181
- `booktabs=True`: Uses booktabs package for professional table appearance
182
- `booktabs=False`: Uses standard LaTeX table styling
183
184
### Typography Enhancement
185
- `microtype=True`: Enables microtype package for improved character spacing
186
- `microtype=False`: Standard LaTeX typography
187
188
### Row Height
189
- `row_height=1.3`: Tables use 1.3x line spacing for better readability
190
- `row_height=None`: Uses LaTeX default row spacing