LaTeX Environment (latex
)¶
LaTeX environment to create and handle LaTeX documents
Associated Modules¶
pyTex - set up a LaTeX environment |
|
pyDocumentClass - set up document class |
|
pyPackages - settings for latex packages |
|
pyTitlePage - create title page |
|
pyBibtex - set up Bibtex |
Usage¶
To use this module class you need to import the PyTex class only. The other classes are imported within:
from latex import PyTex
The content of the LaTeX document is stored in different variables/objects:
Name |
Description |
Variable Name |
Type |
---|---|---|---|
Document class |
preamble |
preamble |
|
Packages |
used packages |
packages |
|
Optional settings |
optional settings |
optionalSettings |
list of strings |
Title |
title page |
titlepage |
|
Content |
complete content of text body |
content |
list of strings |
BibTex |
BibTex settings |
bibtex |
|
Preamble¶
The PyTex class allows the user to set default settings when initialized. At the moment settings for pavayo, validation, presentation and equation are provided:
report = PyTex(defaultSettings = "pavayo")
If none of the default setup is appropriate one can specify the document class and its settings:
report = PyTex(documentClass = "scrreprt", documentclassOptions = ["a4paper", "10pt"])
Presettings are applied if neither default settings or a document class is given.
There a further optional arguments in latex.pyTex.pyTex
.
The file name of the LaTeX document can be either set at the initialization or it can be specified later using:
report.setfileName(fileName = "myReport")
Packages¶
Additional packages can be inserted
report.packages.addPackage("color")
report.packages.addPackage("inputenc", options = "utf8x")
Title¶
The title can be added by using setTitle()
either using a user defined setup
report.setTitle(titleType = "user", content = "\\n".join(titleContentLines))
or the ‘maketitle’ environment
report.setTitle(titleType = "auto", title = "My Document", author = "John Doe", date = "04-06-2012")
Text body¶
There is a number of functions to add various text modules to the document body. The information is stored in the variable ‘content’ as a list of strings.
Warning
The order of the text modules is also used in the LaTeX document.
The following function are implemented to add something to the text body
addSectionTitle()
report.addSectionTitle("Testcases", chapterType = 0) report.addSectionTitle("Testcase %d: %s" %(caseno, TestCaseName), chapterType = 1)
addFileContent()
report.addFileContent('traceControl.h', sectionName = "Control settings", chapterType = 0)
addText()
report.addText("\\clearpage")
addVerbatim()
report.addVerbatim("\\n".join(tVerbatim))
addFile()
report.addFile("./introduction", newPage = False, exists = True)
addFigure()
report.addFigure("./result", caption = "Results", exists = True)
Bibtex¶
Create LaTeX document¶
The document data will written to file when calling writeLatexFile()
report.writeLatexFile()
report.compileLatexFile(cplType = "pdf", outputPath = self.localdir)
Example¶
To generate a small formula and store it as a transparent png image the following is done:
from latex import pyTex
dd = pyTex(fileName = "equation1", defaultSettings = "equation")
dd.addEquations(r"x = \frac{a}{b}", noNumbering = True)
dd.writeLatexFile()
dd.compileLatexFile(cplType = "png")
Modules¶
pyTex - set up a LaTeX environment¶
The pyTex class is designed to set up a LaTeX environment.
For document class (pyDocumentClass
),
packages (pyPackages
),
title page (pyTitlePage
),
and BibTeX (pyBibtex
) self-contained classes are defined.
required classes and methods:
from latex.pyDocumentClass import pyDocumentClass
from latex.pyPackages import pyPackages
from latex.pyTitlePage import pyTitlePage
from latex.pyBibtex import pyBibtex
-
class
PyTex
(fileName='', defaultSettings='', documentClass='scrreprt', documentclassOptions=None, packageSettings=None, toc=True, lof=False, lot=False, content=None, latexPath='latex', dvipngPath='dvipng', pdflatexPath='pdflatex', bibtexPath='bibtex', verbose=0)[source]¶ main class to handle a LaTeX object
- Parameters
fileName (string) – base name of latex project (usually without ‘.tex’ extension)
defaultSettings (string) – default settings for particular projects like ‘pavayo’: [equation’, ‘pavayo’, ‘validation’, ‘presentation’]
documentClass (string) – latex document class
documentclassOptions (list of string) – latex document class options
packageSettings (string) – list of packages (see pyPackages)
toc (boolean) – flag for table of contents
lof (boolean) – flag for list of figures
lot (boolean) – flag for list of tables
content (list of strings) – content of document
verbose (integer) – output level
latexPath (string) – path to latex executable
dvipngPath (string) – path to dvipng executable
pdflatexPath (string) – path to pdflatex executable
bibtexPath (string) – path to bibtex executable
verbose – verbosity level
-
addEquations
(text, number=1, label=None, noNumbering=False, appendix=False)[source]¶ add equation(s) to content
- parameters:
text - formula or list of formulas
- optional parameters:
number - number of equations
label - corresponding label
noNumbering - flag to indicate if labeling is wanted
appendix - flag to add this entry to the appendix instead of the main content
-
addFigure
(fileName, position='ht', width='\\textwidth', height='', furtherSettings='', caption='', label='', noNumbering=False, exists=True, appendix=False)[source]¶ add figure environment to content
- parameters:
fileName - filename of the graphics file
- optional parameters:
position - position of figure in document
width - width of figure
height - height of figure
caption - caption of table
label - corresponding label
noNumbering - flag to indicate if labeling is wanted
exists - flag if file exists
appendix - flag to add this entry to the appendix instead of the main content
-
addFile
(filename, newPage=False, exists=True, appendix=False)[source]¶ add file ‘filename’ to content
- parameters:
filename - name of file
- optional parameters:
newPage - flag whether ‘include’ or ‘input’ is used
exists - flag if file exists
appendix - flag to add this entry to the appendix instead of the main content
-
addFileContent
(filename, sectionName='', chapterType=0, fontsize=10, verbatim=True, appendix=False)[source]¶ add content of file ‘filename’ to content within a verbatim environment
- parameters:
filename - name of file
- optional parameters:
sectionName - title
chapterType - header level
fontsize - font size
verbatim - flag if verbatim environment should be used
appendix - flag to add this entry to the appendix instead of the main content
-
addItemize
(items, appendix=False)[source]¶ Add a itemize environment to the content.
- Parameters
items (iterable) – list of strings, containing the items
appendix (boolean) – flag to add this entry to the appendix instead of the main content
-
addLongtable
(array, **kwargs)[source]¶ add longtable environment to content
- Parameters
array (list of strings) – contents of table as a 2D-array(list)
nCols (string) – header number of columns (optional, default: 0)
setCols (string) – settings for columns (optional, default: “”)
appendix (boolean) – flag to add this entry to the appendix instead of the main content (optional, default: False)
headerSeparator (string) – separate first row of table from rest, e.g. by “hline” (optional, default: “”)
-
addMiniPage
(content, position='ht', width='\\textwidth', appendix=False)[source]¶ add mini page to content
- Parameters
content (list of strings) – contents of mini page
position (string) – position of table in document (optional, default: “ht”)
width (string) – width of mini page
appendix (boolean) – flag to add this entry to the appendix instead of the main content (optional, default: False)
-
addOptionalSettings
(text)[source]¶ add optional settings to latex document
- parameters:
text - text string
-
addSectionTitle
(title, chapterType=0, shortTitle='', label='', noNumbering=False, appendix=False)[source]¶ add section title to content
- Parameters
title (string) – title
chapterType (int) – header level (optional, default: “”)
shortTitle (string) – short tile (optional, default: “”)
label (string) – corresponding label (optional, default: “”)
noNumbering (boolean) – flag to indicate if labeling is wanted (optional, default: False)
appendix (boolean) – flag to add this entry to the appendix instead of the main content (optional, default: False)
-
addSubFigure
(fileList, captionList, position='ht', widthSubfigure='0.45\\textwidth', appendix=False)[source]¶ add subfigure to content
- Parameters
fileList (list of strings) – names of subfigures
captionList (list of strings) – names of desired captions - pay attention: that first entry is always the main caption for entire figure
position (string) – position of the figure in document
widthSubfigure (string) – defines the size of each subfigure
appendix (boolean) – flag to add this entry to the appendix instead of the main content (optional, default: False)
-
addTable
(array, position='ht', nCols=0, setCols='', caption='', label='', noNumbering=False, appendix=False)[source]¶ add table environment to content
- Parameters
array (list of strings) – contents of table as a 2D-array(list)
position (string) – position of table in document (optional, default: “ht”)
nCols (int) – header number of columns (optional, default: 0)
setCols (string) – settings for columns (optional, default: “”)
caption (string) – caption of table (optional, default: “”)
label (string) – corresponding label (optional, default: “”)
noNumbering (boolean) – flag to indicate if labeling is wanted (optional, default: False)
appendix (boolean) – flag to add this entry to the appendix instead of the main content (optional, default: False)
-
addTabular
(array, **kwargs)[source]¶ add tabular environment to content
- Parameters
array (list of strings) – contents of table as a 2D-array(list)
nCols (string) – header number of columns (optional, default: 0)
setCols (string) – settings for columns (optional, default: “”)
appendix (boolean) – flag to add this entry to the appendix instead of the main content (optional, default: False)
headerSeparator (string) – separate first row of table from rest, e.g. by “hline” (optional, default: “”)
-
addText
(text, appendix=False)[source]¶ add text to content
- parameters:
text - text string
- optional parameters:
appendix - flag to add this entry to the appendix instead of the main content
-
addVerbatim
(text, options=None, appendix=False)[source]¶ add verbatim environment to content
- parameters:
text - text string
- optional parameters:
appendix - flag to add this entry to the appendix instead of the main content
-
addWatermark
(text='DRAFT', color='red', translucency=50)[source]¶ Add text as watermark in specified color and translucency
- optional parameters:
text - text
color - color (xcolor package is used)
translucency - translucency in per cent
-
compileLatexFile
(cplType='pdf', msgfileName='latexmsgs', outputPath='')[source]¶ compile latex project
- optional parameters:
cplType - compile type: [‘pdf’, ‘png’, ‘ps’, ‘pspdf’]
msgfileName - name of message file
outputPath - place a copy of the output file at this path
- return:
return code from subprocess call
-
static
escapeChars
(text, encode=None)[source]¶ Escapes characters, which would otherwise not printed correctly in TeX.
-
setBibtex
(sources, style='plain')[source]¶ set bibtex settings using class pyBibtex
- Parameters
sources (string) – list of used bibtex files
style (string) – bibtex style
-
setTitle
(titleType='auto', content='', title='', author='', date='')[source]¶ create title page
- Parameters
titleType (string) – type of title page, options: ‘user’, ‘auto’
content (string) – content of title page (used if titleType = ‘user’)
title (string) – title of document (used if titleType = ‘auto’)
author (string) – author of document (used if titleType = ‘auto’)
date (string) – date of document (used if titleType = ‘auto’)