我想将包cmdkey
中的传递xkeyval
给宏。我可以将的值打印cmdkey
到消息中,但与\in@
它一起使用时似乎未展开。示例如下。我希望看到消息
the value is aaacde. the action is true.
相反,我只看到the value is aaacde
。当我使用字符串进行比较时,它确实有效(参见注释掉的行)。
\documentclass{article}
\usepackage{xkeyval}
\newif\ifaction
\makeatletter
\define@cmdkeys{links}[link@]{book,chap}{}
\setkeys{links}{book=aaacde, chap=asdf}
\def\testme{%
\message{the value is \link@book.}
\in@{aaa}{\link@book}% does not work
%\in@{aaa}{aaabcd}% works
\ifin@\actiontrue\message{the action is true.}\fi
}
\makeatother
\begin{document}
text\testme
\end{document}
答案1
您需要在执行其操作\link@book
之前进行扩展:\in@
\documentclass{article}
\usepackage{xkeyval}
\newif\ifaction
\makeatletter
\define@cmdkeys{links}[link@]{book,chap}{}
\setkeys{links}{book=aaacde, chap=asdf}
\def\reversed@in@#1#2{\in@{#2}{#1}}% <-- Added this
\def\testme{%
\message{the value is \link@book.}
\expandafter\reversed@in@\expandafter{\link@book}{aaa}% <-- Changed here
%\in@{aaa}{aaabcd}% works
\ifin@\actiontrue\message{the action is true.}\fi
}
\makeatother
\begin{document}
text\testme
\end{document}
我添加了一个\reversed@in@
宏,这样我们就可以扩展第一个参数而不是第二个参数。根据经验,参数越靠后,扩展就越困难(不是总是虽然如此)。因此我使用\reversed@in@
宏将参数交换为\in@
。
答案2
您可以定义\expandedin@
:
\documentclass{article}
\usepackage{xkeyval}
\newif\ifaction
\makeatletter
\newcommand{\expandedin@}[2]{%
\begingroup\edef\x{\endgroup\noexpand\in@{#1}{#2}}\x
}
\define@cmdkeys{links}[link@]{book,chap}{}
\def\testme{%
\message{the value is \link@book.}%
\expandedin@{aaa}{\link@book}%
\ifin@\actiontrue\message{the action is true.}\fi
}
\makeatother
\setkeys{links}{book=aaacde, chap=asdf}
\begin{document}
text\testme
\end{document}
使用较新版本的引擎(TeX Live 2019 可用),您可以使用\expanded
。
\documentclass{article}
\usepackage{xkeyval}
\newif\ifaction
\makeatletter
\define@cmdkeys{links}[link@]{book,chap}{}
\def\testme{%
\message{the value is \link@book.}%
\expanded{\noexpand\in@{aaa}{\link@book}}%
\ifin@\actiontrue\message{the action is true.}\fi
}
\makeatother
\setkeys{links}{book=aaacde, chap=asdf}
\begin{document}
text\testme
\end{document}