题面
本文记录 Polygon 题面(Statement)的编辑与渲染方式,重点是如何让题面支持中文,以及有关更高程度自定义题面的方法。
新建题面
进入题目页,点击上方的 Statement,选择题面语言后点击 Create,就可以逐栏填写题面了:Name 是题目标题,Legend 是题目描述,Input、Output 分别是输入、输出格式,Notes 用于样例说明等补充内容,此外还有 Scoring、Interaction 等栏目。各栏内容都按 LaTeX 语法书写,点击 In HTML 和 In PDF 可以用 HTML 和 PDF 预览题面。
中文题面选择 Chinese 即可。如果之后还打算把题目导入 Codeforces,建议同时保留一份英文题面(实际上,你可以在英文题面里写中文,在 Codeforces 上是可以渲染的)。
图片
题面里要插图时,把图片传到题目 Statements 页面底部的 Statement Resource Files(不是 Files 页面里的 Resource Files,那里放的是 problem.tex、olymp.sty 这类构建资源)。上传后,在 problem.tex 里直接按文件名引用即可。
渲染题面
题面的渲染有两条路线,但都以 Files 里的 problem.tex 为内容来源:
- 在 Codeforces 上打开题目时,题面由 HTML 渲染,但它只认识一部分命令:manual 里列出的排版命令(完整列表见 Statements TeX manual),加上默认
problem.tex 里用到的结构命令(\InputFile、\OutputFile、\Example、\Interaction、\Scoring、\Note)。这两类之外的命令不会被识别。数学公式交给 MathJax 渲染,公式内部的命令基本不受这份列表限制。
- 构建 PDF 题面时(单题在 Statement 页面的
In PDF,比赛则在比赛页面构建整场题面),题面由 LaTeX 渲染。PDF 使用 Files 里的 olymp.sty 作为样式包、statements.ftl 作为总控模板。
如果只出英文题面,默认的三个文件就够用了;如果要出中文题面,则需要分别修改这三个文件。下面逐一说明。
单题与比赛用的不是同一份文件
单题 PDF 使用题目 Files 页面里的 problem.tex、olymp.sty、statements.ftl;比赛 PDF 使用比赛 Properties/Files 页面里的 statements.ftl 与 olymp.sty,而 problem.tex 仍取每道题自己的。在比赛里上传额外样式文件时,文件名必须以 statements- 开头,并同步修改文件中的 \ProvidesPackage 与 statements.ftl 里的 \usepackage。
olymp.sty
olymp.sty 是 PDF 的样式包,负责页面尺寸与边距、页眉页脚、problem 环境(题头、样例表格)以及各章节标题的文本与字号。它默认只提供英文与俄文(新版还支持乌克兰文)的标题,也没有为通信题准备“第一次运行”“第二次运行”这类分节标题。
为此我写了一个扩展包 olymp-addons:它不修改原本的 olymp.sty,只是在 olymp.sty 之后加载、覆盖其中的标题文本,同时补上通信题需要的分节命令。加载顺序必须是 olymp → olymp-comm → olymp-zh,否则通信题的那几个标题会被覆盖成英文;在比赛中上传时,文件名、\ProvidesPackage 和 \usepackage 三处都要加 statements- 前缀。具体用法见仓库 README。
需要注意的是,olymp.sty 只参与 PDF 的生成。针对它的修改、以及额外上传的样式文件,都不会在渲染 HTML 时生效:HTML 只使用内置样式。因此用于生成 HTML 的题面里不能使用自己新建的 LaTeX 命令,只能用 HTML 渲染器支持的那部分命令。
olymp.sty 还提供了一批开关,在 statements.ftl 的导言区 \def 即可改变 PDF 的显示:
| 开关 |
作用 |
\def\NoInputFileName{} |
不显示题头中的输入文件名 |
\def\NoOutputFileName{} |
不显示题头中的输出文件名 |
\def\NoTimeLimit{} |
不显示时间限制 |
\def\NoMemoryLimit{} |
不显示内存限制 |
\def\NoProblemHead{} |
不显示题头(题目名、各项限制等) |
\def\ShortProblemTitle{} |
题头只显示题目名,不显示“Problem A.”前缀 |
\def\NoExamples{} |
不显示样例 |
\intentionallyblankpagestrue |
双面印刷时在每题之间补白页,保证每题从右页开始 |
problem.tex
problem.tex 是一个 FreeMarker 模板,负责生成单个题目的题面内容,也就是 \begin{problem} 与 \end{problem} 之间的部分。下面是它的默认内容。
默认的 problem.tex
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 | \begin{problem}{${problem.name}}<#--
-->{<#if "stdin" == problem.inputFile><#--
--><#if "russian" == language>стандартный ввод<#--
--><#else>standard input<#--
--></#if><#else>${problem.inputFile}</#if>}<#--
-->{<#if "stdout" == problem.outputFile><#--
--><#if "russian" == language>стандартный вывод<#--
--><#else>standard output<#--
--></#if><#else>${problem.outputFile}</#if>}<#--
--><#assign timeLimit=problem.timeLimit/1000/><#--
--><#if language="russian"><#--
--><#if problem.timeLimit%1000!=0||(10<=timeLimit%100&&timeLimit%100<20)||timeLimit%10=0||5<=timeLimit><#--
-->{${timeLimit?c} секунд}<#--
--><#else><#--
--><#if timeLimit%10=1><#--
-->{${timeLimit?c} секунда}<#--
--><#else><#--
-->{${timeLimit?c} секунды}<#--
--></#if><#--
--></#if><#--
--><#else><#--
-->{${timeLimit?c} second<#if (timeLimit!=1)>s</#if>}<#--
--></#if><#--
--><#assign memoryLimit=problem.memoryLimit/1048576/><#--
--><#if language="russian"><#--
--><#if problem.memoryLimit%1048576==0&&!(10<=memoryLimit%100&&memoryLimit%100<20)&&2<=memoryLimit%10&&memoryLimit%10<5><#--
-->{${memoryLimit?c} мегабайта}
<#else><#--
-->{${memoryLimit?c} мегабайт}
</#if>
<#else><#--
-->{${memoryLimit?c} megabyte<#if (memoryLimit>1)>s</#if>}
</#if>
<#if providedStatementsCommands?? && providedStatementsCommands?size != 0><#--
--><#list providedStatementsCommands as command><#--
-->${command?string}
</#list>
</#if>
${problem.legend}
<#if problem.input?? && (problem.input?length>0)>
\InputFile
${problem.input}
</#if>
<#if problem.output?? && (problem.output?length>0)>
\OutputFile
${problem.output}
</#if>
<#if problem.interaction?? && (problem.interaction?length>0)>
\Interaction
${problem.interaction}
</#if>
<#if problem.scoring?? && (problem.scoring?length>0)>
\Scoring
${problem.scoring}
</#if>
<#if (problem.sampleTests?size>0)>
\Example<#if (problem.sampleTests?size>1)>s</#if>
\begin{example}
<#list problem.sampleTests as test>
\exmpfile{${test.inputFile}}{${test.outputFile}}%
</#list>
\end{example}
</#if>
<#if (problem.notes??) && (problem.notes?length > 0)>
\Note
${problem.notes}
</#if>
\end{problem}
|
可以看出,standard input 与 standard output 的文本就写死在这里,时限与内存的文本、样例与提示的排版也都在这里。如果要出中文题面,可以把上面这段改成:
中文适配的 problem.tex
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 | \begin{problem}{${problem.name}}<#--
-->{<#if "stdin" == problem.inputFile><#--
--><#if "chinese" == language>标准输入<#--
--><#else>standard input<#--
--></#if><#else>${problem.inputFile}</#if>}<#--
-->{<#if "stdout" == problem.outputFile><#--
--><#if "chinese" == language>标准输出<#--
--><#else>standard output<#--
--></#if><#else>${problem.outputFile}</#if>}<#--
--><#assign timeLimit=problem.timeLimit/1000/><#--
-->{${timeLimit?c} s}<#--
--><#assign memoryLimit=problem.memoryLimit/1048576/><#--
-->{${memoryLimit?c} MB}
<#if providedStatementsCommands?? && providedStatementsCommands?size != 0><#--
--><#list providedStatementsCommands as command><#--
-->${command?string}
</#list>
</#if>
${problem.legend}
<#if problem.input?? && (problem.input?length>0)>
\InputFile
${problem.input}
</#if>
<#if problem.output?? && (problem.output?length>0)>
\OutputFile
${problem.output}
</#if>
<#if problem.interaction?? && (problem.interaction?length>0)>
\Interaction
${problem.interaction}
</#if>
<#if problem.scoring?? && (problem.scoring?length>0)>
\Scoring
${problem.scoring}
</#if>
<#if (problem.sampleTests?size>0)>
\Example<#if (problem.sampleTests?size>1)>s</#if>
\begin{example}
<#list problem.sampleTests as test>
\exmpfile{${test.inputFile}}{${test.outputFile}}%
</#list>
\end{example}
</#if>
<#if (problem.notes??) && (problem.notes?length > 0)>
\Note
${problem.notes}
</#if>
\end{problem}
|
它把 standard input 与 standard output 改成“标准输入”“标准输出”,并把时限、内存的单位缩写为 s 与 MB,更符合中文题面的习惯。注意这只改了正文中出现的文本;“时间限制”“输入格式”“样例”这些章节标题由 olymp.sty 提供,要显示中文还需要在 statements.ftl 里加载 olymp-zh。
虽然 Statement 页面要求按 Legend、Input 等逐项编写题面,但他们只是在 problem.tex 中的对应标题后被调用。因此如果修改 problem.tex,可操作空间将相当大,比如接下来要讲的通信题。
通信题
对于通信题,默认的 problem.tex 会给两段文本分别套上“输入格式”“输出格式”的标题,这对通信题并不合适,因此我推荐的改法是删掉 \InputFile 与 \OutputFile 两行:
针对通信题的 problem.tex
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 | \begin{problem}{${problem.name}}<#--
-->{<#if "stdin" == problem.inputFile><#--
--><#if "chinese" == language>标准输入<#--
--><#else>standard input<#--
--></#if><#else>${problem.inputFile}</#if>}<#--
-->{<#if "stdout" == problem.outputFile><#--
--><#if "chinese" == language>标准输出<#--
--><#else>standard output<#--
--></#if><#else>${problem.outputFile}</#if>}<#--
--><#assign timeLimit=problem.timeLimit/1000/><#--
-->{${timeLimit?c} s}<#--
--><#assign memoryLimit=problem.memoryLimit/1048576/><#--
-->{${memoryLimit?c} MB}
<#if providedStatementsCommands?? && providedStatementsCommands?size != 0><#--
--><#list providedStatementsCommands as command><#--
-->${command?string}
</#list>
</#if>
${problem.legend}
<#if problem.input?? && (problem.input?length>0)>
${problem.input}
</#if>
<#if problem.output?? && (problem.output?length>0)>
${problem.output}
</#if>
<#if problem.interaction?? && (problem.interaction?length>0)>
\Interaction
${problem.interaction}
</#if>
<#if problem.scoring?? && (problem.scoring?length>0)>
\Scoring
${problem.scoring}
</#if>
<#if (problem.sampleTests?size>0)>
\Example<#if (problem.sampleTests?size>1)>s</#if>
\begin{example}
<#list problem.sampleTests as test>
\exmpfile{${test.inputFile}}{${test.outputFile}}%
</#list>
\end{example}
</#if>
<#if (problem.notes??) && (problem.notes?length > 0)>
\Note
${problem.notes}
</#if>
\end{problem}
|
这样“输入格式”“输出格式”的标题就没有了,可以直接在 Input 一栏写第一次运行的说明、在 Output 一栏写第二次运行的说明,正文读起来是连贯的。如果希望保留分节标题,也可以用 olymp-addons 提供的 \FirstRun、\SecondRun、\InputProtocol、\OutputProtocol 自行组织。
编写 HTML 题面时需要注意
前面提到,生成 HTML 题面时只会使用 Polygon 内置的样式,额外上传的扩展不参与渲染。因此通信题的 HTML 题面无法使用 olymp-comm 里定义的 \FirstRun、\SecondRun 等命令,只能自己用 \textbf{\Large{第一次运行}} 这类 LaTeX 原生命令把分节标题拼出来。
用两份题面分别负责 PDF 和 HTML
HTML 的命令限制意味着,一份题面很难既把 PDF 排得精致、又在 HTML 里不出错。这时可以准备两份题面:
Chinese 题面:正文按 PDF 的需要写,完整的 LaTeX、olymp-comm 的命令都可以用;构建 PDF 时在 Statement 页面切到它。
English 题面:正文同样可以写中文,但只使用 HTML 支持的命令。Codeforces 展示的是英文题面,所以 HTML 这一侧交给它。
两份题面的正文各自独立,PDF 与 HTML 互不牵制,代价是同一份内容要维护两遍。中文题面只负责出 PDF,它的 HTML 预览不必在意。
需要注意,problem.tex、olymp.sty、statements.ftl 是题目级资源、两种语言共用(尽管负责 HTML 预览的英文题面只需要用到 problem.tex)。
这样中文走 PDF 专用的写法,英文走 HTML 兼容的写法,配合两份题面,两边都能正常显示。如果两边的排版要求其实一致,那一份题面就够了,不必拆成两份。
statements.ftl
statements.ftl 负责整份 PDF 的骨架:导言区、封面、试题列表,以及把每道题的 problem.tex 导入进来。对于一个中文比赛,可以直接使用下面的模板(使用前请先把 olymp-comm.sty 与 olymp-zh.sty 上传到 Resource Files):
推荐的 statements.ftl
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 | \documentclass[12pt,a4paper,oneside]{ctexart}
\usepackage{olymp}
\usepackage{olymp-comm}
\usepackage{olymp-zh}
\usepackage{lastpage}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{color} % for colored text
\usepackage{import} % for changing current dir
\usepackage{epigraph}
\usepackage{wrapfig} % for having text alongside pictures
\usepackage{verbatim}
\usepackage{indentfirst}
\usepackage[table,xcdraw]{xcolor}
\usepackage{booktabs}
\usepackage{longtable,array}
\usepackage{calc}
\usepackage{url}
\usepackage[UTF8,fontset=fandol]{ctex}
\DeclareTextFontCommand{\textbf}{\bfseries\heiti}
\setlength{\parindent}{2em}
\usepackage{etoolbox}
\def\NoInputFileName{}
\def\NoOutputFileName{}
% \def\NoTimeLimit{}
% \def\NoMemoryLimit{}
\newcounter{probcount}
\renewcommand{\theprobcount}{\Alph{probcount}}
\def\probtablecontent{}
\newcommand{\addprobtotable}[2]{\gappto\probtablecontent{#1 & #2 \\}}
\makeatletter
\newcommand{\problemtitle}[1]{%
\stepcounter{probcount}%
\protected@write\@auxout{}{\string\addprobtotable{\theprobcount}{#1}}%
}
\AtEndDocument{
\immediate\write\@auxout{\string\gdef\string\TotalProblemCount{\arabic{probcount}}}
}
\makeatother
\newcommand{\makeproblemtable}{%
% \begin{figure}[b!]
\begin{center}
{\heiti 试题列表}
\vspace{0.5em}
{\small
\begin{tabular}{|p{1cm}<{\centering}|p{6cm}<{\centering}|}
\hline
\probtablecontent
\hline
\end{tabular}}
\vspace{2em}
{\footnotesize 本试题册共 \ifdefined\TotalProblemCount \TotalProblemCount \else ? \fi \ 题,\pageref{LastPage} 页。}
{\footnotesize 如果您的试题册缺少页面,请立即通知志愿者。}
\end{center}
% \end{figure}
}
\makeatletter
\let\origproblem\problem
\let\origendproblem\endproblem
\renewenvironment{problem}[5]{%
\problemtitle{#1}%
\origproblem{#1}{#2}{#3}{#4}{#5}%
}{%
\origendproblem
}
\makeatother
\newif\ifmultistatements
\title{\vspace{4cm} \heiti ${contest.name!} \\ 试题册}
\author{\kaishu 出题人}
\date{${contest.date!}}
\begin {document}
<#list statements as statement>
<#if statement.path??>
\multistatementstrue
</#if>
</#list>
\ifmultistatements
\maketitle
\thispagestyle{empty}
\setcounter{page}{0}
\makeproblemtable
\clearpage
\fi
\contest
{${contest.name!}}%
{${contest.location!}}%
{${contest.date!}}%
\renewcommand{\thefootnote}{\fnsymbol{footnote}}
\renewcommand{\thempfootnote}{\fnsymbol{mpfootnote}}
<#list statements as statement>
<#if statement.path??>
\graphicspath{{${statement.path}}}
<#if statement.index??>
\def\ProblemIndex{${statement.index}}
</#if>
\import{${statement.path}}{./${statement.file}}
<#else>
\input ${statement.file}
</#if>
</#list>
\end {document}
|
下面按块解释这份模板。
题头开关
\def\NoInputFileName{} 与 \def\NoOutputFileName{} 会让题头不显示输入、输出文件名(使用标准输入输出时通常没必要显示),\NoTimeLimit、\NoMemoryLimit 同理,模板里默认注释掉了。更多开关见上一节的表格。
封面与试题列表
\ifmultistatements 里的第一段负责封面与试题列表:
| \maketitle
\thispagestyle{empty}
\setcounter{page}{0}
\makeproblemtable
\clearpage
|
\maketitle 输出封面,内容来自 \title、\author、\date:标题是「比赛名 + 试题册」,开头的 \vspace{4cm} 把它往下压一段;\author 与 \date 分别是出题人和比赛日期,可以按需改成具体的人名或出题组。\thispagestyle{empty} 去掉封面的页眉页脚,\setcounter{page}{0} 把封面记作第 0 页,于是正文从第 1 页开始,\pageref{LastPage} 得到的也就是正文页数(不含封面)。最后的 \clearpage 把封面页送出;随后才调用 \contest{比赛名}{地点}{日期},设置后面各页页眉里的比赛名与日期。
\makeproblemtable 直接在封面页上排一个两列表格:左列是题号,右列是题目名,列宽分别是 1cm 与 6cm,可以按需要调整;表格下方是「本试题册共 X 题、Y 页」和缺页提示。它没有用 figure 浮动环境,所以表格不会漂到别的页面;如果确实想让它沉到页面底部,把代码里注释掉的 \begin{figure}[b!] 与 \end{figure} 恢复即可。
表格的内容由 \problemtitle 收集。olymp.sty 的 problem 环境本身不会调用这个宏,所以模板在导言区末尾把原环境包了一层:
| \let\origproblem\problem
\let\origendproblem\endproblem
\renewenvironment{problem}[5]{%
\problemtitle{#1}%
\origproblem{#1}{#2}{#3}{#4}{#5}%
}{%
\origendproblem
}
|
\let 先保存原来的 \problem 与 \endproblem,\renewenvironment 只是在外面加一层:进入环境时先调用 \problemtitle 登记题号与题目名,再交给原环境排版。这样既不用改动 olymp.sty,也不依赖它是否提供钩子;参数与原环境一致,第 6 个可选参数(feedback)仍然交给原环境处理。
\problemtitle 本身只做两件事:用 \stepcounter{probcount} 递增题号(\theprobcount 把它排成 A、B、C……),再用 \protected@write 把 \addprobtotable{题号}{题目名} 写进 .aux。下一次编译读入 .aux 时,这些调用被依次执行,累积到 \probtablecontent 中,最后由 \makeproblemtable 排版。\AtEndDocument 里的 \immediate\write 则把总题数写成 \TotalProblemCount,供表格下方的「共 X 题」使用。
也正因为内容要经过 .aux 中转,第一次编译时表格是空的、总题数显示 ?、页码是 ??,再编译一次就正常了(详见「题面导入」)。
单题与比赛共用一份模板
\begin{document} 之后的循环遍历所有 statement:构建比赛题面时每道题都带 path,于是打开 \ifmultistatements;构建单题时没有 path,开关保持关闭。封面、\maketitle、试题列表都放在这个开关里,所以同一份 statements.ftl 既能用于整场比赛,也能直接用于单题,不会在单题 PDF 里多出一页封面。
题面导入
末尾的循环负责排版所有题目:\graphicspath 让图片能在题目目录下查找,\def\ProblemIndex{...} 指定这道题在比赛中的题号,再用 \import 导入对应题目的 problem.tex;单题构建时没有 path,直接 \input。其中 \ProblemIndex 需要 olymp.sty 支持才会生效(上游版本没有用它,部分修改版会用它代替自动编号)。
因为试题列表、总题数和页码都依赖 .aux,第一次编译时它们还是空的或显示 ??,需要再编译一次才能正确;Polygon 构建时会自动多跑一遍,本地手动构建时记得跑两遍。
改完这些文件后,建议先在单题上构建一次 PDF,确认没有缺文件、字体和交叉引用的问题,再去构建整场比赛的题面。