-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlatex.py
More file actions
executable file
·57 lines (48 loc) · 1.87 KB
/
latex.py
File metadata and controls
executable file
·57 lines (48 loc) · 1.87 KB
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
#!/usr/bin/env python
import logging
import os
import platform
import subprocess
logger = logging.getLogger(__name__)
LATEX_TEMPLATE = '''
\\documentclass{{article}}\n
\\usepackage{{qtree}}\n
\\usepackage[active,tightpage]{{preview}}\n
\\usepackage{{varwidth}}\n
\\AtBeginDocument{{\\begin{{preview}}\\begin{{varwidth}}{{\\linewidth}}}}\n
\\AtEndDocument{{\\end{{varwidth}}\\end{{preview}}}}\n
\\begin{{document}}\n
{tree}\n
\\end{{document}}
'''
OS_TO_OPENER = dict(
Darwin=lambda filename: subprocess.call(('open', filename)),
Linux=lambda filename: subprocess.call(('xdg-open', filename))
)
def qtree_to_pdf(qtree, filename='out', tex_template=LATEX_TEMPLATE):
"""
Insert a qtree string into a latex template, and use pdflatex to generate
a pdf. It will try to open the generated PDF, if possible on the OS.
:param qtree: a valid qtree string. For more details refer to
http://www.ling.upenn.edu/advice/latex/qtree/qtreenotes.pdf
:param filename: a prefix for the names of the generated files.
:param tex_template: a latex template string. Expects the token '{tree}' at
the location the qtree string is to be inserted.
"""
tex_fp = os.path.abspath('{}.tex'.format(filename))
with open(tex_fp, 'w') as output:
output.write(tex_template.format(tree=qtree))
# To create a TEX file, we need to be in the parent directory of the file.
subprocess.call(('pdflatex', tex_fp),
cwd=os.path.dirname(tex_fp),
stdout=subprocess.DEVNULL)
# Open the created file.
try:
pdf_fp = os.path.abspath('{}.pdf'.format(filename))
retcode = OS_TO_OPENER[platform.system()](pdf_fp)
if retcode < 0:
raise OSError(-retcode)
except KeyError as err:
logger.warn('Cannot open PDF on this OS', err)
except OSError as err:
logger.warn('Failed to open PDF', err)