在 pylatex 中加入參考書目

在 pylatex 中加入參考書目

我有興趣使用 PyLatex 自動化包含多個條目的表,這樣我就不需要每次需要更改或新增條目時都手動檢查它。在此表的最後一列中,需要我輸入條目時的參考。

到目前為止,我還沒有找到任何如何將 PyLatex 與 Bibliography stiles 結合使用的範例,您知道如何操作的範例嗎?

答案1

實際上沒有什麼特別需要的,只需在文件的序言和\printbibliography(或)中添加適當的包等即可。\bibliography{<filename>}程式碼很短,而且我想,相當不言自明。

import pylatex as pl

# dict with data for table - dict key is citation key
tabledata = dict(
                 aksin=['Foo', 42],
                 angenendt=['Bar', 7],
                 bertram=['Baz',3.14],
                 doody=['Foobar',199]
                 )

doc = pl.Document()

# add biblatex package to preamble
doc.preamble.append(pl.Package('biblatex',options=['sorting=none']))
doc.preamble.append(pl.Command('addbibresource',arguments=["biblatex-examples.bib"]))

# make table
with doc.create(pl.Tabular("l l l",booktabs=True)) as table:
    table.add_row(['Desc','Number','Cite'])
    table.add_hline()
    for key in tabledata.keys():
        table.add_row(tabledata[key]+[pl.Command('cite',arguments=[key])])


doc.append(pl.Command('printbibliography'))

doc.generate_tex('ex')

該 Python 腳本將在以下位置產生此程式碼ex.tex

\documentclass{article}%
\usepackage[T1]{fontenc}%
\usepackage[utf8]{inputenc}%
\usepackage{lmodern}%
\usepackage{textcomp}%
\usepackage{lastpage}%
\usepackage{booktabs}%
%
\usepackage[sorting=none]{biblatex}%
\addbibresource{biblatex{-}examples.bib}%
%
\begin{document}%
\normalsize%
\begin{tabular}{@{}l l l@{}}%
\toprule%
Desc&Number&Cite\\%
\midrule%
Baz&3.14&\cite{bertram}\\%
Foobar&199&\cite{doody}\\%
Foo&42&\cite{aksin}\\%
Bar&7&\cite{angenendt}\\\bottomrule%
%
\end{tabular}%
\printbibliography%
\end{document}

\normalsize忽略開頭和結尾的愚蠢每一個與 a 一起%,需要解決的一個問題是刪除-in周圍的大括號\addbibresource。也就是說,應該是\addbibresource{biblatex-examples.bib}。 (我不知道為什麼要添加這些大括號,也不知道如何刪除它們。)

在 上運行通常的++ pdflatex,你會得到biberpdflatexex.tex

程式碼的輸出

答案2

我遇到過同樣的問題。只需使用 Torborn T. 的程式碼即可刪除括號「{」和「}」。鍵入此命令:

doc.preamble.append(pl.Command('addbibresource',arguments=[pl.NoEscape(r"biblatex-examples.bib")]))

代替:

doc.preamble.append(pl.Command('addbibresource',arguments=["biblatex-examples.bib"]))

相關內容