我正在编写一个 LaTeX 程序包,用于漂亮地打印氨基酸残基和数字以及可选符号。我创建了一个lua
程序,它根据可自定义的格式输出,\StyleOption{}
使用lua
表格('sl'
表示'as'
短下划线、缩写句子大小写等),该表格按预期工作(还有更多残基,如等ala_abbrev
,tyr_abbrev
但它们已针对这个问题进行了精简):
\documentclass{article}
\usepackage{luacode}
\begin{luacode*}
his_abbrev = {['sl'] = "h", ['su'] = "H", ['al'] = "his", ['as'] = "His", ['au'] = "HIS"}
ile_abbrev = {['sl'] = "i", ['su'] = "I", ['al'] = "ile", ['as'] = "Ile", ['au'] = "ILE"}
lys_abbrev = {['sl'] = "k", ['su'] = "K", ['al'] = "lys", ['as'] = "Lys", ['au'] = "LYS"}
amino_acid_names = {
his_abbrev, ile_abbrev, lys_abbrev,
}
function amino_acid_name(residue, style)
for _, table in ipairs (amino_acid_names) do
for key, val in pairs (table) do
if tostring (val) == residue then
tex.print (table[style])
end
end
end
end
\end{luacode*}
\newcommand\StyleOption{as}
\newcommand\residueString[2]{\directlua{
amino_acid_name ("\luaescapestring{#1}", "\luaescapestring{#2}")}}
\NewDocumentCommand{\aail}{o m m}{%
\IfValueTF{#1}{%
\residueString{#2}{\StyleOption}%
\textsuperscript{$\scriptstyle\mkern-1mu#1\mkern-1.5mu$} $\mkern-2mu#3$%
}{%
\residueString{#2}{\StyleOption} $\mkern1.5mu#3$%
}%
}
\begin{document}
\aail{i}{434}
\aail[*]{lys}{112}
\aail{HIS}{88}
\end{document}
我的问题来自于尝试将上述内容转换为 LaTeX3 格式。我广泛使用 LuaLaTeX,所以我很高兴这个包需要,lua
但其他人可能不需要。我尝试过如何在 LaTeX3 中定义/迭代嵌套属性列表但我不确定如何迭代prop
包含props
(\l_abbrev_all_prop
)并且LaTeX3 接口文档可能提供了解决方案,但当你不知道要搜索什么时,它会很令人生畏。
问题:如何才能让以下 LaTeX3 代码\ResidueString
根据\StyleOption
类似于lua
代码中定义的键进行打印?
\documentclass{article}
\ExplSyntaxOn
\NewDocumentCommand{\StyleOption}{}{as} % for the key to \l_abbrev_his_prop etc
\prop_new:N \l_abbrev_his_prop
\prop_new:N \l_abbrev_ile_prop
\prop_new:N \l_abbrev_lys_prop
\prop_new:N \l_abbrev_all_prop
% to save space in Q, just putting them all on one line
\prop_set_from_keyval:Nn \l_abbrev_his_prop {sl = h, su = H, al = his, as = His, au = HIS}
\prop_set_from_keyval:Nn \l_abbrev_ile_prop {sl = i, su = I, al = ile, as = Ile, au = ILE}
\prop_set_from_keyval:Nn \l_abbrev_lys_prop {sl = k, su = K, al = lys, as = Lys, au = LYS}
\prop_set_from_keyval:Nn \l_abbrev_all_prop
{
his = \l_abbrev_his_prop,
ile = \l_abbrev_ile_prop,
lys = \l_abbrev_lys_prop
}
% \cs_new:Npn \residueString:nn #1#2
% {
% \prop_map_function:NN \l_abbrev_all_prop #1#2 % What goes here?
% }
\NewDocumentCommand { \aail } { o m m }
{
\IfValueTF{#1}{%
\residueString{ #2 }{ \StyleOption }%
\textsuperscript{$\scriptstyle\mkern-1mu#1\mkern-1.5mu$} $\mkern-2mu#3$%
}{%
\residueString{ #2 }{ \StyleOption } $\mkern1.5mu#3$%
}%
}
\ExplSyntaxOff
\begin{document}
\aail{i}{434}
\aail[*]{lys}{112}
\aail{HIS}{88}
\end{document}
注意:这一切都是可定制的,包括添加自定义残基、字距距离、上标/下标、在包选项/\SetResidueOptions{key=val}
等中设置的浮动标题选项,但这只是一个最小的例子。
答案1
Lua 的一个相当直译的翻译是:
\documentclass{article}
\ExplSyntaxOn
\NewExpandableDocumentCommand{\StyleOption}{}{as} % for the key to \l_abbrev_his_prop etc
\prop_new:N \l_abbrev_his_prop
\prop_new:N \l_abbrev_ile_prop
\prop_new:N \l_abbrev_lys_prop
\prop_new:N \l_abbrev_all_prop
% to save space in Q, just putting them all on one line
\prop_set_from_keyval:Nn \l_abbrev_his_prop {sl = h, su = H, al = his, as = His, au = HIS}
\prop_set_from_keyval:Nn \l_abbrev_ile_prop {sl = i, su = I, al = ile, as = Ile, au = ILE}
\prop_set_from_keyval:Nn \l_abbrev_lys_prop {sl = k, su = K, al = lys, as = Lys, au = LYS}
\prop_set_from_keyval:Nn \l_abbrev_all_prop
{
his = \l_abbrev_his_prop,
ile = \l_abbrev_ile_prop,
lys = \l_abbrev_lys_prop
}
\cs_new:Npn \residueString:nn #1#2
{
\tl_set:Nx\l_residue{#1}
\tl_set:Nx\l_style{#2}
\prop_map_function:NN \l_abbrev_all_prop \__abbrev_aux:nn
}
\cs_generate_variant:Nn\prop_item:Nn{Ne}
\cs_new:Npn \__abbrev_aux:nn #1#2
{
\prop_map_inline:Nn #2 {
\tl_if_eq:NnT
\l_residue
{##2}
{\prop_item:Ne#2{\l_style}}
}
}
\NewDocumentCommand { \aail } { o m m }
{
\IfValueTF{#1}{%
\residueString:nn{ #2 }{ \StyleOption }%
\textsuperscript{$\scriptstyle\mkern-1mu#1\mkern-1.5mu$}~ $\mkern-2mu#3$%
}{%
\residueString:nn{ #2 }{ \StyleOption } ~ $\mkern1.5mu#3$%
}%
}
\ExplSyntaxOff
\begin{document}
\aail{i}{434}
\aail[*]{lys}{112}
\aail{HIS}{88}
\end{document}