-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_template.py
320 lines (260 loc) · 9.08 KB
/
create_template.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import click
import os
################################################################################
#### TeX Class File
hmcpset = '''
% HMC Math dept HW class file
% v0.04 by Eric J. Malm, 10 Mar 2005
%%% IDENTIFICATION --------------------------------------------------------
\\NeedsTeXFormat{LaTeX2e}[1995/01/01]
\\ProvidesClass{hmcpset}
[2005/03/10 v0.04 HMC Math Dept problem set class]
%%% INITIAL CODE ----------------------------------------------------------
% test whether the document is being compiled with PDFTeX
\\RequirePackage{ifpdf}
%%% DECLARATION OF OPTIONS ------------------------------------------------
%% Header Options: header*, no header
\\newif\\ifhmcpset@header
% no header block in upper right hand corner
\\DeclareOption{noheader}{%
\\hmcpset@headerfalse%
}
% do print header block
\\DeclareOption{header}{%
\\hmcpset@headertrue%
}
%% Font Options: palatino*, cm
\\newif\\ifhmcpset@palatino
% use palatino fonts
\\DeclareOption{palatino}{%
\\hmcpset@palatinotrue%
}
% use compuer modern fonts
\\DeclareOption{cm}{%
\\hmcpset@palatinofalse%
}
%% Problem Boxing: boxed*, unboxed
\\newif\\ifhmcpset@boxed
% box problem statements
\\DeclareOption{boxed}{%
\\hmcpset@boxedtrue%
}
% don't box problem statements
\\DeclareOption{unboxed}{%
\\hmcpset@boxedfalse%
}
% pass remaining options to article class
\\DeclareOption*{\\PassOptionsToClass{\\CurrentOption}{article}}
%%% EXECUTION OF OPTIONS --------------------------------------------------
%% default to:
% including header,
% loading mathpazo package for palatino fonts,
% boxing problem statements
\\ExecuteOptions{header,palatino,boxed}
\\ProcessOptions
%%% PACKAGE LOADING -------------------------------------------------------
%% based on std article class
\\LoadClass{article}
%% Font loading: Palatino text/math fonts
\\ifhmcpset@palatino
\\RequirePackage{mathpazo}
\\fi
%% AMSLaTeX math environments and symbols
\\RequirePackage{amsmath}
\\RequirePackage{amssymb}
%% boxed minipage for boxed problem environment
\\RequirePackage{boxedminipage}
%%% MAIN CODE -------------------------------------------------------------
%% Tell dvips/pdflatex correct page size
\\ifpdf
\\AtBeginDocument{%
\\setlength{\\pdfpageheight}{\\paperheight}%
\\setlength{\\pdfpagewidth}{\\paperwidth}%
}
\\else
\\AtBeginDvi{\\special{papersize=\\the\\paperwidth,\\the\\paperheight}}%
\\fi
%% Problem set environments
% boxed problem environment
\\newenvironment{problem}[1][]{%
\\ifhmcpset@boxed\\def\\hmcpset@probenv{boxed}\\else\\def\\hmcpset@probenv{}\\fi%
\\bigskip% put space before problem statement box %
\\noindent\\begin{\\hmcpset@probenv minipage}{\\columnwidth}%
\\def\\@tempa{#1}%
\\ifx\\@tempa\\empty\\else%
\\hmcpset@probformat{#1}\\hspace{0.5em}%
\\fi%
}{%
\\end{\\hmcpset@probenv minipage}%
}
% display optional argument to problem in bold
\\let\\hmcpset@probformat\\textbf
% solution environment with endmark and optional argument
\\newenvironment{solution}[1][]{%
\\begin{trivlist}%
\\def\\@tempa{#1}%
\\ifx\\@tempa\\empty%
\\item[]%
\\else%
\\item[\\hskip\\labelsep\\relax #1]%
\\fi%
}{%
\\mbox{}\\penalty10000\\hfill\\hmcpset@endmark%
\\end{trivlist}%
}
% default endmark is small black square
\\def\\hmcpset@endmark{\\ensuremath{\\scriptscriptstyle\\blacksquare}}
%% Problem set list, for top of document
\\newcommand{\\problemlist}[1]{\\begin{center}\\large\\sffamily{#1}\\end{center}}
%% commands for upper-right id header block
\\newcommand{\headerblock}{%
\\begin{flushright}
\\mbox{\hmcpset@name}\\protect\\
\\mbox{\hmcpset@class}\\protect\\
\\mbox{\hmcpset@assignment}\\protect\\
\\hmcpset@duedate%
\\ifx\\hmcpset@extraline\\empty\\else\\protect\\\\hmcpset@extraline\\fi%
\\end{flushright}%
}
% put id header block at start of document
\\ifhmcpset@header\\AtBeginDocument{\\headerblock}\\fi
% internal state for headerblock
\\def\\hmcpset@name{}
\\def\\hmcpset@class{}
\\def\\hmcpset@assignment{}
\\def\\hmcpset@duedate{}
\\def\\hmcpset@extraline{}
% commands to set header block info
\\newcommand{\\name}[1]{\\def\\hmcpset@name{#1}}
\\newcommand{\\class}[1]{\\def\\hmcpset@class{#1}}
\\newcommand{\\assignment}[1]{\\def\\hmcpset@assignment{#1}}
\\newcommand{\\duedate}[1]{\\def\\hmcpset@duedate{#1}}
\\newcommand{\\extraline}[1]{\\def\\hmcpset@extraline{#1}}
'''
################################################################################
#### TeX Template Fragments
begin = '''
\\documentclass[11pt,letterpaper,boxed]{{hmcpset}}
\\usepackage[margin=1in,headheight=14pt]{{geometry}} % set page layout
\\usepackage{{amssymb}} % AMS symbols
\\usepackage{{enumerate}} % enumerate environment
\\usepackage{{fancyhdr}} % fancy header
\\usepackage{{gensymb}} % generic symbols for text and math mode
\\usepackage{{lastpage}} % get the page count
\\usepackage{{mathtools}} % useful symbols and tools
\\usepackage{{parskip}} % fix paragraph indentation
\\pagestyle{{fancy}}
\\lhead{{{0}}}
\\chead{{{1}}}
\\rhead{{{2}}}
\\lfoot{{}}
\\cfoot{{}}
\\rfoot{{Page\\ \\thepage\\ of\\ \\pageref{{LastPage}}}}
\\linespread{{1.1}}
\\newcommand\\blankpage{{
\\thispagestyle{{empty}}
\\addtocounter{{page}}{{-1}}
\\newpage}}
\\renewcommand\\footrulewidth{{0.4pt}}
\\begin{{document}}
\\problemlist{{{3}}}
'''
problem = '''
%------------------------- Problem {0} -----------------------
\\begin{{problem}}[{0}]
\\hfill
\\end{{problem}}
\\begin{{solution}}
\\vfill
\\end{{solution}}
\\newpage
'''
end = '''
\\end{document}
'''
################################################################################
#### Helper Functions
def determine_title(course, assignment):
'''Determines the title of the problem set.'''
title = course
if assignment and assignment is not 'hw':
if course:
title += ': '
if assignment.isdigit():
title += 'HW '
title += assignment
return title
def query_inputs(advanced):
'''Gets information about problem set.'''
click.echo('Please enter the following information:')
name = click.prompt('Name')
course = click.prompt('Course')
assignment = click.prompt('Assignment Name/Number', default='hw',
show_default=False)
due_date = click.prompt('Due Date')
# Limit maximum number of problems to 50.
num_problems = click.prompt('Number of Problems', default=0,
show_default=False,
type=click.IntRange(0, 50, clamp=True))
title = determine_title(course, assignment)
problem_list = click.prompt('Problem List', default=title,
show_default=False) if advanced else title
return name, course, assignment, due_date, num_problems, title, \
problem_list
def determine_homework_file_name(assignment):
'''Determines the name of the file.'''
# Strip '/' characters since they can't be used in file names.
assignment = assignment.replace('/', '')
counter = 0
file_name = ''
if assignment.isdigit():
file_name += 'hw'
file_name += '{0}.tex'.format(assignment)
while os.path.exists(file_name):
counter += 1
file_name = ''
if assignment.isdigit():
file_name += 'hw'
file_name += '{0} ({1}).tex'.format(assignment, counter)
return file_name
def create_homework_file(file_name, name, title, due_date, problem_list,
num_problems):
'''Creates the file in the current directory.'''
with click.open_file(file_name, 'w') as template_file:
template_file.write(begin.format(
name, title, due_date, problem_list).lstrip())
for i in range(1, num_problems+1):
template_file.write(problem.format(i))
template_file.write(end)
click.echo('The file "' + file_name +
'" has been created in the current directory.')
def verify_hmcpset():
'''
Determines whether hmcpset is in the current directory and asks to add it
if it is not found.
'''
if not os.path.exists('hmcpset.cls'):
click.echo('Your current directory does not have the required '
'"hmcpset.cls".')
if click.confirm('Create "hmcpset.cls"?', default=True):
with click.open_file('hmcpset.cls', 'w') as pset_file:
pset_file.write(hmcpset.lstrip())
click.echo('The file "hmcpset.cls" has been created in the '
'current directory.')
################################################################################
#### Main Function
@click.command()
@click.option('-a', '--advanced', default=False, is_flag=True,
help='Advanced options.')
def main(advanced):
'''Create a ready-to-compile tex file using the hmcpset class.'''
if advanced:
click.echo('Entering advanced mode.')
name, course, assignment, due_date, num_problems, title, problem_list \
= query_inputs(advanced)
file_name = determine_homework_file_name(assignment)
create_homework_file(file_name, name, title, due_date,
problem_list, num_problems)
verify_hmcpset()
if __name__ == '__main__':
main()