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 anotherNoEscape
string it will produce aNoEscape
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 usingstr
. If it is aNoEscape
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’
Windows gets angry about the curly braces that resolve the above issue on linux Latex distributions. MikTeX however, has no qualms about multiple dots in the filename so the behavior is different for posix vs nt when the length of file_parts is greater than two.
Parameters: filename (str) – The file 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 instancebold
orMediumText
. - as_content (bool) – Indicates whether the items in the list should be dumped using
dumps_as_content
Returns: A single LaTeX string.
Return type: 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: Returns: The formatted string.
Return type: 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: Returns: The formatted string.
Return type: 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: Returns: The formatted string.
Return type: Examples
>>> verbatim(r"\renewcommand{}") '\\verb|\\renewcommand{}|' >>> print(verbatim(r"\renewcommand{}")) \verb|\renewcommand{}| >>> print(verbatim('pi|pe', '!')) \verb!pi|pe!