2

I am interested in using PyLatex to automatize a table with several entries in such a way that I don't need to go through it manually every time that I need to change or add an entry. In the last column of this table, the reference from where I took the entry is needed.

So far, I haven't found any example of how to use PyLatex with Bibliography stiles, may you know an example of how to do it?

1

Nothing special needed really, just a matter of adding the appropriate package etc. to the preamble, and \printbibliography (or \bibliography{<filename>}) in the document. The code is short and, I suppose, fairly self-explanatory.

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')

That Python script will generate this code in 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}

Disregarding the sillyness of starting with \normalsize and ending every line with a %, the one problem that needs to be fixed is to remove the braces around the - in \addbibresource. That is, it should be \addbibresource{biblatex-examples.bib}. (I don't know why those braces are added, nor how to remove them.)

Running the usual pdflatex + biber + pdflatex on ex.tex, and you get

output of code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.