File size: 807 Bytes
f91510c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import re

def split_sections(path):
    
    with open(path, 'r') as f:
      tex_string = f.read()
    
    # find all section titles and their starting index
    sections = re.findall(r'\\section{(.+?)}', tex_string, flags=re.DOTALL)
    section_indices = [match.start() for match in re.finditer(r'\\section{', tex_string)]

    # add index for end of last section
    section_indices.append(len(tex_string))

    # split string into sections and store in dictionary
    section_dict = {}
    for i in range(len(sections)):
        start = section_indices[i]
        end = section_indices[i+1]
        section_text = tex_string[start:end]
        section_title = sections[i].strip()
        section_dict[section_title] = section_text

    return section_dict


sections = split_sections('./control.tex')