How to add a caption to a PyLaTeX Tabular table
The pylatex
library provides a Table()
class as was noted in the question. This class generates the LaTeX table
environment, for example:
table1 = Table(position="htb")
generates
\begin{table}[htb]%
\end{table}
The tabular
environment can be appended to the Table()
object in PyLaTeX, similar to the way a subsection is appended to a section. In turn the table can be appended to a subsection. For example:
test1 = Subsection('MultiColumn')
table1 = Table(position="htb")
tabular1 = Tabular('|c|c|c|c|')
# fill tabular
table1.append(tabular1)
table1.add_caption("My Table Caption")
test1.append(table1)
The Table()
class has a function add_caption()
to add a caption. If you also want to add a label then you need a separate object from the class Label()
and append this to the table object. Further objects or code can be appended to the table, for example a \centering
command to center the tabular in the middle of the page.
MWE:
from pylatex import Document, Section, Subsection, \
Table, Tabular, MultiColumn, MultiRow, Label, Ref, NoEscape
doc = Document("multirow")
section = Section('Multirow Test')
test1 = Subsection('MultiColumn')
# make table
table1 = Table(position="htb")
# make tabular
tabular1 = Tabular('|c|c|c|c|')
tabular1.add_hline()
tabular1.add_row((1, 2, 3, 4))
tabular1.add_hline()
# horizontally center tabular on page
table1.append(NoEscape(r'\centering'))
# append tabular to table
table1.append(tabular1)
# add caption and label
table1.add_caption("My Table Caption")
label1 = Label("tab1")
table1.append(label1)
# append table to subsection
test1.append(table1)
section.append(test1)
# create and print reference to table
ref1 = Ref("tab1")
section.append("The numbers are in Table ")
section.append(ref1)
section.append(".")
doc.append(section)
doc.generate_pdf(clean_tex=False)
Generated LaTeX:
\documentclass{article}%
\usepackage[T1]{fontenc}%
\usepackage[utf8]{inputenc}%
\usepackage{lmodern}%
\usepackage{textcomp}%
\usepackage{lastpage}%
\begin{document}%
\normalsize%
\section{Multirow Test}%
\label{sec:Multirow Test}%
\subsection{MultiColumn}%
\label{subsec:MultiColumn}%
\begin{table}[htb]%
\centering%
\begin{tabular}{|c|c|c|c|}%
\hline%
1&2&3&4\\%
\hline%
\end{tabular}%
\caption{My Table Caption}%
\label{tab1}%
\end{table}
The numbers are in Table %
\ref{tab1}%
.
\end{document}
Result:
Well, Marijn beat me to it. Anyway, I just wanted to point out, that you can nest the tabulars inside of the tables (and the other structures too). That way, you get better structured code and don't need to declare variables explicitly all the time:
from pylatex import Document, Section, Subsection, Tabular, Table, MultiColumn, MultiRow
doc = Document('multirow')
with doc.create(Section('Multirow Test')):
with doc.create(Subsection('MultiColumn')):
with doc.create(Table(position='h!')) as table:
with doc.create(Tabular('|c|c|c|c|')) as tabular:
tabular.add_hline()
tabular.add_row((1, 2, 3, 4))
tabular.add_hline()
table.add_caption('My Table Caption')
doc.generate_pdf(clean_tex=False)
As you can see in the example below, you don't need to invent new variable names (tabular1, tabular2, etc.) and you don't have to .append
everything in the end:
from pylatex import Document, Section, Subsection, Tabular, Table, MultiColumn, MultiRow
doc = Document('multirow')
with doc.create(Section('Multirow Test')):
with doc.create(Subsection('MultiColumn')):
with doc.create(Table(position='h!')) as table:
with doc.create(Tabular('|c|c|c|c|')) as tabular:
tabular.add_hline()
tabular.add_row((1, 2, 3, 4))
tabular.add_hline()
table.add_caption('My Table Caption')
with doc.create(Subsection('MultiColumn 2')):
with doc.create(Table(position='h!')) as table:
with doc.create(Tabular('|c|c|c|c|')) as tabular:
tabular.add_hline()
tabular.add_row((5, 6, 7, 8))
tabular.add_hline()
table.add_caption('My Table Caption 2')
doc.generate_pdf(clean_tex=False)