Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ This version might not be stable, but to install it use::
1.4.2_ - `docs <../v1.4.2/>`__ - 2023-10-19
-------------------------------------------

Added
~~~~~
- Add support for the ``\texttt`` command

Added
~~~~~
- Add `.Chapter` in ``__init__.py``
Expand Down
3 changes: 2 additions & 1 deletion examples/full.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
Tabular,
TikZ,
)
from pylatex.utils import italic
from pylatex.utils import italic, monospace

if __name__ == "__main__":
image_filename = os.path.join(os.path.dirname(__file__), "kitten.jpg")
Expand All @@ -38,6 +38,7 @@
with doc.create(Section("The simple stuff")):
doc.append("Some regular text and some")
doc.append(italic("italic text. "))
doc.append(monospace("Even some monospaced (typewriter) text. "))
doc.append("\nAlso some crazy characters: $&#{}")
with doc.create(Subsection("Math that is incorrect")):
doc.append(Math(data=["2*3", "=", 9]))
Expand Down
26 changes: 26 additions & 0 deletions pylatex/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,32 @@ def italic(s, *, escape=True):
return NoEscape(r"\textit{" + s + "}")


def monospace(s, *, escape=True):
r"""Make a string appear monospaced (typewriter) in LaTeX formatting.
monospace() wraps a given string in the LaTeX command \texttt{}.
Args
----
s : str
The string to be formatted.
escape: bool
If true the monospace text will be escaped.
Returns
-------
NoEscape
The formatted string.
Examples
--------
>>> monospace("hello")
NoEscape(\texttt{hello})
>>> print(monospace("hello"))
\texttt{hello}
"""
if escape:
s = escape_latex(s)

return NoEscape(r"\texttt{" + s + "}")


def verbatim(s, *, delimiter="|"):
r"""Make the string verbatim.

Expand Down
3 changes: 3 additions & 0 deletions tests/test_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
escape_latex,
fix_filename,
italic,
monospace,
verbatim,
)

Expand Down Expand Up @@ -493,6 +494,8 @@ def test_utils():

italic(s="")

monospace(s="")

verbatim(s="", delimiter="|")


Expand Down