0
# Lists and Enumerations
1
2
List environments including itemized lists, numbered lists, and description lists with customization options. PyLaTeX provides comprehensive support for LaTeX's list environments, enabling the creation of structured content with automatic numbering, custom symbols, and nested hierarchies.
3
4
## Capabilities
5
6
### Itemized Lists
7
8
The Itemize class creates bulleted lists with default or custom bullet symbols.
9
10
```python { .api }
11
class Itemize(List):
12
def __init__(self, *, options=None, **kwargs):
13
"""
14
Create an itemized (bulleted) list.
15
16
Parameters:
17
- options: str, list, or Options, custom list options
18
"""
19
20
def add_item(self, s):
21
"""
22
Add an item to the list.
23
24
Parameters:
25
- s: str or LatexObject, the item content
26
"""
27
```
28
29
Usage example:
30
31
```python
32
from pylatex import Document, Section
33
from pylatex.lists import Itemize
34
35
doc = Document()
36
37
with doc.create(Section('Project Requirements')):
38
with doc.create(Itemize()) as itemize:
39
itemize.add_item('User authentication system')
40
itemize.add_item('Database integration')
41
itemize.add_item('RESTful API endpoints')
42
itemize.add_item('Frontend user interface')
43
itemize.add_item('Comprehensive testing suite')
44
45
# Nested lists
46
with doc.create(Section('Technical Stack')):
47
with doc.create(Itemize()) as itemize:
48
itemize.add_item('Backend Technologies')
49
50
# Nested list
51
with itemize.create(Itemize()) as nested:
52
nested.add_item('Python 3.8+')
53
nested.add_item('Django framework')
54
nested.add_item('PostgreSQL database')
55
56
itemize.add_item('Frontend Technologies')
57
58
with itemize.create(Itemize()) as nested:
59
nested.add_item('React.js')
60
nested.add_item('TypeScript')
61
nested.add_item('CSS3 with Flexbox')
62
```
63
64
### Enumerated Lists
65
66
The Enumerate class creates numbered lists with support for custom numbering schemes and styles.
67
68
```python { .api }
69
class Enumerate(List):
70
def __init__(self, enumeration_symbol=None, *, options=None, **kwargs):
71
"""
72
Create an enumerated (numbered) list.
73
74
Parameters:
75
- enumeration_symbol: str, custom enumeration symbol pattern
76
- options: str, list, or Options, custom list options
77
78
Requires:
79
- enumitem package (automatically added when using enumeration_symbol)
80
"""
81
82
def add_item(self, s):
83
"""
84
Add an item to the enumerated list.
85
86
Parameters:
87
- s: str or LatexObject, the item content
88
"""
89
```
90
91
Usage example:
92
93
```python
94
from pylatex import Document, Section, NoEscape
95
from pylatex.lists import Enumerate
96
97
doc = Document()
98
99
with doc.create(Section('Implementation Steps')):
100
# Basic numbered list
101
with doc.create(Enumerate()) as enum:
102
enum.add_item('Design system architecture')
103
enum.add_item('Set up development environment')
104
enum.add_item('Implement core functionality')
105
enum.add_item('Write comprehensive tests')
106
enum.add_item('Deploy to production')
107
108
# Custom numbering styles
109
with doc.create(Section('Custom Numbering')):
110
# Roman numerals
111
with doc.create(Enumerate(enumeration_symbol=NoEscape(r'\roman*'))) as enum:
112
enum.add_item('First phase')
113
enum.add_item('Second phase')
114
enum.add_item('Third phase')
115
116
# Alphabetical
117
with doc.create(Enumerate(enumeration_symbol=NoEscape(r'\alph*)'))) as enum:
118
enum.add_item('Primary objective')
119
enum.add_item('Secondary objective')
120
enum.add_item('Tertiary objective')
121
122
# Custom format
123
with doc.create(Enumerate(enumeration_symbol=NoEscape(r'Step \arabic*:'))) as enum:
124
enum.add_item('Initialize variables')
125
enum.add_item('Process input data')
126
enum.add_item('Generate output')
127
128
# Nested enumerated lists
129
with doc.create(Section('Detailed Workflow')):
130
with doc.create(Enumerate()) as enum:
131
enum.add_item('Data Collection')
132
133
with enum.create(Enumerate(enumeration_symbol=NoEscape(r'\alph*)'))) as subenum:
134
subenum.add_item('Survey participants')
135
subenum.add_item('Gather measurements')
136
subenum.add_item('Record observations')
137
138
enum.add_item('Data Analysis')
139
140
with enum.create(Enumerate(enumeration_symbol=NoEscape(r'\alph*)'))) as subenum:
141
subenum.add_item('Statistical processing')
142
subenum.add_item('Visualization')
143
subenum.add_item('Interpretation')
144
```
145
146
### Description Lists
147
148
The Description class creates definition-style lists with terms and their descriptions.
149
150
```python { .api }
151
class Description(List):
152
def __init__(self, *, options=None, **kwargs):
153
"""
154
Create a description list.
155
156
Parameters:
157
- options: str, list, or Options, custom list options
158
"""
159
160
def add_item(self, label, s):
161
"""
162
Add an item to the description list.
163
164
Parameters:
165
- label: str, the term or label to define
166
- s: str or LatexObject, the description content
167
"""
168
```
169
170
Usage example:
171
172
```python
173
from pylatex import Document, Section, Command
174
from pylatex.lists import Description
175
176
doc = Document()
177
178
with doc.create(Section('Terminology')):
179
with doc.create(Description()) as desc:
180
desc.add_item('API', 'Application Programming Interface - a set of protocols and tools for building software applications')
181
desc.add_item('REST', 'Representational State Transfer - an architectural style for distributed hypermedia systems')
182
desc.add_item('CRUD', 'Create, Read, Update, Delete - the four basic operations of persistent storage')
183
desc.add_item('ORM', 'Object-Relational Mapping - programming technique for converting data between incompatible type systems')
184
185
# Enhanced formatting
186
with doc.create(Section('Technical Specifications')):
187
with doc.create(Description()) as desc:
188
desc.add_item(
189
Command('textbf', 'Python'),
190
'High-level programming language with dynamic semantics and extensive standard library'
191
)
192
desc.add_item(
193
Command('textbf', 'Django'),
194
'Web framework that encourages rapid development and clean, pragmatic design'
195
)
196
desc.add_item(
197
Command('textbf', 'PostgreSQL'),
198
'Open-source relational database system with strong reputation for reliability and performance'
199
)
200
```
201
202
## Advanced List Features
203
204
### Custom List Styling
205
206
```python
207
from pylatex import Document, Section, Package, NoEscape
208
from pylatex.lists import Itemize, Enumerate
209
from pylatex.base_classes import Options
210
211
doc = Document()
212
213
# Add enumitem package for advanced customization
214
doc.packages.append(Package('enumitem'))
215
216
with doc.create(Section('Custom Styled Lists')):
217
# Custom bullet symbols
218
custom_options = Options()
219
custom_options.append(NoEscape(r'label=\textbullet'))
220
221
with doc.create(Itemize(options=custom_options)) as itemize:
222
itemize.add_item('Custom bullet point')
223
itemize.add_item('Another bullet point')
224
225
# Compact spacing
226
compact_options = Options()
227
compact_options.append(NoEscape('nosep'))
228
229
with doc.create(Enumerate(options=compact_options)) as enum:
230
enum.add_item('Tight spacing item 1')
231
enum.add_item('Tight spacing item 2')
232
enum.add_item('Tight spacing item 3')
233
234
# Custom indentation
235
indent_options = Options()
236
indent_options.append(NoEscape('leftmargin=2cm'))
237
indent_options.append(NoEscape('itemindent=1cm'))
238
239
with doc.create(Itemize(options=indent_options)) as itemize:
240
itemize.add_item('Custom indented item')
241
itemize.add_item('Another indented item')
242
```
243
244
### Multi-Level Lists
245
246
```python
247
from pylatex import Document, Section, NoEscape
248
from pylatex.lists import Enumerate, Itemize
249
250
doc = Document()
251
252
with doc.create(Section('Project Hierarchy')):
253
with doc.create(Enumerate()) as main_list:
254
main_list.add_item('Frontend Development')
255
256
with main_list.create(Itemize()) as sub_items:
257
sub_items.add_item('Component Architecture')
258
259
with sub_items.create(Enumerate(enumeration_symbol=NoEscape(r'\alph*)'))) as subsub:
260
subsub.add_item('Header component')
261
subsub.add_item('Navigation component')
262
subsub.add_item('Footer component')
263
264
sub_items.add_item('State Management')
265
sub_items.add_item('Routing System')
266
267
main_list.add_item('Backend Development')
268
269
with main_list.create(Itemize()) as sub_items:
270
sub_items.add_item('Database Design')
271
sub_items.add_item('API Endpoints')
272
sub_items.add_item('Authentication')
273
274
main_list.add_item('Testing Strategy')
275
```
276
277
### Mixed Content Lists
278
279
```python
280
from pylatex import Document, Section, Command, NoEscape
281
from pylatex.lists import Enumerate, Description
282
from pylatex.math import Math
283
284
doc = Document()
285
286
with doc.create(Section('Mathematical Concepts')):
287
with doc.create(Description()) as desc:
288
# List items with mathematical content
289
desc.add_item(
290
'Quadratic Formula',
291
Math(data=NoEscape(r'x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}'), inline=True)
292
)
293
294
desc.add_item(
295
'Pythagorean Theorem',
296
['For a right triangle: ', Math(data=NoEscape(r'a^2 + b^2 = c^2'), inline=True)]
297
)
298
299
desc.add_item(
300
Command('textit', 'Euler\'s Identity'),
301
Math(data=NoEscape(r'e^{i\pi} + 1 = 0'), inline=True)
302
)
303
304
# Code examples in lists
305
with doc.create(Section('Programming Examples')):
306
with doc.create(Enumerate()) as enum:
307
enum.add_item([
308
'Variable declaration: ',
309
NoEscape(r'\texttt{x = 10}')
310
])
311
enum.add_item([
312
'Function definition: ',
313
NoEscape(r'\texttt{def func(param):}')
314
])
315
enum.add_item([
316
'Class instantiation: ',
317
NoEscape(r'\texttt{obj = MyClass()}')
318
])
319
```
320
321
## List Formatting and Styling
322
323
### Spacing and Layout Control
324
325
```python
326
from pylatex import Document, Package, Command, NoEscape
327
from pylatex.lists import Itemize, Enumerate
328
from pylatex.base_classes import Options
329
330
doc = Document()
331
doc.packages.append(Package('enumitem'))
332
333
# Global list settings
334
doc.preamble.append(Command('setlist', options='itemsep=0.5em,parsep=0pt,partopsep=0pt'))
335
336
with doc.create(Section('Controlled Spacing')):
337
# No extra spacing
338
tight_options = Options(['noitemsep', 'topsep=0pt', 'parsep=0pt'])
339
340
with doc.create(Itemize(options=tight_options)) as itemize:
341
itemize.add_item('Compact item 1')
342
itemize.add_item('Compact item 2')
343
itemize.add_item('Compact item 3')
344
345
# Extra spacing
346
spaced_options = Options(['itemsep=1em', 'topsep=1em'])
347
348
with doc.create(Enumerate(options=spaced_options)) as enum:
349
enum.add_item('Spaced item 1')
350
enum.add_item('Spaced item 2')
351
enum.add_item('Spaced item 3')
352
```
353
354
### Custom Symbols and Numbering
355
356
```python
357
from pylatex import Document, Section, Package, NoEscape, Command
358
from pylatex.lists import Itemize, Enumerate
359
360
doc = Document()
361
doc.packages.append(Package('enumitem'))
362
doc.packages.append(Package('pifont')) # For special symbols
363
364
with doc.create(Section('Custom Symbols')):
365
# Special bullet symbols
366
with doc.create(Itemize(enumeration_symbol=NoEscape(r'\ding{43}'))) as itemize:
367
itemize.add_item('Star bullet point')
368
itemize.add_item('Another star point')
369
370
# Arrow bullets
371
with doc.create(Itemize(enumeration_symbol=NoEscape(r'$\rightarrow$'))) as itemize:
372
itemize.add_item('Arrow bullet point')
373
itemize.add_item('Another arrow point')
374
375
# Custom numbering with prefixes/suffixes
376
with doc.create(Enumerate(enumeration_symbol=NoEscape(r'Task \arabic*:'))) as enum:
377
enum.add_item('Complete requirements analysis')
378
enum.add_item('Design system architecture')
379
enum.add_item('Implement core features')
380
```
381
382
### Continuation and Resuming
383
384
```python
385
from pylatex import Document, Section, NoEscape
386
from pylatex.lists import Enumerate
387
from pylatex.base_classes import Options
388
389
doc = Document()
390
391
with doc.create(Section('Continuing Lists')):
392
# First part of list
393
with doc.create(Enumerate()) as enum:
394
enum.add_item('First item')
395
enum.add_item('Second item')
396
enum.add_item('Third item')
397
398
doc.append('Some intervening text that breaks the list.')
399
400
# Resume numbering
401
resume_options = Options(['resume'])
402
403
with doc.create(Enumerate(options=resume_options)) as enum:
404
enum.add_item('Fourth item (continued)')
405
enum.add_item('Fifth item (continued)')
406
```
407
408
## Practical List Applications
409
410
### Task Lists and Checklists
411
412
```python
413
from pylatex import Document, Section, Package, NoEscape
414
from pylatex.lists import Itemize
415
416
doc = Document()
417
doc.packages.append(Package('amssymb')) # For checkboxes
418
419
with doc.create(Section('Project Checklist')):
420
checkboxes = [
421
(NoEscape(r'$\boxtimes$'), 'Requirements gathering', 'completed'),
422
(NoEscape(r'$\boxtimes$'), 'System design', 'completed'),
423
(NoEscape(r'$\square$'), 'Implementation', 'pending'),
424
(NoEscape(r'$\square$'), 'Testing', 'pending'),
425
(NoEscape(r'$\square$'), 'Deployment', 'pending')
426
]
427
428
with doc.create(Itemize()) as itemize:
429
for checkbox, task, status in checkboxes:
430
itemize.add_item([checkbox, ' ', task])
431
```
432
433
### Bibliography-Style Lists
434
435
```python
436
from pylatex import Document, Section, Command
437
from pylatex.lists import Description
438
439
doc = Document()
440
441
with doc.create(Section('References')):
442
with doc.create(Description()) as desc:
443
desc.add_item(
444
'[1]',
445
'Smith, J. (2023). "Advanced LaTeX Techniques." '
446
+ Command('textit', 'Journal of Document Processing') + ', 15(3), 123-145.'
447
)
448
desc.add_item(
449
'[2]',
450
'Johnson, A. & Brown, B. (2022). "Python for Scientific Writing." '
451
+ 'Academic Press, New York.'
452
)
453
desc.add_item(
454
'[3]',
455
'Davis, C. (2024). "Automated Document Generation." '
456
+ 'Proceedings of the International Conference on Documentation.'
457
)
458
```
459
460
### Feature Comparison Lists
461
462
```python
463
from pylatex import Document, Section, Command
464
from pylatex.lists import Description
465
from pylatex.table import Tabular
466
467
doc = Document()
468
469
with doc.create(Section('Feature Comparison')):
470
with doc.create(Description()) as desc:
471
desc.add_item(
472
Command('textbf', 'Basic Plan'),
473
'Up to 5 users, 10GB storage, email support'
474
)
475
desc.add_item(
476
Command('textbf', 'Professional Plan'),
477
'Up to 25 users, 100GB storage, priority support, advanced analytics'
478
)
479
desc.add_item(
480
Command('textbf', 'Enterprise Plan'),
481
'Unlimited users, 1TB storage, dedicated support, custom integrations'
482
)
483
```
484
485
## Error Handling and Best Practices
486
487
### Empty List Handling
488
489
Lists in PyLaTeX automatically handle empty content through the `omit_if_empty` attribute:
490
491
```python
492
from pylatex.lists import Itemize
493
494
# This will not produce LaTeX output if no items are added
495
empty_list = Itemize()
496
# empty_list.add_item() # No items added
497
498
# This ensures the list appears in output
499
non_empty_list = Itemize()
500
non_empty_list.add_item('At least one item')
501
```
502
503
### Package Requirements
504
505
Different list features require specific packages:
506
507
- **Basic lists**: No additional packages
508
- **Custom enumeration**: `enumitem` package (automatically added)
509
- **Special symbols**: `pifont`, `amssymb`, or `textcomp` packages
510
- **Unicode symbols**: May require `fontspec` with XeLaTeX or LuaLaTeX
511
512
```python
513
from pylatex import Document, Package
514
515
doc = Document()
516
doc.packages.append(Package('enumitem'))
517
doc.packages.append(Package('pifont'))
518
doc.packages.append(Package('amssymb'))
519
```
520
521
The list system in PyLaTeX provides flexible and powerful tools for creating structured content with professional formatting and extensive customization options.