-
Notifications
You must be signed in to change notification settings - Fork 0
/
MakeTex
executable file
·112 lines (96 loc) · 4.01 KB
/
MakeTex
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#! /usr/bin/python
#Script to produce a latex template.
#Default: Article, My name, date.
#Need to set up flags to modify all that, plus add in database stuff and packages.
import sys
import argparse
#Command line argument
parser = argparse.ArgumentParser(description='Create a LaTeX template file without having to go to all the bother of copying files.')
parser.add_argument("-t", "--title", help="Change the title of the tex document.", default="Place holder title,\\\\ please change this")
parser.add_argument("-a", "--author", help="Change the name of the author.", default="Anthony James Dempsey")
parser.add_argument("-dt", "--doc", help="Change the document type.", default="article")
parser.add_argument("-f", "--filename", help="File name to save the template to.", default="test.tex")
parser.add_argument("-d", "--dir", help="Directory to put the file in.", default="./")
parser.add_argument("-p", "--packages", help="Packages to include in the template.", default=[], nargs="*")
parser.add_argument("-v", "--verbosity", help="Verbosity of the program.", default=0, action="count",)
parser.add_argument("-bs", "--bibstyle", help="Include a bibliography with this style.")
parser.add_argument("-bn", "--bibname", help="Include a bibliography with this name.")
args = parser.parse_args()
##Set up the variables, storing the defaults or the changes made by the cli flags
doc_type = args.doc
author = args.author
title = args.title
file_name = args.filename
packages_list = args.packages
directory = args.dir
bib_string = ""
if args.bibstyle == None and args.bibname != None:
if args.verbosity >= 1:
print "Have bibliography name, do not have bibliography style using default julyabuns."
bib_string = "\\bibliographystyle{julyabuns}\n\\bibliography{"+args.bibname+"}{}"
elif args.bibstyle != None and args.bibname == None:
if args.verbosity >= 1:
print "Have bibliography style, do not have bibliography name using default database."
bib_string = "\\bibliographystyle{"+args.bibstyle+"}\n\\bibliography{database}{}"
elif args.bibstyle == None and args.bibname == None:
if args.verbosity >= 1:
print "Have neither bibliography name or style, bibliography not being used in template."
bib_string = ""
else:
if args.verbosity >= 1:
print "Have been provide with both bibliography name or style."
bib_string = "\\bibliographystyle{"+args.bibstyle+"}\n\\bibliography{"+args.bibname+"}{}"
if directory[-1] != "/":
directory += "/"
path = directory+file_name
if args.verbosity >= 2:
print "Creating file "+file_name+" in directory "+directory
packages = "\n"
if args.verbosity >= 2:
print "Using packages:"
for i in packages_list:
if args.verbosity >= 2:
print "\t"+i
packages += "\usepackage{"+i+"}\n"
##Create the string for the template
string = '''\\documentclass{{{0}}}
\\author{{{1}}}
\\title{{{2}}}
\\date{{\\today}}
{3}
\\begin{{document}}
\\maketitle
\\end{{document}}
{4}
'''.format(doc_type, author, title, packages, bib_string);
if args.verbosity >= 1:
print "Template successfully created"
if args.verbosity >= 2:
print "Template string:\n"+string
##Open file, write to file, close file. Yay error catching, because you never know.
try:
f = open(path, 'w')
except IOError as (errno, strerror):
print "I/O error({0}): {1}".format(errno, strerror)
sys.exit(errno)
except:
print "Unexpected error:", sys.exc_info()[0]
sys.exit(1)
try:
f.write(string)
except IOError as (errno, strerror):
print "I/O error({0}): {1}".format(errno, strerror)
sys.exit(errno)
except:
print "Unexpected error:", sys.exc_info()[0]
sys.exit(1)
try:
f.close()
except IOError as (errno, strerror):
print "I/O error({0}): {1}".format(errno, strerror)
sys.exit(errno)
except:
print "Unexpected error:", sys.exc_info()[0]
sys.exit(1)
if args.verbosity >= 1:
print "Template has been created successfully"