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 ),

Pylatex module in Python __del__: Questions

How can I make a time delay in Python?

5 answers

I would like to know how to put a time delay in a Python script.

2973

Answer #1

import time
time.sleep(5)   # Delays for 5 seconds. You can also use a float value.

Here is another example where something is run approximately once a minute:

import time
while True:
    print("This prints once a minute.")
    time.sleep(60) # Delay for 1 minute (60 seconds).

2973

Answer #2

You can use the sleep() function in the time module. It can take a float argument for sub-second resolution.

from time import sleep
sleep(0.1) # Time in seconds

Pylatex module in Python __del__: Questions

How to delete a file or folder in Python?

5 answers

How do I delete a file or folder in Python?

2639

Answer #1


Path objects from the Python 3.4+ pathlib module also expose these instance methods:

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically