问题描述
我的目标是创建一个动态表,我将从我的数据库中获取值并使用迭代在表中创建多行。我目前不知道是否可以使用“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()
期望:
我也尝试过在 Latex 中使用“pythontex”,其中使用 \sum_(a+b) 创建方程很容易,用于求和,但创建动态表似乎很困难或不可能。
期待您的建议。 :)
解决方法
我在使用 pylatex 时很快学会的一个技巧是使用 NoEscape 将原始 LaTeX 嵌入到文档中,当 pylatex 不足时:
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()
这里我使用显示方程,您也可以使用带有单个 $ 的内联公式。