在.bib
文件中,我维护一些文件字段localfile
:
@book{ST,
author = {Mister X},
series = {Notes},
localfile = {./SetTheory.pdf},
shorthand = {\href{run:./SetTheory.pdf}{ST}},
title = {Set Theory},
year = {2018}
}
该代码运行良好,但我想使其自动化。
简单的解决方案是从变量中获取
localfile
,如下所示:shorthand = {\href{run:\localfie}{ST}},
同样适用于
shorthand
shorthand = {\href{run:\localfie}{\shorthand}},
这样我就可以使用 Latex 生成的简写
最好的解决方案是修改速记的打印方式,逻辑如下:如果 localfile 存在
shorthand = {\href{run:\localfile}{ST}},
否则让乳胶正常工作。
答案1
以下示例使用已知字段file
而不是未知字段localfile
。如果要使用,localfile
则必须先定义自定义数据模型。
在该shorthand
字段中您只需提供简写,无需额外的\href
标记。
如果file
存在,则引用中的简写链接到外部文件,否则它只是像往常一样链接到参考书目条目。
\documentclass[british]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{csquotes}
\usepackage[style=authoryear, backend=biber]{biblatex}
\usepackage{hyperref}
\DeclareFieldFormat{shorthandlink}{%
\iffieldundef{file}
{\bibhyperref{#1}}
{\ifhyperref
{\href{run:\thefield{file}}{#1}}
{#1}}}
\renewbibmacro*{cite:shorthand}{%
\printtext[shorthandlink]{\printfield{shorthand}}}
\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@book{appleby,
author = {Humphrey Appleby},
title = {On the Importance of the Civil Service},
date = {1980},
shorthand = {CS},
}
@online{elk,
author = {Anne Elk},
title = {A Theory on Brontosauruses},
url = {http://example.edu/~elk/bronto.pdf},
file = {./bronto.pdf},
shorthand = {EB},
}
\end{filecontents}
\addbibresource{\jobname.bib}
\begin{document}
\cite{elk,appleby}
\printbibliography
\end{document}
PDF 有
3 0 obj
<<
/Type /Annot
/Border[0 0 1]/H/I/C[0 .7 .7]
/Rect [147.716 654.302 163.542 665.093]
/Subtype/Link/A<</F(./bronto.pdf)/S/Launch>>
>>
endobj
4 0 obj
<<
/Type /Annot
/Subtype /Link
/Border[0 0 1]/H/I/C[0 1 0]
/Rect [167.636 654.302 182.356 665.093]
/A << /S /GoTo /D (cite.0@appleby) >>
>>
endobj
如果你只希望将其用于参考书目中的标签,则需要
\documentclass[british]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{csquotes}
\usepackage[style=alphabetic, backend=biber]{biblatex}
\usepackage{hyperref}
\newcommand*{\iffilelink}{%
\ifboolexpr{ test {\ifhyperref}
and test {\ifbibliography}
and not test {\iffieldundef{file}}}}
\DeclareFieldFormat{labelalphawidth}{%
\mkbibbrackets{%
\iffilelink
{\href{run:\thefield{file}}{#1}}
{#1}}}
\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@book{appleby,
author = {Humphrey Appleby},
title = {On the Importance of the Civil Service},
date = {1980},
shorthand = {CS},
}
@online{elk,
author = {Anne Elk},
title = {A Theory on Brontosauruses},
url = {http://example.edu/~elk/bronto.pdf},
file = {./bronto.pdf},
shorthand = {EB},
}
\end{filecontents}
\addbibresource{\jobname.bib}
\begin{document}
\cite{elk,appleby}
\printbibliography
\end{document}
现在逻辑稍微复杂一些,因为我们需要在测量标签长度时停止使用链接,所以我们添加了检查\ifbibliography
。
对于numeric
您需要重新定义的风格,labelnumberwidth
而不是labelalphawidth
。