-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblemset.cls
342 lines (293 loc) · 9.58 KB
/
problemset.cls
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{problemset}[2012/01/19 Problem Set For ACM-Style Programming Contests]
\newif\ifplastex
\plastexfalse
\newif\if@footer\@footertrue
\DeclareOption{nofooter}{\@footerfalse}
\newif\if@problemnumbers\@problemnumberstrue
\DeclareOption{noproblemnumbers}{\@problemnumbersfalse}
\newif\if@problemids\@problemidstrue
\DeclareOption{noproblemids}{\@problemidsfalse}
\newif\if@samplenumbers\@samplenumberstrue
\DeclareOption{nosamplenumbers}{\@samplenumbersfalse}
\newif\if@clearevenpages\@clearevenpagestrue
\newif\if@autoincludesamples\@autoincludesamplestrue
\DeclareOption{noautoincludesamples}{\@autoincludesamplesfalse}
\DeclareOption{plainproblems}{
\@footerfalse
\@problemnumbersfalse
\@clearevenpagesfalse
}
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}
\ProcessOptions\relax
\LoadClass{article}
\RequirePackage{times} % Font choice
\RequirePackage{amsmath} % AMS
\RequirePackage{amssymb} % AMS
\RequirePackage[OT2,T1]{fontenc} % Cyrillic and standard % TODO: make alphabet options more general
\RequirePackage[utf8]{inputenc} % UTF-8 support
\RequirePackage{fancyhdr} % Headers
\RequirePackage{graphicx} % Graphics
\RequirePackage{subfigure} % Subfigures
\RequirePackage{wrapfig} % Illustrations
\RequirePackage{import} % Proper file inclusion
\RequirePackage{fancyvrb} %
\RequirePackage{listingsutf8} % For samples
\RequirePackage[left=1in,right=1in,top=0.75in,bottom=0.75in]{geometry}
%\RequirePackage{fullpage} % Set up margins for full page
\RequirePackage{url} % Urls
\RequirePackage[normalem]{ulem} % \sout
\RequirePackage[colorlinks=true,implicit=false]{hyperref}
\ifplastex\else
\RequirePackage{pgffor}
\fi
%% Commands used to set name, logo, etc of contest
\newcommand*{\contestname}[1]{\def\@contestname{#1}}
\newcommand*{\contestshortname}[1]{\def\@contestshortname{#1}}
\newcommand*{\contestlogo}[1]{\def\@contestlogo{#1}}
\newcommand*{\location}[1]{\def\@location{#1}}
\newcommand*{\licenseblurb}[1]{\def\@licenseblurb{#1}}
\newcommand*{\problemlanguage}[1]{\def\@problemlanguage{#1}}
\contestname{}
\contestshortname{}
\contestlogo{}
\location{}
\licenseblurb{}
\problemlanguage{}
% Command to set a header logo
\newsavebox{\PS@headerbox}
\savebox{\PS@headerbox}{}
\addtolength{\headheight}{0.25in}
\addtolength{\textheight}{-0.25in}
\setlength{\headsep}{12pt}
\newcommand*{\headerlogo}[1]{
\def\@headerlogo{#1}
\savebox{\PS@headerbox}{\includegraphics[width=\textwidth]{\@headerlogo}}
\addtolength{\textheight}{\headheight}
\settoheight{\headheight}{\usebox{\PS@headerbox}}
\addtolength{\headheight}{4.2pt}
\addtolength{\textheight}{-\headheight}
}
% Typesetting sections in a problem
\renewcommand\section{\@startsection{section}{1}{\z@}%
{-3ex}%
{1ex}%
{\normalfont\large\sf\bfseries}}
\renewcommand\subsection{\@startsection{subsection}{2}{\z@}%
{-3.55ex}%
{1.7ex}%
{\normalfont\normalsize\sf\bfseries}}
\renewcommand{\contentsname}{Problems}
% TODO: make last command of illustration optional
\newcommand{\illustration}[3]{
\begin{wrapfigure}{r}{#1\textwidth}
\includegraphics[width=#1\textwidth]{#2}
\begin{flushright}
\vspace{-9pt}
\tiny #3
\end{flushright}
\vspace{-15pt}
\end{wrapfigure}
\par
\noindent
}
%% Redefine cleardoublepage to put a text on even-numbered empty
%% pages.
\newcommand{\makeemptypage}{
~\thispagestyle{empty}
\vfill
\centerline{\Large \textsf{ This page is intentionally left blank.}}
\vfill
\clearpage
}
\renewcommand{\cleardoublepage}{
\clearpage%
\ifodd\value{page}\else\makeemptypage\fi%
}
\newcommand{\clearproblemsetpage}{
\if@clearevenpages
\cleardoublepage
\else
\clearpage
\fi
}
%% Set up a problem counter and number problems A B C ...
\newcounter{problemcount}
\setcounter{problemcount}{0}
\newcommand{\problemnumber}{\Alph{problemcount}}
%% Number figures as A.1 A.2... B.1 B.2...
%% (except if we're converting to HTML or if we're not using problem numbers)
\ifplastex\else
\if@problemnumbers
\renewcommand{\thefigure}{\problemnumber.\arabic{figure}}
\fi
\fi
%% Command for starting new problem
%% Problem inclusion
\newcommand{\includeproblem}[1]{
\startproblem{#1}
\import{#1/problem_statement/}{problem\@problemlanguage.tex}
%% Automatically include samples 1..9, if enabled
\ifplastex\else
\if@autoincludesamples
\foreach \SampleNum in {1,...,9} {
\IfFileExists{\@problemid/data/sample/\SampleNum.in}{
\displaysample{\@problemid/data/sample/\SampleNum}
}
}
\fi
\fi
}
\newcommand{\startproblem}[1]{
\clearproblemsetpage
\refstepcounter{problemcount}
\setcounter{samplenum}{0}
\setcounter{figure}{0}%
\def\@problemid{#1}
}
\newcommand{\problemname}[1]{
\def\@problemname{#1}
\problemheader{\@problemname}{\@problemid}
}
\newcommand{\ps@formattime}[1]{
#1\ifnum#1=1 second \else seconds \fi
}
\newcommand{\problemheader}[2]{
\begin{center}
\textsf{
\if@problemnumbers {\huge Problem \problemnumber\\[3mm]} \fi
{\LARGE #1}
\if@problemids {\\[2mm]{\Large Problem ID: #2}} \fi
\IfFileExists{#2/.timelimit}{
\newread\ps@timelimitfile
\openin\ps@timelimitfile=#2/.timelimit
\read\ps@timelimitfile to\ps@timelimit
\\[2mm]{\Large Time limit:\ps@formattime{\ps@timelimit}}
}{}
\\[5mm]
}
\end{center}
\addtocontents{toc}{
\if@problemnumbers \problemnumber \fi
& \@problemname \\}%
}
%% Commands related to sample data
\newcommand{\sampleinputname}{Sample Input}
\newcommand{\sampleoutputname}{Sample Output}
%% Sample counter
\newcounter{samplenum}
\newcommand{\sampleid}{\arabic{samplenum}}
%% Define the command used to give sample data
%% Takes filename as parameter
\newcommand{\includesample}[1]{
\displaysample{\@problemid/data/sample/#1}
}
\newcommand{\displaysample}[1]{
\IfFileExists{#1.in}{}{\ClassError{problemset}{Can't find file '#1.in'}{}}
\IfFileExists{#1.ans}{}{\ClassError{problemset}{Can't find file '#1.ans'}{}}
\refstepcounter{samplenum}
\vspace{0.4cm}
\sampletable
{\sampleinputname{} \if@samplenumbers\sampleid\fi}{#1.in}
{\sampleoutputname{} \if@samplenumbers\sampleid\fi}{#1.ans}
}
\newlength{\PS@sampleidealwidth}
\setlength{\PS@sampleidealwidth}{0.473\textwidth}
\newsavebox{\PS@sampleinbox}
\newsavebox{\PS@sampleoutbox}
\newlength{\PS@sampleinwidth}
\newlength{\PS@sampleoutwidth}
\newlength{\PS@sampletotwidth}
\newcommand{\sampletable}[4]{
% First find widths of the two files
\savebox{\PS@sampleinbox}{\lstinputlisting[inputencoding=utf8/latin1,basicstyle=\ttfamily]{#2}}
\savebox{\PS@sampleoutbox}{\lstinputlisting[inputencoding=utf8/latin1,basicstyle=\ttfamily]{#4}}
\settowidth{\PS@sampleoutwidth}{\usebox{\PS@sampleoutbox}}
\settowidth{\PS@sampleinwidth}{\usebox{\PS@sampleinbox}}
\setlength{\PS@sampletotwidth}{\PS@sampleinwidth}
\addtolength{\PS@sampletotwidth}{\PS@sampleoutwidth}
% Check if too wide for side-by-side
\ifdim\PS@sampletotwidth>2\PS@sampleidealwidth
\par
\noindent
\begin{tabular}{|l|}
\multicolumn{1}{l}{\textsf{\textbf{#1}}}\\
\hline
\parbox[t]{0.968\textwidth}{\vspace{-0.3cm}\usebox{\PS@sampleinbox}}\\
\hline
\end{tabular}
\par
\vspace{0.25cm}
\noindent
\begin{tabular}{|l|}
\multicolumn{1}{l}{\textsf{\textbf{#3}}}\\
\hline
\parbox[t]{0.968\textwidth}{\vspace{-0.3cm}\usebox{\PS@sampleoutbox}}\\
\hline
\end{tabular}
\else
% Side by side possible, figure out if adjustments are needed.
\ifdim\PS@sampleoutwidth>\PS@sampleidealwidth% Sample out too large
\setlength{\PS@sampleinwidth}{2\PS@sampleidealwidth}
\addtolength{\PS@sampleinwidth}{-\PS@sampleoutwidth}
\else
\ifdim\PS@sampleinwidth>\PS@sampleidealwidth% Sample in too large
\setlength{\PS@sampleoutwidth}{2\PS@sampleidealwidth}
\addtolength{\PS@sampleoutwidth}{-\PS@sampleinwidth}
\else% Ideal case: neither sample in nor sammple out too large
\setlength{\PS@sampleinwidth}{\PS@sampleidealwidth}
\setlength{\PS@sampleoutwidth}{\PS@sampleidealwidth}
\fi
\fi
\par
\noindent
\begin{tabular}{|l|l|}
\multicolumn{1}{l}{\textsf{\textbf{#1}}} &
\multicolumn{1}{l}{\textsf{\textbf{#3}}} \\
\hline
\parbox[t]{\PS@sampleinwidth}{\vspace{-0.3cm}\usebox{\PS@sampleinbox}}
&
\parbox[t]{\PS@sampleoutwidth}{\vspace{-0.3cm}\usebox{\PS@sampleoutbox}}
\\
\hline
\end{tabular}
\fi
\par
}
% Remaining part of file is headers and toc, not tested with plasTeX
% and should not be used in plastex mode
\ifplastex\else
\AtBeginDocument{
%% Set up headers
\fancypagestyle{problem}{
\fancyhf{} % Clear old junk
\fancyhead[C]{\usebox{\PS@headerbox}}
\if@footer
\fancyfoot[L]{
\emph{
\@contestshortname{}
\ifdefined\@problemname
\if@problemnumbers Problem \problemnumber:{} \fi
\@problemname
\fi
\ifx\@licenseblurb\@empty\relax\else
\\\@licenseblurb
\fi
}
}
\fancyfoot[R]{\thepage}
\fi
}
\renewcommand{\headrulewidth}{0pt}
\pagestyle{problem}
% Set up table of contents for cover page
\addtocontents{toc}{\protect\begin{tabular}{cl}}
}
\AtEndDocument{
\clearproblemsetpage
% Annoyingly enough addtocontents won't work at end of doc
\immediate\write\@auxout{%
\string\@writefile{toc}{\string\end{tabular}}%
}
}
\fi