我希望我描述得正确。在一个较大的项目中,我试图使我的文档级命令采用 CamelCase 格式,但当使用旨在动态调用命令的键和值时,这很困难,因为它们都是小写。我确信这是一个可扩展性问题,但我只是没有看到解决方案。我搜索了该网站,但找不到一个我认为显然相关的示例。
在 MWE 中,命令\AlwaysDogHere
、\AlwaysCatHere
和\AlwaysBotHere
通常位于序言中,但我已将它们移到文档中,以便可以轻松看到它们的结果。输出typeout{}
是正确的,但相关命令没有被正确地动态构建,这是我的问题。请参阅 MWE 中的注释。只要文档命令像键和值一样都是小写,一切都会正常工作。
我的尝试:
% !TEX program = lualatexmk
% !TEX encoding = UTF-8 Unicode
\documentclass{article}
\usepackage{pgfkeys}
\pgfkeys{%
/mwe/optiond/.cd,
initial@setup/.style={%
/mwe/options/mwe@example/.initial=dog,
},%
initial@setup,%
mykey/.is choice,
mykey/.default=cat,
mykey/dog/.style={/mwe/options/mwe@example=dog},
mykey/cat/.style={/mwe/options/mwe@example=cat},
mykey/bot/.style={/mwe/options/mwe@example=bot},
}%
\NewDocumentCommand{\AlwaysDogHere}{}{%
% This command does something related to dog.
%\relax
Dog
}%
\NewDocumentCommand{\AlwaysCatHere}{}{%
% This command does something related to cat.
%\relax
Cat
}%
\NewDocumentCommand{\AlwaysBotHere}{}{%
% This command does something related to bot.
%\relax
Bot
}%
% The key should be lowercase in the \typeout{} output.
\typeout{}%
\typeout{Working with \pgfkeysvalueof{/mwe/options/mwe@example}}%
\typeout{}%
\begin{document}
Hello.
% I need the key's first letter to be capitalized in the
% next line so the correct macro will be invoked dynamically
% in situ.
\csname Always\MakeUppercase\expandafter\expandafter\pgfkeysvalueof{/mwe/options/mwe@example} Here\endcsname%
\end{document}
答案1
你正在\Alwaysdog Here
用d
和 空格进行调用,你可以使用 进行完全扩展\expanded
(注意\MakeUppercase
这里需要 2022 LateX)
\documentclass{article}
\usepackage{pgfkeys}
\pgfkeys{%
/mwe/optiond/.cd,
initial@setup/.style={%
/mwe/options/mwe@example/.initial=dog,
},%
initial@setup,%
mykey/.is choice,
mykey/.default=cat,
mykey/dog/.style={/mwe/options/mwe@example=dog},
mykey/cat/.style={/mwe/options/mwe@example=cat},
mykey/bot/.style={/mwe/options/mwe@example=bot},
}%
\NewDocumentCommand{\AlwaysDogHere}{}{%
% This command does something related to dog.
%\relax
Dog
}%
\NewDocumentCommand{\AlwaysCatHere}{}{%
% This command does something related to cat.
%\relax
Cat
}%
\NewDocumentCommand{\AlwaysBotHere}{}{%
% This command does something related to bot.
%\relax
Bot
}%
% The key should be lowercase in the \typeout{} output.
\typeout{}%
\typeout{Working with \pgfkeysvalueof{/mwe/options/mwe@example}}%
\typeout{}%
\begin{document}
Hello.
% I need the key's first letter to be capitalized in the
% next line so the correct macro will be invoked dynamically
% in situ.
\expandafter\show\csname Always\MakeUppercase\expandafter\expandafter\pgfkeysvalueof{/mwe/options/mwe@example} Here\endcsname
\expandafter\show\csname Always\expanded{\noexpand\MakeUppercase\pgfkeysvalueof{/mwe/options/mwe@example}}Here\endcsname
\csname Always\expanded{\noexpand\MakeUppercase\pgfkeysvalueof{/mwe/options/mwe@example}}Here\endcsname
\end{document}