pythonlatexequationpylatex

Latex pylatex Math Equation: I want to add equations to a table using python. eg Summation


My goal is to create a dynamic table, where I'll get values from my Database and using iterations create multiple rows in the table. I currently don't know if it is possible using 'pylatex'. My concern now is to create a Math equation in my pdf using python.

Following you can get a glimpse of What i am trying to do.

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

Expectations:

enter image description here

I have also tried "pythontex" in latex, where creating equations is easy using \sum_(a+b), for summation, but creating dynamic tables seems difficult or impossible.

Looking forward for your suggestions. :)


Solution

  • A trick I quickly learned when working with pylatex is using NoEscape to embed raw LaTeX into the document, when pylatex comes short:

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

    Here I use Displayed equations, you could use inline formulas as well with single $.