2

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.stack.imgur.com/CPC6d.png

1 Answers1

1

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}$')))
# ...
jf_
  • 3,099
  • 2
  • 13
  • 31
  • It does not work, I get this error: ```subprocess.CalledProcessError: Command '['latexmk', '--pdf', '--interaction=nonstopmode ... returned non-zero exit status 12.```. Do you know what is happening ? – michael heredia perez Apr 30 '21 at 15:50
  • Is there any other error message before that? – jf_ Apr 30 '21 at 15:53
  • Oh I see. I just answered the part with the gamma command. It also needs to be put in math mode. I updated the answer. – jf_ Apr 30 '21 at 15:57