-
Notifications
You must be signed in to change notification settings - Fork 4
/
create_latex_pdf.py
executable file
·91 lines (64 loc) · 2.03 KB
/
create_latex_pdf.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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Modified by Yu Wang([email protected])
# Modify code from Claus Haslauer (https://github.com/clausTue/OO_latex)
# Reasons of Modify: MacTex have different texbin; pdfcrop is a perl script could not get right result in shell command, remove crop in python script and add Terminal execution in applescript
# Time: 2016.04.17
"""
"""
import sys
import os
import datetime
import time
def main():
"""
this script uses `pdflatex` and `pdfcrop` to produce a pdf of a latex string that is passed to the script as command line argument
requires valid latex installation
- there is no test for this string being a valid latex expression
"""
cur_path = os.getcwd()
# # check if previous files exist; if yes: delete them
filenames = ["temp.ps"
, "temp.aux"
, "temp.dvi"
, "temp.log"
, "temp.tex"
, "temp.pdf"
, "temp-crop.pdf"]
for filename in filenames:
try:
os.remove(filename)
except:
print "file did not exist"
pass
# # other option
#if os.path.isfile(os.path.join(cur_path, 'temp.ps')):
# # this is the latex string that is being passed
str_latex_note = sys.argv[1]
print "%s" % str_latex_note
# # this is a minimal latex template into which the string is inserted
preamble = """\documentclass{article}
\usepackage{amsmath,amssymb}
\pagestyle{empty}
\\begin{document}
{\huge
\[
%s
\]
}
\end{document}"""% (str_latex_note)
## write latex file
cur_file = "temp.tex"
fobj = open(os.path.join(cur_path, cur_file), 'w')
fobj.writelines(preamble)
fobj.close()
## process latex file
# print cur_path
os.chdir(cur_path)
# Here change the path
cmd = '/Library/Tex/texbin/pdflatex temp.tex'
# print cmd
# print "running latex"
os.system(cmd)
if __name__ == '__main__':
main()