我想使用文字数据注释作为本地化键。一些用例包括:用相关类型修饰 ISBN 列表中的项目,例如“精装”或“电子书”;以及(有选择地)指定位置,例如“某个出版商:马萨诸塞州阿默斯特”但“马萨诸塞大学:阿默斯特”。我还没能弄清楚如何让它工作;请参阅下面的 MWE。
\documentclass{article}
\usepackage[%
style=authoryear,%
]{biblatex}
\addbibresource{\jobname.bib}
\usepackage{xpatch}
\begin{filecontents}[force]{\jobname.bib}
@book{foo,
author = {John Shade and Charles Kinbote},
author+an = {2:family="commentator"},
date = {2000-01-01},
date+an = {="prepublished"},
title = {The Title},
title+an = {="fromenglish"},
location = {Cambridge},
location+an = {1="countryus"},
}
\end{filecontents}
% Can't use part annotation as bibstring:
%\xapptobibmacro{name:given-family}{ \bibxstring{\getpartannotation{family}}}{}{}
\xapptobibmacro{name:given-family}{ {\getpartannotation{family}}}{}{}
% Can't use item annotation as bibstring:
%\xapptolistformat{location}{ \bibxstring{\getitemannotation}}{}{}
\xapptolistformat{location}{ {\getitemannotation}}{}{}
% Can't use field annotation as bibstring:
%\xapptofieldformat{titlecase}{ \bibxstring{{\getfieldannotation}}}{}{}
\xapptofieldformat{titlecase}{ {\getfieldannotation}}{}{}
% Same with date fields:
%\xapptobibmacro{date+extradate}{ \bibxstring{\getfieldannotation[date]}}{}{}
\xapptobibmacro{date+extradate}{ {\getfieldannotation[date]}}{}{}
\begin{document}
\nocite{*}
\printbibliography
\end{document}
此代码编译成功,并成功在预期位置打印文字注释。如果我用注释掉的xappto
版本替换其中一行未注释的版本,则会出错Missing \endcsname
;注释会以粗体显示,就好像相应的本地化字符串未定义一样。用 替换\bibxstring
会\bibstring
更失败。
\bibstring
我可以通过将宏直接放入文件中的数据注释中来解决这个问题.bib
,但我发现这有点丑陋,想知道是否有更好的方法。
答案1
用于检索数据注释的宏通常不可扩展,因为它们使用可选参数。这意味着它们不能用作的参数\bibxstring
(当然也不能用作的参数\bibstring
)。
我们可以尝试定义可扩展地检索数据的新命令(没有可选参数)。
我想出了
\documentclass{article}
\usepackage[%
style=authoryear,%
]{biblatex}
\usepackage{xpatch}
\newcommand*{\expandablefieldannotation}[1]{%
\expandablefieldannotationmanual{#1}{}}
\newcommand*{\expandablefieldannotationmanual}[2]{%
\csuse{abx@annotation@literal@field@%
\ifstrempty{#1}{\currentfield}{#1}@%
\ifstrempty{#2}{default}{#2}}}
\newcommand*{\expandableitemannotation}[1]{%
\expandableitemannotationmanual{#1}{}{}}
\newcommand*{\expandableitemannotationmanual}[3]{%
\csuse{abx@annotation@literal@item@%
\ifstrempty{#1}{\currentlist}{#1}@%
\ifstrempty{#2}{default}{#2}@%
\ifstrempty{#3}{\the\value{listcount}}{#3}}}
\newcommand*{\expandablepartannotation}[2]{%
\expandablepartannotationmanual{#1}{}{}{#2}}
\newcommand*{\expandablepartannotationmanual}[4]{%
\csuse{abx@annotation@literal@part@%
\ifstrempty{#1}{\currentname}{#1}@%
\ifstrempty{#2}{default}{#2}@%
\ifstrempty{#3}{\the\value{listcount}}{#3}@#4}}
% Can't use part annotation as bibstring:
\xapptobibmacro{name:given-family}{ \bibxstring{\expandablepartannotation{}{family}}}{}{}
% Can't use item annotation as bibstring:
\xapptolistformat{location}{ \bibxstring{\expandableitemannotation{}} }{}{}
% Can't use field annotation as bibstring:
\xapptofieldformat{titlecase}{ \bibxstring{\expandablefieldannotation{}}}{}{}
% Same with date fields:
\xapptobibmacro{date+extradate}{ \bibxstring{\expandablefieldannotation{date}}}{}{}
\begin{filecontents}{\jobname.bib}
@book{foo,
author = {John Shade and Charles Kinbote},
author+an = {2:family="commentator"},
date = {2000-01-01},
date+an = {="prepublished"},
title = {The Title},
title+an = {="fromenglish"},
location = {Cambridge},
location+an = {1="countryus"},
}
\end{filecontents}
\addbibresource{\jobname.bib}
\begin{document}
\nocite{*}
\printbibliography
\end{document}
目前为止,但界面可以改进。