File: basic_inheritance.py

package info (click to toggle)
python-pylatex 1.4.2%2Bds-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,044 kB
  • sloc: python: 3,810; sh: 209; makefile: 169; xml: 12
file content (45 lines) | stat: -rw-r--r-- 1,393 bytes parent folder | download
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
#!/usr/bin/env python3
"""
This example shows basic document generation functionality by inheritance.

..  :copyright: (c) 2017 by Matthias Brandt.
    :license: MIT, see License for more details.
"""

# begin-doc-include
from pylatex import Command, Document, Section, Subsection
from pylatex.utils import NoEscape, italic


class MyDocument(Document):
    def __init__(self):
        super().__init__()

        self.preamble.append(Command("title", "Awesome Title"))
        self.preamble.append(Command("author", "Anonymous author"))
        self.preamble.append(Command("date", NoEscape(r"\today")))
        self.append(NoEscape(r"\maketitle"))

    def fill_document(self):
        """Add a section, a subsection and some text to the document."""
        with self.create(Section("A section")):
            self.append("Some regular text and some ")
            self.append(italic("italic text. "))

            with self.create(Subsection("A subsection")):
                self.append("Also some crazy characters: $&#{}")


if __name__ == "__main__":
    # Document
    doc = MyDocument()

    # Call function to add text
    doc.fill_document()

    # Add stuff to the document
    with doc.create(Section("A second section")):
        doc.append("Some text.")

    doc.generate_pdf("basic_inheritance", clean_tex=False)
    tex = doc.dumps()  # The document as string in LaTeX syntax