考虑以下依赖于选项管理的类的代码pgfopts
:
% CLASS
\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesClass{myclass}[2022/10/11]
\LoadClass{standalone}
\makeatletter
\RequirePackage{pgfopts}
\pgfkeys{
/myclass/.cd,
myoption/.is choice,
myoption/true/.code = {\newcommand{\myclass@myoption}{true}},% newcommand or renewcommand?
myoption/false/.code = {\newcommand{\myclass@myoption}{false}},% newcommand or renewcommand?
myoption/.default = true,% default or initial?
}
\ProcessPgfOptions{/myclass}
% Things to modify
\def\myclass@true{true}% I would like to avoid to have to define this
\def\myclass@false{false}% I would like to avoid to have to define this
\newcommand{\myclass@displayboolean}[1]{%
\ifx\myclass@myoption\myclass@true% This seems to fail
\fbox{\texttt{MY OPTION = TRUE}}%
\else%
\fbox{\texttt{MY OPTION = FALSE}}%
\fi%
}
% This has to stay the same if possible
\newcommand{\printmyoption}{\myclass@displayboolean{\myoption}}
\makeatother
以及它的以下用法:
\documentclass{myclass}
\begin{document}
\printmyoption
\end{document}
或者
\documentclass[myoption = true]{myclass}
\begin{document}
\printmyoption
\end{document}
或者
\documentclass[myoption = false]{myclass}
\begin{document}
\printmyoption
\end{document}
问题:在所有情况下,它都会输出,MY OPTION = FALSE
所以我猜命令中的测试\myclass@displayboolean
失败了。我尝试了几种方法,但还没有找到正确的方法。
问题:如何正确地定义myoption
为布尔选择,以及如何在宏内正确地对其进行分支?
答案1
您正在使用\newcommand
,它创建“长”宏(可以\par
在参数中接受标记),然后与使用 创建的宏进行比较\def
,它是“短”的。即使这两个宏不接受任何参数,它们也不\ifx
相等。使用相同的方法保存您的标记和比较版本:
\newcommand*\myclass@myoption{}
\pgfkeys{
/myclass/.cd,
myoption/.is choice,
myoption/true/.code = {\def\myclass@myoption{true}},
myoption/false/.code = {\def\myclass@myoption{false}},
myoption/.default = true,
}