我不知道为什么这个测试失败以及如何让它工作:
\documentclass{article}
\usepackage{filecontents}
\begin{filecontents}{myTestSty.sty}
\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{myTestSty}[2018/11/29 v. 1.0 myTestSty]
\RequirePackage{kvoptions}
\SetupKeyvalOptions{family=@myTestSty,prefix=@myTestSty@}
\DeclareStringOption{myKey}
\def\myKey{\@myTestSty@myKey}
\ProcessKeyvalOptions*
\end{filecontents}
\usepackage{etoolbox}
\usepackage[%
%myKey={Hello world!},
myKey={} %<-- blank?
]{myTestSty}
\begin{document}
myKey: --{\myKey}--
myKey: \ifblank{\myKey}{blank}{not-blank}
\end{document}
答案1
三个错误:
\@myTestSty@myKey
定义的时候必须要扩展\myKey
;- 你必须扩展
\myKey
来测试它的替换文本是否为空; \myKey
应该去的定义后处理选项。
\documentclass{article}
\usepackage{filecontents}
\begin{filecontents}{myTestSty.sty}
\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{myTestSty}[2018/11/29 v. 1.0 myTestSty]
\RequirePackage{kvoptions}
\SetupKeyvalOptions{family=@myTestSty,prefix=@myTestSty@}
\DeclareStringOption{myKey}
\ProcessKeyvalOptions*
\edef\myKey{\expandonce{\@myTestSty@myKey}} % <--- expand
\end{filecontents}
\usepackage{etoolbox}
\usepackage[%
%myKey={Hello world!},
myKey={} %<-- blank?
]{myTestSty}
\begin{document}
myKey: X{\myKey}X
myKey: \expandafter\ifblank\expandafter{\myKey}{blank}{not-blank}
\end{document}
您可以通过定义来缩短测试
\newcommand{\ifblanke}[1]{%
\expandafter\ifblank\expandafter{#1}%
}
并调用
\ifblanke{\myKey}{blank}{not~blank}
采用不同的方法l3keys2e
:
\documentclass{article}
\usepackage{filecontents}
\begin{filecontents}{myTestSty.sty}
\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{myTestSty}[2018/11/29 v. 1.0 myTestSty]
\RequirePackage{expl3,l3keys2e,xparse}
\ExplSyntaxOn
\keys_define:nn { mytest/main }
{
myKey .tl_set:N = \myKey,
}
\ProcessKeysPackageOptions { mytest/main }
\NewExpandableDocumentCommand{\IfBlankTF}{smmm}
{
\IfBooleanTF{#1}
{
\tl_if_blank:VTF #2 { #3 } { #4 }
}
{
\tl_if_blank:nTF #2 { #3 } { #4 }
}
}
\ExplSyntaxOff
\end{filecontents}
\usepackage[%
%myKey={Hello world!},
myKey={} %<-- blank?
]{myTestSty}
\begin{document}
myKey: X{\myKey}X
myKey: \IfBlankTF*{\myKey}{blank}{not-blank}
\end{document}