Pylatex module in Python



Latex called “Lay-tech” is a documentation system for high quality documentation. It is mainly used for the preparation of technical or scientific papers, but can be used for almost all forms of publication. Latex is not a word processor like MS Word or LibreOffice Writer. Instead, Latex encourages authors not to worry about the appearance of their documents, but to focus on getting the right content. For example, consider the following document:

 This article explains the use of pylatex module Python.Engineering October 2018 

To create this in most word processors, the author would have to decide which layout to use, so would choose (say) 18pt Helvetica for the title, 12pt Times Roman for the name, and so on. As a result, the author spends time developing the document. Latex is based on the idea that it allows authors to start writing a document and leave it to the document designers to develop it. So in Latex you could enter the above document as:

 documentclass {article} itle {This article explains use of pylatex module} author {GeeksforGeeks} date {October 2018} egin {document} maketitle Continue reading end {document} 

Latex document layout:
There are two main parts of a latex document:
Preamble :

  • Preamble — this is the first part of the latex file.
  • It contains detailed information about the document such as document class, author name, title, etc.

Body:

  • Sections, tables, mathematical equations, graphs, etc. may be included in the body of a latex document.
  • All document content is inside & # 39 ; / begin {document} & # 39; and & # 39; / end {document} & # 39;

Some features of latex:

  1. Preparation of journal articles, technical reports, technical or non-technical books, and slide presentations.
  2. This provides better control over large documents containing sections, links, tables, and figures.
  3. This can also be useful to prepare documents containing complex mathematical formulas.
  4. The generation of bibliographies and indexes is automatic in LaTeX.
  5. It also provides multilingual typing support.
  6. To latex document we can also add graphics, illustration and process or spot color.
  7. PostScript or metaphone fonts are also possible in LaTeX.

Sample LaTeX document:
Example 1: In this example we are shaping a plain latex so that from latex we use the simple input format we used in latex.

documentclass {article} %

usepackage [T1] {fontenc} %

usepackage [utf8] {inputenc} %

usepackage {lmodern} %

usepackage {textcomp} %

usepackage {lastpage} %

usepackage [tmargin = 1cm, lmargin = 10cm] {geometry} %

usepackage {amsm ath} %

usepackage {tikz} %

usepackage {pgfplots} %

pgfplotsset {compat = newest} %

usepackage {graphicx} %

%
%
%

egin {document} %

ormalsize %

section {The regular stuff} %

label {sec: The regular stu ff} %

Some text and some %

extit {italic text. } %

ewline %

Also some crazy symbols: $ & amp; # { } %

subsection {Incorrect math} %

label {subsec: Incorrect math} %

[ %

2 * 3 = 22 %

]

  
%

end {document}

Output:

Example 2: In this example, we used the subsection label to form latex.

documentclass {article} %

usepackage [T1] {fontenc} %

usepackage [utf8] {inputenc} %

usepackage {lmodern} %

usepackage {textcomp} %

usepackage {lastpage} %

usepackage [tmargin = 1cm, lmargin = 10cm] {geometry} %

usepackage {amsmath} %

usepackage {tikz} %

usepackage {pgfplots} %

pgfplotsset {compat = newest} %

usepackage {graphicx} %

%
%
%
%

subsection {Table} %

label {subsec: Table} %

egin {tabular} {rc | cl} %

hline %

a & amp; b & amp; c & amp; d %

cline { 1 %

- %

2} %

& amp; & amp; & amp; %

e & amp; f & amp; g & amp; 7h %

end {tabular}

 
%

section {Special features} %

label {sec: Special features} %

subsection {Correct matrix equations} %

label {subsec : Correct matrix equations} %

[ %

egin {pmatrix} %

1 & amp; 4 & amp; 4 %

2 & amp; 3 & amp; 4 %

2 & amp; 2 & amp; 5 %

end {pmatrix} egin {pmatrix} %

800 %

30 %

30 %

end {pmatrix } = egin {pmatrix} %

810 %

60 %

50 %

end {pmatrix} %

]

 
%

end {document}

Output:

What is Pylatex:
PyLaTeX — it is a Python library for creating and compiling latex documents. The goal of this library is to be simple, but also to provide an extensible interface between Python and latex.

Some features of pylatex:

  • We can access all LaTeX features in Python using this module
  • We can create documents with fewer lines of code
  • Since python is a high-level language, it is easier to write code for pylatex in python compared to LaTeX
  • In the above LaTeX code, you must have seen that in order to get equations we need to compute the values ​​and then enter them into the LaTeX document, but with the added python functionality to do arithmetic operations it is much easier to prepare the documents

Create a Pylatex document:

  • Install MikTeX and the pylatex module on your system and import it into Python code.
    To install MikTeX on your system, follow the link:
     https://miktex.org/download 

    To install pylatex on a Windows operating system, enter the following command at the command line:

     python -m pip install pylatex 
  • Create a document to import a document class from the pylatex module. There are different types of documents in latex: article, report, letter, etc. To create a document of type article, create an object of the Document class from latex and pass & # 39; article & # 39;
     doc = Document (documentclass = `article`) 
  • To add the necessary changes to the document, such as styling or formatting, import the classes required in the python code from pylatex. To add various utilities to a latex document using pylatex, the following way is possible
     from pylatex import Document, Section, Subsection from pylatex.utils import italic, bold 
  • To generate a PDF document file, call the generate_pdf method of the Document class using an object of the Document class, and be sure to pass the pdf document name in its argument this way.
     doc.generate_pdf ("Demo_article") 

Pylatex example:
Code 1:

# Python program generating
# small document using pylatex

 

import numpy as np

 
# import from mod ulya pylatex

from pylatex import Document, Section, Subsection, Tabular

from pylatex import Math, TikZ, Axis, Plot, Figure, Matrix, Alignat

from pylatex.utils import italic

import os

 

if __ name__ = = `__main__` :

image_filename = os. path.join (os.path.dirname (__ file__), `kitten.jpg` )

 

geometry_options = { "tmargin" : "1cm" , "lmargin" : "10cm" }

  doc = Document (geometry_options = geometry_options)

 

# create a PDF titled "simple stuff"

  with doc.create (Section ( ` The simple stuff` )):

  doc.append ( ` Some regular text and some` )

doc.append (italic ( `italic text. ` ))

  doc.append ( `Also some crazy characters: $ {}` )

with doc.create (Subsection ( `Math that is incorrect` )):

doc.append (Math (data = [ `2 * 3` , ` = ` , 9 ]))

 

# create pdf subsection

with doc. create (Subsection ( `Ta ble of something` )):

with doc.create (Tabular ( `rc | cl` )) as table:

table.add_hline ()

table.add_row (( 1 , 2 , 3 , 4 ))

table.add_hline ( 1 , 2 )

table.add_empty_row ()

t able.add_row (( 4 , 5 , 6 , 7 ))

 

# create PDF using .generate_pdf

doc.generate_pdf ( `full` , clean_tex = False )

Output:

Code 2:

import numpy as np

 

from pylatex import Document, Section, Subsection, Tabular

from pylatex import Math, TikZ, Axis, Plot, Figure, Matrix, Alignat

from pylatex.utils import italic

import os

 

if __ name__ = = `__main__` :

image_filename = os. path.join (os.path.dirname (__ file__), `kitten.jpg` )

 

geometry_options = { "tmargin" : "1cm" , "lmargin" : "10c m " }

  doc = Document (geometry_options = geometry_options)

 

# create a matrix using the numpy module

a = np.array ([[ 100 , 10 , 20 ]]). T

  M = np.matrix ([[ 2 , 3 , 4 ],

[ 0 , 0 , 1 ],

[ 0 , 0 , 2 ]])

 

# create a heading using fashion

with doc.create (Section ( `The fancy stuff` )):

with doc.create (Subsection ( `Correct matrix equations` )):

doc.append (Math (data = [Matrix (M), Matrix (a), `=` , Matrix (M * a)]))

 

# create pdf subsection

with doc.create (Subsection ( ` Alignat math environment` )):

with doc.create (Alignat (numbering = False , escape = False )) as agn:

agn.append (r `frac {a} {b} & amp; = 0 ` )

agn.extend ([Matrix (M), Matrix (a), `& amp; =` , Matrix (M * a)])

 

with doc.create (Subsection ( `Beautiful graphs` )):

with doc.create (TikZ ()):

plot_options = ` height = 4cm, width = 6cm, grid = major`

  with doc.create (Axis (options = plot_options )) as plot:

plot.append (Plot (name = `model` , func = `-x ^ 5 - 242` ))

  

  coordinates = [

( - 4.77778 , 2027.60977 ),

( - 3 .55556 , 347.84069 ),

( - 2.33333 , 22.58953 ),

( - 1.11111 , - 493.50066 ),

( 0.11111 , 46.66082 ),

( 1.33333 , - 205.5628 6 ),

( 2.55556 , - 341.40638 ),

( 3.77778 , - 1169.24780 ),

( 5.00000 , - 3269.56775 ),

]

 

plot.append (Plot (name = ` estimate` , coordinates = coordinates))

 

with doc.create (Subsection ( `Cute kitten pictures` )):

with doc.create (Figure (position = `h!` )) as kitten_pic:

kitten_pic.add_image (image_filename, width = `120px` )

( 2.55556 , - 341.40638 ),