Latex pylatex 数学方程:我想使用 python 将方程添加到表格中。例如求和

问题描述 投票:0回答:2

我的目标是创建一个动态表,我将从数据库中获取值,并使用迭代在表中创建多行。我目前不知道是否可以使用“pylatex”。我现在关心的是使用 python 在我的 pdf 中创建一个数学方程。

接下来您可以了解我正在尝试做什么。

from pylatex import Document, Section, Subsection, Tabular
from pylatex import Math, TikZ, Axis, Plot, Figure, Matrix, Alignat
from pylatex.utils import italic, bold
...
...
with doc.create(Subsection('Math Equations')):
        doc.append('Attempt to create dynamic table i.e creating dynamic rows depending on the number of rows in Database, and displaying those values in PDF Table. \n \n')
        with doc.create(Tabular('|p{3cm}|p{7cm}|p{3cm}|')) as table:
            table.add_hline()
            table.add_row((bold('ID'), bold('Equation'), bold('Result')))
            table.add_hline()
            table.add_row('1',Math(inline=False, data="\sum(a+b)", escape=None),'Result')
            table.add_hline()
            for x in range(0, 3):
                table.add_row((bold(x), bold(x+1), bold(x+2)))
                table.add_hline()

enter image description here

期望:

enter image description here

我还在乳胶中尝试过“pythontex”,其中使用 \sum_(a+b) 进行求和创建方程很容易,但创建动态表似乎很困难或不可能。

期待您的建议。 :)

python latex equation pylatex
2个回答
2
投票

我在使用 pylatex 时很快学会的一个技巧是,当 pylatex 不足时,使用 NoEscape 将原始 LaTeX 嵌入到文档中:

from pylatex import Document, Section, Subsection, Tabular, NoEscape
from pylatex import Math, TikZ, Axis, Plot, Figure, Matrix, Alignat
from pylatex.utils import italic, bold
...
...
with doc.create(Subsection('Math Equations')):
    doc.append('Attempt to create dynamic table i.e creating dynamic rows depending on the number of rows in Database, and displaying those values in PDF Table. \n \n')
    with doc.create(Tabular('|p{3cm}|p{7cm}|p{3cm}|')) as table:
        table.add_hline()
        table.add_row((bold('ID'), bold('Equation'), bold('Result')))
        table.add_hline()
        table.add_row('1', NoEscape(r"$$\sum(a+b)$$"), 'Result')
        table.add_hline()
        for x in range(0, 3):
            table.add_row((bold(x), bold(x+1), bold(x+2)))
            table.add_hline()

这里我使用显示方程,您也可以使用内联公式以及单个 $。


0
投票

Math
有类有
escape=False
,使用
data=(r"...")
它可以工作:

from pylatex import Document, Section, Subsection, Tabular
from pylatex import Math, TikZ, Axis, Plot, Figure, Matrix, Alignat
from pylatex.utils import italic, bold

doc=Document()
with doc.create(Subsection('Math Equations')):
        doc.append('Attempt to create dynamic table i.e creating dynamic rows depending on the number of rows in Database, and displaying those values in PDF Table. \n \n')
        with doc.create(Tabular('|p{3cm}|p{7cm}|p{3cm}|')) as table:
            table.add_hline()
            table.add_row((bold('ID'), bold('Equation'), bold('Result')))
            table.add_hline()
            table.add_row('1',Math(inline=False, data=(r"\sum(a+b)"), escape=False),'Result')
            table.add_hline()
            for x in range(0, 3):
                table.add_row((bold(x), bold(x+1), bold(x+2)))
                table.add_hline()

# Generate the .pdf file
doc.generate_pdf('math_test', clean_tex=False)

enter image description here

附注= LaTeX 纯粹主义者不会同意将一些显示数学放在表格中,更好地内联,稍微增大行高。
还应避免垂直规则。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.