This repository was archived by the owner on Jul 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix_latex.py
50 lines (38 loc) · 1.45 KB
/
fix_latex.py
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
"""
Parse a LaTeX file, make figure paths relative, and remove other PythonTeX stuff
"""
import os
import parse
import click
@click.group()
def cli():
click.echo('Fixing LaTeX document...')
@cli.command()
@click.option('--input-file', '-i', multiple=True, help='TeX file to parse')
@click.option('--output-file', '-o', multiple=True, help='Path to fixed TeX file')
def fix_figure_paths(input_file, output_file):
with open(input_file[0], 'r') as f:
lines = f.readlines()
for i, l in enumerate(lines):
if 'includegraphics' in l:
r = parse.parse('\includegraphics[{}]{{{}}}', l.strip())
_, fig_filename = os.path.split(r[1])
new_line = f' \includegraphics[{r[0]}]{{{fig_filename}}}\n'
lines[i] = new_line
with open(output_file[0], 'w') as f:
f.write(''.join(lines))
@cli.command()
@click.option('--input-file', '-i', multiple=True, help='TeX file to parse')
@click.option('--output-file', '-o', multiple=True, help='Path to fixed TeX file')
def remove_pythontex_block(input_file, output_file):
with open(input_file[0], 'r') as f:
lines = f.readlines()
for i, l in enumerate(lines):
if 'begin pythontex bug fix' in l.lower():
i_start = i
if 'end pythontex bug fix' in l.lower():
i_end = i
with open(output_file[0], 'w') as f:
f.write(''.join(lines[:i_start] + lines[i_end+1:]))
if __name__ == '__main__':
cli()