![`\url` 中的嵌套命令](https://linux22.com/image/399536/%60%5Curl%60%20%E4%B8%AD%E7%9A%84%E5%B5%8C%E5%A5%97%E5%91%BD%E4%BB%A4.png)
我正在尝试向我的文档添加 DOI 链接,并期望以下操作能够正常工作:
\url{https://doi.org/\citefield{Knu86}{doi}}
然而,我得到了这个:
https://doi.org/\citefield{Knu86}{doi}
完整 MWE:
\documentclass{article}
\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@book{Knu86,
author = {Knuth, Donald E.},
year = {1986},
title = {The \TeX book},
doi = {101},
}
\end{filecontents}
\usepackage{biblatex}
\addbibresource{\jobname.bib}
\begin{document}
This works: https://doi.org/\citefield{Knu86}{doi}
This doesn't: \url{https://doi.org/\citefield{Knu86}{doi}}
\end{document}
答案1
该命令\citefield
不可扩展,这意味着它可以用于排版(格式化)字段内容,但不能用于 LaTeX 直接需要实际文本字符串的地方(例如\url
;当然,\url
这更加棘手,因为它实际上并不执行其参数中的宏,而是\url{\foo}
输出为“ \foo
”,因此\citefield
即使它是可扩展的也不会起作用)。
您可以定义一个新的宏来直接打印 DOI
\documentclass[british]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{csquotes}
\usepackage[style=authoryear, backend=biber]{biblatex}
\addbibresource{biblatex-examples.bib}
% based on \DeclareFieldFormat{doi} in biblatex.def
%\DeclareFieldFormat{citedoi}{%
% \ifhyperref
% {\href{https://doi.org/#1}{\nolinkurl{#1}}}
% {\nolinkurl{#1}}}
%
% or
\DeclareFieldFormat{citedoi}{\url{https://doi.org/#1}}
\DeclareCiteCommand{\citedoi}
{\boolfalse{citetracker}%
\boolfalse{pagetracker}%
\usebibmacro{prenote}}
{\printfield[citedoi]{doi}}
{\multicitedelim}
{\usebibmacro{postnote}}
\begin{document}
\citedoi{sigfridsson}
\cite{sigfridsson}
\printbibliography
\end{document}