![BibLaTeX 中的自訂字段](https://rvso.com/image/449617/BibLaTeX%20%E4%B8%AD%E7%9A%84%E8%87%AA%E8%A8%82%E5%AD%97%E6%AE%B5.png)
我想在我的 .bib 條目中允許有一個可選的自訂欄位。我不需要這些字段成為參考書目的一部分,但希望能夠透過以下方式存取這些字段
\citefield{<entry-key>}{<custom-field-name>}
答案可能就在下面列出的參考文獻中的某個地方,但我最初的適應嘗試失敗\DeclareSourcemap
了\DeclareFieldFormat
。
參考:
代碼:
\begin{filecontents}{mybib.bib}
@book{knuth1984texbook,
title={The texbook},
author={Knuth, D.E. and Knuth, A.D. and Bibby, D. and American Mathematical Society and Addison-Wesley Publishing Company and Addison-Wesley},
isbn={9780201134483},
lccn={85030845},
series={Computers \& typesetting},
url={https://books.google.com/books?id=hEYuAQAAIAAJ},
year={1984},
publisher={Addison-Wesley},
myFieldA={Useful Book},
myFieldB={on Shelf 4},
}
@book{goossens1994latex,
title={The LaTeX Companion},
author={Goossens, M. and Mittelbach, F. and Samarin, A.},
isbn={9780201541991},
lccn={lc93023150},
series={Addison-Wesley series on tools and techniques for computer typesetting},
url={https://books.google.com/books?id=54A3MuBzIrEC},
year={1994},
publisher={Addison-Wesley},
myFieldA={Also Useful Book},
myFieldB={on Shelf 5},
}
\end{filecontents}
%% -----------------
\documentclass{article}
\usepackage{biblatex}
\addbibresource{mybib.bib}
\begin{document}
This should print ``Also Useful Book'':
\citefield{goossens1994latex}{myFieldA}
\end{document}
答案1
如果您只需要幾個字段,並且願意犧牲一點標記語義(用於引文),您可以使用“自訂字段”biblatex
提供(標準樣式不使用它們,但如果您使用的是非標準的,您可能需要檢查它是否真的是“空的”)。
biblatex
鑑於您在評論中提到您希望為欄位指定自訂名稱,這可以透過使用 SourceMap 將值從自訂欄位複製到 的資料模型中的可用欄位之一來實現。這意味著您可以在文件中使用自訂名稱.bib
,但在實際引用時您仍然必須使用biblatex
「知道」欄位。
這可能是這樣的:
\begin{filecontents}[overwrite]{mybib.bib}
@book{knuth1984texbook,
title={The texbook},
author={Knuth, D.E. and Knuth, A.D. and Bibby, D. and American Mathematical Society and Addison-Wesley Publishing Company and Addison-Wesley},
isbn={9780201134483},
lccn={85030845},
series={Computers \& typesetting},
url={https://books.google.com/books?id=hEYuAQAAIAAJ},
year={1984},
publisher={Addison-Wesley},
myFieldA={Useful Book},
myFieldB={on Shelf 4},
}
@book{goossens1994latex,
title={The LaTeX Companion},
author={Goossens, M. and Mittelbach, F. and Samarin, A.},
isbn={9780201541991},
lccn={lc93023150},
series={Addison-Wesley series on tools and techniques for computer typesetting},
url={https://books.google.com/books?id=54A3MuBzIrEC},
year={1994},
publisher={Addison-Wesley},
myFieldA={Also Useful Book},
myFieldB={on Shelf 5},
}
\end{filecontents}
%% -----------------
\documentclass{article}
\usepackage{biblatex}
\addbibresource{mybib.bib}
\DeclareSourcemap{
\maps[datatype=bibtex]{
\map{
\step[fieldsource=myFieldA]
\step[fieldset=usera, origfieldval]
\step[fieldsource=myFieldB]
\step[fieldset=userb, origfieldval]
}
}
}
\begin{document}
This should print ``Also Useful Book'':
\citefield{goossens1994latex}{usera}
This should print ``on Shelf 5'':
\citefield{goossens1994latex}{userb}
\end{document}
這種方法的優點是不需要擴展資料模型,因此比較簡單。但是,如果您確實需要在兩側使用自訂欄位名稱,那麼據我所知,唯一的方法是擴展資料模型以包含您的欄位。在這種情況下,規範的答案是 @moewe 的答案:如何使用 BibLaTeX/Biber 建立全新的資料類型?。