python-3.xlatexpylatex

pylatex tex file generation in NoEscape container


i want to create PDF`s via Python 3.7.9 and Pylatex 1.4.1. I dont know if i encountered a bug.

I try to generate the following formula in the NoEscape container from Pylatex:

from pylatex import Document, Section, Math
from pylatex.utils import NoEscape

doc = Document('basic')
section = Section("Section1")

math = NoEscape("$g(10)=1\times 3\times 7\times 9 = 189$")

section.append(math)

doc.append(section)

doc.generate_pdf(clean_tex=False)

The first output from Pylatex is a .tex file. Pylatex generates the following code with this code snippet:

\documentclass{article}%
\usepackage[T1]{fontenc}%
\usepackage[utf8]{inputenc}%
\usepackage{lmodern}%
\usepackage{textcomp}%
\usepackage{lastpage}%
%
%
%
\begin{document}%
\normalsize%
\section{Section1}%
\label{sec:Section1}%
$g(10)=1    imes 3  imes 7  imes 9 = 189$

%
\end{document}

The \times is allways just imes.

The correct output would be:

$g(10)=1\times 3\times 7\times 9 = 189$

Anyone know a solution for this?

Thanks in advance.


Solution

  • the \ is an escape character, in fact \t is the tab.

    this applies to strings in python:

    # \ on its own is an escape character.
    s = "$g(10)=1\times 3\times 7\times 9 = 189$"
    print(s)
    
    # \\ this will work
    s = "$g(10)=1\\times 3\\times 7\\times 9 = 189$"
    print(s)
    

    result:

    $g(10)=1    imes 3  imes 7  imes 9 = 189$
    $g(10)=1\times 3\times 7\times 9 = 189$