我尝试用pgfkeys
和编写一个包etoolbox
。我想要定义一个布尔值和一个键。根据键是真还是假,我想要加载一个包。
布尔值似乎有效,但根据日志文件中列出的包,包并未加载。
\begin{filecontents}{iotestm.sty}
\ProvidesPackage{iotestm}[mwe for golatex]
\RequirePackage{etoolbox, pgfopts}
%---------------------------------------------------
% Defining package options
%---------------------------------------------------
\newbool{test@if@prettyfont}
\pgfkeys{
test/.cd,
pretty-font/.is choice,
pretty-font/true/.code = \booltrue{test@if@prettyfont},
pretty-font/false/.code= {\boolfalse{test@if@prettyfont}},
pretty-font/.initial = true
}
\newrobustcmd\testsetup[1]{\pgfqkeys{/test}{#1}}
\ProcessPgfOptions{/test}
%---------------------------------------------------
% Defining package commands
%---------------------------------------------------
\ifbool{test@if@prettyfont}{%
\RequirePackage{lmodern}%
}{}
\newcommand{\testfont}{\ifbool{test@if@prettyfont}{true}{false}}
\end{filecontents}
\listfiles
\documentclass{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{iotestm}
\testsetup{%
pretty-font = false
}
\begin{document}
Font: \testfont
\end{document}
有人能告诉我我做错了什么吗?或者我到目前为止所做的使用是否完全错误?
答案1
该宏在加载\testsetup
后很长一段时间才使用——在加载时,未设置为,并且测试扩展为,从而不会加载所请求的包。 iotestm
test@if@prettyfont
true
false
这个\ifbool{...}{\RequirePackage{...}}{...}
测试可以推迟到前导钩子的末尾,用\AtEndPreamble
,此时\testsetup
宏已经被调用并设置了相关数据。
\begin{filecontents}{iotestm.sty}
\ProvidesPackage{iotestm}[mwe for golatex]
\RequirePackage{etoolbox}
\RequirePackage{pgfopts}
%---------------------------------------------------
% Defining package options
%---------------------------------------------------
\newbool{test@if@prettyfont}
\pgfkeys{
test/.cd,
pretty-font/.is choice,
pretty-font/true/.code = {\booltrue{test@if@prettyfont}},
pretty-font/false/.code= {\boolfalse{test@if@prettyfont}},
pretty-font/.initial=true,
}
\newrobustcmd\testsetup[1]{\pgfqkeys{/test}{#1}}
\ProcessPgfOptions{/test}
%---------------------------------------------------
% Defining package commands
%---------------------------------------------------
\AtEndPreamble{
\ifbool{test@if@prettyfont}{%
\RequirePackage{lmodern}%
}{}
}
\newcommand{\testfont}{\ifbool{test@if@prettyfont}{true}{false}}
\end{filecontents}
\listfiles
\documentclass{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{iotestm}
\testsetup{%
pretty-font=true
}
\begin{document}
Font: \testfont
\end{document}