如何向 PyLaTeX 表格添加标题

如何向 PyLaTeX 表格添加标题

我想Tabular在 PyLaTeX 中为表格添加标题,但我不知道该怎么做。似乎可以使用表类但所有的例子都使用表格元素

基本上我想要类似以下的东西:

from pylatex import Document, Section, Subsection, Tabular, MultiColumn,\
    MultiRow

doc = Document("multirow")
section = Section('Multirow Test')

test1 = Subsection('MultiColumn')

table1 = Tabular('|c|c|c|c|')
table1.add_hline()
table1.add_row((1, 2, 3, 4))
table1.add_hline()
**table1.add_caption("My Table Caption")**

test1.append(table1)
section.append(test1)


doc.append(section)
doc.generate_pdf(clean_tex=False)

答案1

pylatex库提供了一个Table()类,如问题中所述。此类生成 LaTeXtable环境,例如:

table1 = Table(position="htb")

生成

\begin{table}[htb]%
\end{table}

在 PyLaTeX 中,可以将环境tabular附加到对象Table(),类似于将子节附加到节的方式。反过来,可以将表附加到子节。例如:

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)

该类具有添加标题的Table()功能。如果您还想添加标签,则需要从该类中单独获取一个对象并将其附加到表对象。可以将其他对象或代码附加到表中,例如将表格置于页面中间的命令。add_caption()Label()\centering

梅威瑟:

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)

生成的 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}

结果:

在此处输入图片描述

答案2

好吧,Marijn 抢先了一步。无论如何,我只想指出,您可以将表格嵌套在表格中(其他结构也是如此)。这样,您就可以获得更好的结构化代码,而不必始终明确声明变量:

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)

正如您在下面的示例中看到的,您不需要发明新的变量名(tabular1,tabular2 等),并且您不必.append在最后做所有事情:

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)

在此处输入图片描述

相关内容