testclass.cls:
\ProvidesClass{testclass}
\NeedsTeXFormat{LaTeX2e}
\RequirePackage{xkeyval}
\define@key{family}{key1}[]{\def\mytext{#1}}
\define@key{family}{key2}[]{\def\MyText{#1}}
\presetkeys{family}{key1=12345, key2=abcde}{}
\DeclareOptionX{text}{\setkeys{family}{#1}}
\ProcessOptionsX
\ExecuteOptionsX{text={key1=12345, key2=abcde}}
\LoadClass{ctexbook}
main.tex,不会报错:
\documentclass{testclass}
\begin{document}
\mytext
\MyText
\end{document}
main.tex,会报错:
\documentclass[text={key1=67890}]{testclass}
\begin{document}
\mytext
\MyText
\end{document}
错误信息如下:
main.tex:
\RequirePackage or \LoadClass in Options Section.
The font size command \normalsize is not defined:
Unused global option(s):
[text={key1=67890}].
testclass.cls:
Missing \begin{document}.
You can't use `macro parameter character #' in horizontal mode.
\@removeelement #1#2#3->\def \reserved@a ##1,#1,##
Paragraph ended before \reserved@a was complete.
<to be read again>
请问有什么办法能解决?
testclass.cls
\ProvidesClass{testclass}
\NeedsTeXFormat{LaTeX2e}
\RequirePackage{l3keys2e}
\ExplSyntaxOn
%定义 keyval,第一个参数为模块名,第二个参数为所要设置的 keyval 列表
\keys_define:nn{family}
{
name.tl_set:N = \l_name_tl,
age.int_set:N = \l_age_int,
}
\def\myname{\textbf{\tl_use:N \l_name_tl}}
\def\myage{\textbf{\int_use:N \l_age_int}}
% 设置默认值
\keys_set:nn{family}
{
name = ljguo,
age = 21,
}
\ExplSyntaxOff
% 传递 family 模块的 keyval list 给 class
\ProcessKeysOptions{family}
\LoadClass{ctexbook}
main.tex
\documentclass{testclass}
\begin{document}
my name is \myname, and i am \myage ~years old.
\end{document}
这将得到:
\documentclass[name = zhangsan,age = 81]{testclass}
\begin{document}
my name is \myname, and i am \myage ~years old.
\end{document}
这将得到:
texdoc interface3
了解更详细的用法