pythonlatexpylatex

How do I compile a greek letter in PyLaTeX into a table?


I am trying to do a code to create a LaTex table from Python implementing PyLaTeX. Two cells of the table I want them to be greek letters, like [what is shown in the image][1]. Look at my block of code and see that I am typing '\gamma' and '\gamma_{sat}' but it does not work, just print it textually, does anyone know how can I solve this problem?

I have tried typing '\\gamma' , r'\gamma', '$\gamma'.... but I just don't know how to do it.

# Creo una subsección para las tablas. 
    with doc.create(Subsection('Tablas de interés', numbering = False)):
        # Creo el entorno Table.
        with doc.create(Table(position='h!')) as entorno_table:
            # Creo el entorno Center.
            with entorno_table.create(Center()) as centered:
                # Creo el entorno Tabular.
                with centered.create(Tabular('|c|c|c|c|c|')) as entorno_tabular:
                    # Construyo la tabla.
                    entorno_tabular.add_hline()
                    entorno_tabular.add_row(('Estrato', 'Profundidad Inicial', 'Profundidad final', '\gamma', '\gamma_{sat}'))
                    entorno_tabular.add_hline()
                    entorno_tabular.add_row(('1', '0', '1', '14.6', '18.38'))
                    entorno_tabular.add_hline()
                    entorno_tabular.add_row(('2', '1', '2.5', '14.9', '18.48'))
                    entorno_tabular.add_hline()
                    entorno_tabular.add_row(('3', '2.5', '5', '15.2', '18.58'))
                    entorno_tabular.add_hline()
                    entorno_tabular.add_row(('4', '5', '6.7', '15.5', '18.68'))
                    entorno_tabular.add_hline()
                    entorno_tabular.add_row(('5', '6.7', '10', '15.8', '18.78'))
                    entorno_tabular.add_hline()
                    #table.add_empty_row()
            # Añado el caption al entorno Table.
            entorno_table.add_caption('Propiedades del suelo para pregunta ##.')


  [1]: https://i.sstatic.net/CPC6d.png

Solution

  • Use NoEscape to get the Latex commands out into the document. You also need to put the gammas in math mode $.. $.

    from pylatex import Document, Subsection, Table, Tabular, Center, NoEscape # ...
    
    # ...
        entorno_tabular.add_row(('Estrato', 'Profundidad Inicial', 'Profundidad final', NoEscape('$\gamma$'), NoEscape('$\gamma_{sat}$')))
    # ...