From 913ce3a35e935ea68d2fe35949984bfd32816cea Mon Sep 17 00:00:00 2001 From: Jacob Tomaw <60254+flatiron32@users.noreply.github.com> Date: Sat, 1 Feb 2025 12:35:00 -0500 Subject: [PATCH] Add support for the \emph command --- docs/source/changelog.rst | 4 ++++ examples/full.py | 3 ++- pylatex/utils.py | 30 ++++++++++++++++++++++++++++++ tests/test_args.py | 3 +++ 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/docs/source/changelog.rst b/docs/source/changelog.rst index 306933d9..9f63c6c4 100644 --- a/docs/source/changelog.rst +++ b/docs/source/changelog.rst @@ -14,6 +14,10 @@ This version might not be stable, but to install it use:: pip install git+https://github.com/JelteF/PyLaTeX.git +Added +~~~~~ +- Add support for the ``\emph`` command + 1.4.2_ - `docs <../v1.4.2/>`__ - 2023-10-19 ------------------------------------------- diff --git a/examples/full.py b/examples/full.py index 06f97264..fe980b2b 100755 --- a/examples/full.py +++ b/examples/full.py @@ -27,7 +27,7 @@ Tabular, TikZ, ) -from pylatex.utils import italic +from pylatex.utils import italic, emphasis if __name__ == "__main__": image_filename = os.path.join(os.path.dirname(__file__), "kitten.jpg") @@ -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(emphasis("Even some emphasized text. ")) doc.append("\nAlso some crazy characters: $&#{}") with doc.create(Subsection("Math that is incorrect")): doc.append(Math(data=["2*3", "=", 9])) diff --git a/pylatex/utils.py b/pylatex/utils.py index ee453c85..ecb904b7 100644 --- a/pylatex/utils.py +++ b/pylatex/utils.py @@ -298,6 +298,36 @@ def italic(s, *, escape=True): return NoEscape(r"\textit{" + s + "}") +def emphasis(s, *, escape=True): + r"""Make a string appear emphasis in LaTeX formatting. + + emphasis() wraps a given string in the LaTeX command \emph{}. + + Args + ---- + s : str + The string to be formatted. + escape: bool + If true the emphasis text will be escaped + + Returns + ------- + NoEscape + The formatted string. + + Examples + -------- + >>> emphasis("hello") + NoEscape(\emph{hello}) + >>> print(emphasis("hello")) + \emph{hello} + """ + if escape: + s = escape_latex(s) + + return NoEscape(r"\emph{" + s + "}") + + def verbatim(s, *, delimiter="|"): r"""Make the string verbatim. diff --git a/tests/test_args.py b/tests/test_args.py index 348eb074..4f908480 100755 --- a/tests/test_args.py +++ b/tests/test_args.py @@ -80,6 +80,7 @@ fix_filename, italic, verbatim, + emphasis, ) matplotlib.use("Agg") # Not to use X server. For TravisCI. @@ -493,6 +494,8 @@ def test_utils(): italic(s="") + emphasis(s="") + verbatim(s="", delimiter="|")