考虑这个最小的例子:
\begin{filecontents}{test.cls}
\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\RequirePackage{expl3,l3keys2e}
\ProvidesExplClass
{test}
{2013/12/23}
{1.0}
{A test class}
\bool_new:N \g_test_key_bool
\bool_new:N \g_test_key_a_bool
\bool_new:N \g_test_key_b_bool
\keys_define:nn { test } {
test .bool_set:N = \g_test_key_bool,
key .multichoice:,
key / a .code:n = \bool_set_true:N \g_test_key_a_bool,
key / b .code:n = \bool_set_true:N \g_test_key_b_bool,
}
\ProcessKeysOptions{ test }
\LoadClass{article}
\end{filecontents}
\documentclass[test,key={a,b}]{test}
\usepackage{fontspec}
\begin{document}
ducks
\end{document}
当fontspec
加载时(或者可能是任何使用类级选项的包\ProcessOptions
),我收到以下错误:
………
(/usr/local/texlive/2013/texmf-dist/tex/latex/fontspec/fontspec.sty
(/usr/local/texlive/2013/texmf-dist/tex/latex/l3packages/xparse/xparse.sty)
! LaTeX Error: Missing \begin{document}.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.271 \ProcessOptions*
! You can't use `macro parameter character #' in horizontal mode.
\@removeelement #1#2#3->\def \reserved@a ##1,#1,##
2\reserved@a {##1,##2\rese...
l.271 \ProcessOptions*
)
Runaway argument?
{##1,##2\reserved@b }\def \reserved@b ##1,\reserved@b ##2\reserved@b \ETC.
! File ended while scanning use of \reserved@a.
<inserted text>
\par
l.28 ^^M
(./cv.test.aux)
! LaTeX Error: \RequirePackage or \LoadClass in Options Section.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.29 \begin{document}
[1] (./cv.test.aux) )
(\end occurred when \iftrue on line 271 was incomplete)
(\end occurred when \ifx on line 271 was incomplete)
(\end occurred when \ifx on line 271 was incomplete)
(\end occurred when \ifx on line 271 was incomplete)
(\end occurred when \ifx on line 271 was incomplete)
(\end occurred when \ifx on line 271 was incomplete)
(see the transcript file for additional information)
Output written on cv.test.pdf (1 page).
Transcript written on cv.test.log.
LaTeX exited abnormally with code 1 at Sun Jan 19 12:46:17
我怀疑这是因为 LaTeX 提供的传统选项解析可能没有考虑到括号组。(如果不弄乱 catcodes,这是否可能?!?)例如,如果 LaTeX 将选项拆分为,我可以看到此错误,[test], [key={a], [b}]
但我只是不明白这有什么意义。
是否可以在类级选项中启用此键样式,或者我应该跳转到单独的设置功能(类似于\hypersetup{…}
)?
答案1
正如在评论,类选项列表中的括号通常会导致问题,或者至少是有风险的。LaTeX2e 处理选项的方式不是“原生”使用 keyval 方法:所有允许使用包或类 keyval 选项的包都是通过后处理内核传递的选项来实现的key=val
。用于从全局列表中删除“已使用”类选项的代码无法处理括号的存在,因此如果包识别出带有一些括号内容的类选项,则会导致错误。这些都不是 所独有的l3keys2e
。
LaTeX2e 选项处理系统的设置方式意味着无法安全地加载包并让其更改选项处理例程:这“太晚了”。CTAN 上有两个包确实重新定义了选项处理系统,但要有效使用它们,必须加载它们前行\documentclass
使用\RequirePackage
。因此,它们不能被视为“一般修复”:设置是非标准的。