File size: 4,609 Bytes
c27d6d1
3f3baec
 
 
d101c51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f3baec
 
d101c51
3f3baec
 
 
 
 
 
109f376
 
3f3baec
109f376
3f3baec
 
109f376
3f3baec
 
 
 
e16c6ed
 
 
3f3baec
 
 
 
 
e16c6ed
3f3baec
 
 
 
e16c6ed
 
 
3f3baec
 
d101c51
 
3f3baec
 
 
 
 
 
 
 
 
 
 
 
d101c51
3f3baec
 
 
e16c6ed
 
3f3baec
e16c6ed
3f3baec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d101c51
3f3baec
 
 
 
 
 
 
 
 
 
d101c51
3f3baec
 
 
 
 
 
 
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
"""This module provides functionality to convert a JSON LLM output to LaTeX Beamer presentation."""

import json

from .latex_tools import (
    escape_latex_special_chars,
    replace_unicode_greek,
)


def escape_chars(text: str) -> str:
    """Escape selected characters.
    Args:
        text (str): Text to process.
    Returns:
        str: Processed text.
    """
    return replace_unicode_greek(escape_latex_special_chars(text))


def render_content(content: list[str], content_format: str) -> str:
    """Render content in LaTeX format depending if it is itemized list or plain text.
    Args:
        content (list[str]): List of content items.
        content_format (str): Format of the content, either "itemize" or "text".
    Returns:
        str: LaTeX formatted content.
    """
    # Remove blank or whitespace-only entries
    filtered_content = [c.strip() for c in content if c.strip()]
    if content_format == "text":
        return "\n\n".join(filtered_content)
    return (
        "\\begin{itemize}\n"
        + "\n".join(f"\\item {c}" for c in filtered_content)
        + "\n\\end{itemize}"
    )


def json_to_tex(
    presentation_json: str, theme: str, color_theme: str, aspect_ratio: str
) -> str:
    """Convert a JSON representation of a presentation to LaTeX Beamer format.
    Args:
        presentation (Dict[str, Any]): JSON LLM output of the presentation.
        theme (str): Beamer theme to use.
        color_theme (str): Beamer color theme to use.
        aspect_ratio (str): Aspect ratio for the presentation, e.g., "16:9", "4:3".
    Returns:
        str: LaTeX Beamer code as a string.
    """
    presentation = json.loads(presentation_json)
    aspect_ratio_num = 169 if aspect_ratio == "16:9" else 43
    header = f"""\\documentclass[aspectratio={aspect_ratio_num}]{{beamer}}
                \\usepackage[utf8]{{inputenc}}
                \\usetheme{{{theme}}}
                \\usecolortheme{{{color_theme}}}
                \\title{{{escape_chars(presentation["title"])}}}
                \\author{{{escape_chars(presentation["author"])}}}
                \\date{{\\today}}

                \\usepackage{{graphicx}}

                \\begin{{document}}

                \\frame{{\\titlepage}}"""

    slides = []

    for slide in presentation["slides"]:
        slide_type = slide["type"]
        content = [escape_chars(text) for text in slide.get("content", [])]
        content_format = slide.get("content_format", "itemize")
        body = render_content(content, content_format)

        # if slide_type == "title":
        #     continue

        if slide_type in {"introduction", "summary"}:
            title = "Introduction" if slide_type == "introduction" else "Summary"
            slides.append(
                f"""\\begin{{frame}}
                \\frametitle{{{title}}}
                {body}
                \\end{{frame}}"""
            )

        elif slide_type == "core_idea":
            core_title = slide.get("title", "Core Idea")
            layout = slide.get("layout", "text_only")

            if layout == "text_only":
                slides.append(
                    f"""\\begin{{frame}}
                    \\frametitle{{{core_title}}}
                    {body}
                    \\end{{frame}}"""
                )

            elif layout == "two_column":
                img = slide["image"]
                slides.append(
                    f"""\\begin{{frame}}
                        \\frametitle{{{core_title}}}
                        \\begin{{columns}}
                        \\begin{{column}}{{0.5\\linewidth}}
                        {body}
                        \\end{{column}}
                        \\begin{{column}}{{0.5\\linewidth}}
                        \\center{{\\includegraphics[height=1.0\\textheight, width=1.0\\textwidth, keepaspectratio]{{{img["path"]}}} \\\\ {escape_chars(img["caption"])}}}
                        \\end{{column}}
                        \\end{{columns}}
                        \\end{{frame}}"""
                )

            elif layout == "single_image":
                img = slide["image"]
                slides.append(
                    f"""\\begin{{frame}}
                        \\frametitle{{{core_title}}}
                        \\center{{\\includegraphics[height=0.5\\textheight, width=0.8\\textwidth, keepaspectratio]{{{img["path"]}}} \\\\ {escape_chars(img["caption"])} \\\\}}
                        {body}
                        \\end{{frame}}"""
                )

    footer = "\\end{document}"

    return "\n\n".join([header] + slides + [footer])