Projekt

Allgemein

Profil

Feature #1106 » test.py

Tobias Mai, 30.05.2024 11:10

 
1
import re
2
import os
3

    
4
def replace_latex_formulas(text):
5
    # Define the regex patterns for single $, double $$, and eqnarray environments
6
    single_dollar_pattern = r'\$(.*?)\$'
7
    double_dollar_pattern = r'\$\$(.*?)\$\$'
8
    eqnarray_pattern = r'\\begin\{eqnarray\*\}(.*?)\\end\{eqnarray\*\}'
9

    
10
    # Function to format the matched LaTeX formula for single $
11
    def replacer_single(match):
12
        latex_formula = match.group(1)
13
        # Escape backslashes
14
        latex_formula_escaped = latex_formula.replace('\\', '\\\\')
15
        return f'\\begin{{html}}\\\\({latex_formula_escaped}\\\\)\\end{{html}}'
16

    
17
    # Function to format the matched LaTeX formula for double $$
18
    def replacer_double(match):
19
        latex_formula = match.group(1)
20
        # Escape backslashes
21
        latex_formula_escaped = latex_formula.replace('\\', '\\\\')
22
        return f'\\begin{{html}}\\\\[{latex_formula_escaped}\\\\]\\end{{html}}'
23

    
24
    # Function to format the matched eqnarray environment
25
    def replacer_eqnarray(match):
26
        eqnarray_content = match.group(1)
27
        # Escape backslashes
28
        eqnarray_escaped = eqnarray_content.replace('\\', '\\\\')
29
        return f'\\begin{{html}}\\begin{{eqnarray*}}{eqnarray_escaped}\\end{{eqnarray*}}\\end{{html}}'
30

    
31
    # First replace eqnarray environments
32
    replaced_text = re.sub(eqnarray_pattern, replacer_eqnarray, text, flags=re.DOTALL)
33
    # Then replace double $$ formulas
34
    replaced_text = re.sub(double_dollar_pattern, replacer_double, replaced_text, flags=re.DOTALL)
35
    # Finally replace single $ formulas
36
    replaced_text = re.sub(single_dollar_pattern, replacer_single, replaced_text, flags=re.DOTALL)
37
    
38
    return replaced_text
39

    
40
def process_file(input_filename):
41
    # Read the content from the input file
42
    with open(input_filename, 'r') as file:
43
        text = file.read()
44
    
45
    # Replace LaTeX formulas in the text
46
    modified_text = replace_latex_formulas(text)
47
    
48
    # Create the output filename by adding "2" before the file extension
49
    base, ext = os.path.splitext(input_filename)
50
    output_filename = f"{base}2{ext}"
51
    
52
    # Write the modified text to the output file
53
    with open(output_filename, 'w') as file:
54
        file.write(modified_text)
55

    
56
    print(f"Processed file saved as: {output_filename}")
57

    
58
# Example usage
59
input_filename = 'example.tex'  # Replace with your input file name
60
process_file(input_filename)
61

    
62

    
63
# import re
64
# import os
65
# 
66
# def replace_latex_formulas(text):
67
#     # Define the regex patterns for single $ and double $$ LaTeX formulas
68
#     single_dollar_pattern = r'\$(.*?)\$'
69
#     double_dollar_pattern = r'\$\$(.*?)\$\$'
70
#     
71
#     # Function to format the matched LaTeX formula for single $
72
#     def replacer_single(match):
73
#         latex_formula = match.group(1)
74
#         # Escape backslashes
75
#         latex_formula_escaped = latex_formula.replace('\\', '\\\\')
76
#         return f'\\begin{{html}}\\\\({latex_formula_escaped}\\\\)\\end{{html}}'
77
# 
78
#     # Function to format the matched LaTeX formula for double $$
79
#     def replacer_double(match):
80
#         latex_formula = match.group(1)
81
#         # Escape backslashes
82
#         latex_formula_escaped = latex_formula.replace('\\', '\\\\')
83
#         return f'\\begin{{html}}\\\\[{latex_formula_escaped}\\\\]\\end{{html}}'
84
# 
85
#     # First replace double $$ formulas
86
#     replaced_text = re.sub(double_dollar_pattern, replacer_double, text)
87
#     # Then replace single $ formulas
88
#     replaced_text = re.sub(single_dollar_pattern, replacer_single, replaced_text)
89
#     
90
#     return replaced_text
91
# 
92
# def process_file(input_filename):
93
#     # Read the content from the input file
94
#     with open(input_filename, 'r') as file:
95
#         text = file.read()
96
#     
97
#     # Replace LaTeX formulas in the text
98
#     modified_text = replace_latex_formulas(text)
99
#     
100
#     # Create the output filename by adding "2" before the file extension
101
#     base, ext = os.path.splitext(input_filename)
102
#     output_filename = f"{base}2{ext}"
103
#     
104
#     # Write the modified text to the output file
105
#     with open(output_filename, 'w') as file:
106
#         file.write(modified_text)
107
# 
108
#     print(f"Processed file saved as: {output_filename}")
109
# 
110
# # Example usage
111
# input_filename = 'example.tex'  # Replace with your input file name
112
# process_file(input_filename)
    (1-1/1)