BibLaTeX' \citefield 未按预期扩展

BibLaTeX' \citefield 未按预期扩展

我遇到了一个看起来像是宏扩展的问题。请考虑以下示例:

\documentclass{article}
\usepackage{hyperref}
\usepackage{biblatex}

\begin{filecontents}{expprob.bib}
@online{example,
  shorthand    = {foo-bar},
  url          = {http://example.com}
}
\end{filecontents}

\bibliography{expprob}

\newcommand{\demofield}[2]{foo-#1}
\newcommand{\demourl}[1]{\url{http://example.com/link/\demofield{#1}{shorthand}}}

\newcommand{\biburl}[1]{\url{http://example.com/link/\citefield{#1}{shorthand}}}

\begin{document}

\textbf{without biblatex}

expected: \url{http://example.com/link/foo-bar}

actual: \demourl{bar}

\textbf{with biblatex}

ze field: \citefield{example}{shorthand}

expected: \url{http://example.com/link/foo-bar}

actual: \biburl{example}

\end{document}

命令\demourl\biburl非常相似,它们只是使用不同的宏来确定 URL 的一部分。处理此文件(例如使用latexmk)会产生以下结果:

问题

如您所见,该\citefield命令正常工作并检索属性的内容shorthand。问题是宏未在内展开\url,而看似相似的宏\demofield却正确展开。我不明白这一点——这里发生了什么,我如何让系统\citefield也展开?

答案1

在阅读了一些有关强制 *TeX 扩展受保护命令所涉及的麻烦后,我改变了我的方法并使用了 BiBTeX 工具。以下代码包含三个命令集 - 一个用于打印出原始 URL,一个用于格式化和链接 URL \url,一个用于创建\href带有任意文本的超链接。

\documentclass{article}
\usepackage{hyperref}
\usepackage{biblatex}

% These are the parts used to print a raw URL without any formatting.
\DeclareFieldFormat{rawurl}{http://example.com/link/#1}
\DeclareCiteCommand{\rawurlcite}{}{\printfield[rawurl]{shorthand}}{}{}

% These are the parts used to print a hyperlinked URL.
\DeclareFieldFormat{linkurl}{\url{http://example.com/link/#1}}
\DeclareCiteCommand{\linkurlcite}{}{\printfield[linkurl]{shorthand}}{}{}

% This provides a \hrefcite{key}{text} command
\makeatletter
\DeclareCiteCommand{\@saveurlcite}{}{\savefield{shorthand}{\@savedshorthand}}{}{}
\newcommand{\hrefcite}[2]{%
\@saveurlcite{#1}%
\edef\@savedurl{http://example.com/link/\@savedshorthand}%
\href{\@savedurl}{#2}%
\let\@savedurl\empty%
\let\@savedshorthand\empty}

\begin{filecontents}{expprob.bib}
@online{example,
  shorthand    = {foo-bar},
  url          = {http://example.com}
}
\end{filecontents}

\bibliography{expprob}

\newcommand{\demofield}[2]{foo-#1}
\newcommand{\demourl}[1]{\url{http://example.com/link/\demofield{#1}{shorthand}}}

\newcommand{\biburl}[1]{\expandafter\url{\qlcite{#1}}}

\begin{document}

\textbf{without biblatex}

expected: \url{http://example.com/link/foo-bar}

actual: \demourl{bar}

\textbf{with biblatex}

ze field: \citefield{example}{shorthand}

rawurlcite: \rawurlcite{example}

linkurlcite: \linkurlcite{example}

a hyperlink: \hrefcite{example}{example text}

\end{document}

相关内容