如果在加载时给出某些类选项,则会出现 hyperref 错误

如果在加载时给出某些类选项,则会出现 hyperref 错误

在下面的 MCE 中,一切都按预期工作:

\begin{filecontents}[overwrite]{myclass.cls}
\ProvidesExplClass{myclass}{2022-06-15}{0.1}{Class of mine}
\NeedsTeXFormat{LaTeX2e}
\LoadClass { article }    

\RequirePackage{l3keys2e}
\RequirePackage{xcolor}
\RequirePackage{hyperref}

\clist_const:Nn \c__myclass_default_pagecolor_clist {1.0, 0.98, 0.94}

\definecolor{myclass_pagecolor}{rgb}{\c__myclass_default_pagecolor_clist}

\keys_define:nn { myclass }{ 
   pagecolor .code:n = {  \definecolor{myclass_pagecolor}{rgb}{#1} } }

\ProcessKeysOptions { myclass }

\AddToHook{begindocument/before}{ \pagecolor{myclass_pagecolor} }

\NewDocumentCommand \myclasssetup { m } { \keys_set:nn { myclass } { #1 } }
\end{filecontents}

\documentclass%
% [pagecolor={0.5, 0.98, 0.94}]
{myclass}
\myclasssetup{pagecolor={0.5, 0.98, 0.94}}
\begin{document}
Foo
\end{document}

但是,一旦[pagecolor={0.5, 0.98, 0.94}]取消注释加载时类选项,编译就会失败并出现错误:

! LaTeX Error: Missing \begin{document}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.4020 \ProcessKeyvalOptions{Hyp}

值得注意的是,如果用\colorlet而不是来定义颜色\definecolor,则不会出现此问题:

\begin{filecontents}[overwrite]{myclass.cls}
\ProvidesExplClass{myclass}{2022-06-15}{0.1}{Class of mine}
\NeedsTeXFormat{LaTeX2e}
\LoadClass { article }

\RequirePackage{l3keys2e}
\RequirePackage{xcolor}
\RequirePackage{hyperref}

\tl_const:Nn \c__myclass_default_pagecolor_tl {cyan}

\colorlet{myclass_pagecolor}{\c__myclass_default_pagecolor_tl}

\keys_define:nn { myclass } {
  pagecolor .code:n = { \colorlet{myclass_pagecolor}{#1} }  }

\ProcessKeysOptions { myclass }

\AddToHook{begindocument/before}{ \pagecolor{myclass_pagecolor} }

\NewDocumentCommand \myclasssetup { m } { \keys_set:nn { myclass } { #1 } }
\end{filecontents}

\documentclass%
[pagecolor=yellow]
{myclass}
% \myclasssetup{pagecolor=yellow}
\begin{document}
Foo
\end{document}

这是怎么回事?

相关内容