'Python - how to convert .txt to LaTeX?
I am currently performing multiple transformations from a .txt file to LaTeX using the following code:
from pylatex import Document, PageStyle, LineBreak, Section, Subsection, Math, Alignat, LargeText, Center, MediumText
from pylatex.utils import bold, italic, NoEscape
import os as os
class Math_LaTeX ():
def __init__(self, doc):
#---Inicializamos---
self.doc = doc
#---De aqui sacamos las ecuaciones del archivo .txt---
equations = self.Read()
#---Creamos el documento Latex---
self.Create(equations)
#---Creamos el PDF---
self.doc.generate_pdf(filepath = TituloPDF, clean_tex = False, compiler = 'pdflatex')
def Read(self):
data = open(archivotxt, 'rt')
equations = data.readlines()
eqs = []
for eq in equations:
eqs.append(' '.join([line.strip() for line in eq.strip().splitlines()]))
return eqs
def Create(self, equations):
for eq in equations:
with self.doc.create(Alignat(numbering = False, escape = False)) as math_eq:
math_eq.append(eq)
geometry_options = {
"head": "0.5cm",
"left": "0.5cm",
"right": "0.5cm",
"bottom": "2.5cm"
}
doc = Document(geometry_options = geometry_options, inputenc = 'utf8')
Math_LaTeX(doc)
if os.path.exists(archivotxt):
os.remove(archivotxt)
else:
print("El archivo no existe")
This is what i get:
\documentclass{article}%
\usepackage[T1]{fontenc}%
\usepackage[utf8]{inputenc}%
\usepackage{lmodern}%
\usepackage{textcomp}%
\usepackage{lastpage}%
\usepackage{geometry}%
\geometry{head=0.5cm,left=0.5cm,right=0.5cm,bottom=2.5cm}%
\usepackage{amsmath}%
%
%
%
\begin{document}%
\normalsize%
\begin{alignat*}{2}%
{\left( 3 \right) {x_{1}} +\left( -0.1 \right) {x_{2}} +\left( -0.2 \right) {x_{3}} = {7.85} }%
\end{alignat*}%
\begin{alignat*}{2}%
{\left( 0.1 \right) {x_{1}} +\left( 7 \right) {x_{2}} +\left( -0.3 \right) {x_{3}} = {-19.3} }%
\end{alignat*}%
\begin{alignat*}{2}%
{\left( 0.3 \right) {x_{1}} +\left( -0.2 \right) {x_{2}} +\left( 10 \right) {x_{3}} = {71.4} }%
\end{alignat*}%
\text{ -------------------------------------------------------------------------- }%
\end{alignat*}%
\begin{alignat*}{2}%
\text{Entonces la solución estaría dada por:}%
\end{alignat*}%
\begin{alignat*}{2}%
X = \begin{bmatrix} x_{1} \\ x_{2} \\ x_{3} \end{bmatrix} = \begin{bmatrix} 3.0 \\ -2.5 \\ 7.0 \end{bmatrix}%
\end{alignat*}%
\end{document}
However, I cannot customize various aesthetic aspects of the final PDF, so I decided to create a text file with all the specifications through Python.
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[spanish]{babel}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{textcomp}
\usepackage{lastpage}
\usepackage{geometry}
\geometry{head=2.5cm,left=2.5cm,right=2.5cm,bottom=2.5cm}
\usepackage{amsmath}
\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead[]{Ejercicio 01}
\begin{document}
$ {\left( 3 \right) {x_{1}} +\left( -0.1 \right) {x_{2}} +\left( -0.2 \right) {x_{3}} = {7.85} } $
$ {\left( 0.1 \right) {x_{1}} +\left( 7 \right) {x_{2}} +\left( -0.3 \right) {x_{3}} = {-19.3} } $
$ {\left( 0.3 \right) {x_{1}} +\left( -0.2 \right) {x_{2}} +\left( 10 \right) {x_{3}} = {71.4} } $
\newpage
\lhead[]{Ejercicio 02}
$ k = {2},x^{\left( {2} \right)}={\begin{bmatrix} 2.61667 \\ -2.79452 \\ 7.00561 \end{bmatrix} }$%
$ \text{ -------------------------------------------------------------------------- } $%
\end{document}
This writes it to the .txt file without any problem, but what I need is for this new txt file to be transformed into a PDF using a LaTeX compiler, all from Python as above and that's what I don't know how to do.
I would greatly appreciate your help
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
