pylatex.utils

This module implements some simple utility functions.

class NoEscape[source]

Bases: str

A simple string class that is not escaped.

When a NoEscape string is added to another NoEscape string it will produce a NoEscape string. If it is added to normal string it will produce a normal string.

Parameters:string (str) – The content of the NoEscape string.
escape_latex(s)[source]

Escape characters that are special in latex.

Parameters:s (str, NoEscape or anything that can be converted to string) – The string to be escaped. If this is not a string, it will be converted to a string using str. If it is a NoEscape string, it will pass through unchanged.
Returns:The string, with special characters in latex escaped.
Return type:NoEscape

Examples

>>> escape_latex("Total cost: $30,000")
'Total cost: \$30,000'
>>> escape_latex("Issue #5 occurs in 30% of all cases")
'Issue \#5 occurs in 30\% of all cases'
>>> print(escape_latex("Total cost: $30,000"))

References

fix_filename(path)[source]

Fix filenames for use in LaTeX.

Latex has problems if there are one or more points in the filename, thus ‘abc.def.jpg’ will be changed to ‘{abc.def}.jpg’

Parameters:filename (str) – The filen name to be changed.
Returns:The new filename.
Return type:str

Examples

>>> fix_filename("foo.bar.pdf")
'{foo.bar}.pdf'
>>> fix_filename("/etc/local/foo.bar.pdf")
'/etc/local/{foo.bar}.pdf'
>>> fix_filename("/etc/local/foo.bar.baz/document.pdf")
'/etc/local/foo.bar.baz/document.pdf'
>>> fix_filename("/etc/local/foo.bar.baz/foo~1/document.pdf")
'\detokenize{/etc/local/foo.bar.baz/foo~1/document.pdf}'
dumps_list(l, *, escape=True, token='%\n', mapper=None, as_content=True)[source]

Try to generate a LaTeX string of a list that can contain anything.

Parameters:
  • l (list) – A list of objects to be converted into a single string.
  • escape (bool) – Whether to escape special LaTeX characters in converted text.
  • token (str) – The token (default is a newline) to separate objects in the list.
  • mapper (callable or list) – A function, class or a list of functions/classes that should be called on all entries of the list after converting them to a string, for instance bold or MediumText.
  • as_content (bool) – Indicates whether the items in the list should be dumped using dumps_as_content
Returns:

A single LaTeX string.

Return type:

NoEscape

Examples

>>> dumps_list([r"\textbf{Test}", r"\nth{4}"])
'\\textbf{Test}%\n\\nth{4}'
>>> print(dumps_list([r"\textbf{Test}", r"\nth{4}"]))
\textbf{Test}
\nth{4}
>>> print(pylatex.utils.dumps_list(["There are", 4, "lights!"]))
There are
4
lights!
>>> print(dumps_list(["$100%", "True"], escape=True))
\$100\%
True
bold(s, *, escape=True)[source]

Make a string appear bold in LaTeX formatting.

bold() wraps a given string in the LaTeX command textbf{}.

Parameters:
  • s (str) – The string to be formatted.
  • escape (bool) – If true the bold text will be escaped
Returns:

The formatted string.

Return type:

NoEscape

Examples

>>> bold("hello")
'\\textbf{hello}'
>>> print(bold("hello"))
\textbf{hello}
italic(s, *, escape=True)[source]

Make a string appear italicized in LaTeX formatting.

italic() wraps a given string in the LaTeX command textit{}.

Parameters:
  • s (str) – The string to be formatted.
  • escape (bool) – If true the italic text will be escaped
Returns:

The formatted string.

Return type:

NoEscape

Examples

>>> italic("hello")
'\\textit{hello}'
>>> print(italic("hello"))
\textit{hello}
verbatim(s, *, delimiter='|')[source]

Make the string verbatim.

Wraps the given string in a verb LaTeX command.

Parameters:
  • s (str) – The string to be formatted.
  • delimiter (str) – How to designate the verbatim text (default is a pipe | )
Returns:

The formatted string.

Return type:

NoEscape

Examples

>>> verbatim(r"\renewcommand{}")
'\\verb|\\renewcommand{}|'
>>> print(verbatim(r"\renewcommand{}"))
\verb|\renewcommand{}|
>>> print(verbatim('pi|pe', '!'))
\verb!pi|pe!
make_temp_dir()[source]

Create a temporary directory if it doesn’t exist.

Directories created by this functionn follow the format specified by _tmp_path and are a pylatex subdirectory within a standard tempfile tempdir.

Returns:The absolute filepath to the created temporary directory.
Return type:str

Examples

>>> make_temp_dir()
'/var/folders/g9/ct5f3_r52c37rbls5_9nc_qc0000gn/T/pylatex'
rm_temp_dir()[source]

Remove the temporary directory specified in _tmp_path.