From c8e4ecd46c5bc11039290d8fa37cd96961d903e6 Mon Sep 17 00:00:00 2001 From: Roy Smart Date: Tue, 4 Jun 2024 15:04:58 -0600 Subject: [PATCH 1/3] Added `aastex.Variable` class. --- aastex/_aastex.py | 31 +++++++++++++++++++++++++++++++ aastex/_tests/test_aastex.py | 19 +++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/aastex/_aastex.py b/aastex/_aastex.py index 344cd46..93ecd92 100644 --- a/aastex/_aastex.py +++ b/aastex/_aastex.py @@ -24,6 +24,7 @@ "Affiliation", "Author", "Acronym", + "Variable", "Abstract", "Section", "Subsection", @@ -170,6 +171,36 @@ def dumps(self): return command +@dataclasses.dataclass +class Variable(pylatex.base_classes.LatexObject): + + name: str + """The name of the variable.""" + + value: float | u.Quantity + """The value of the variable.""" + + @property + def _name(self) -> str: + return NoEscape(f"\\{self.name}") + + @property + def _value(self) -> str: + v = self.value + if isinstance(v, u.Quantity): + v = f"{v:latex_inline}" + v = rf"\ensuremath{{{v[1:~0]}}}" + else: + v = str(v) + return NoEscape(v) + + def dumps(self) -> str: + return Command( + command="newcommand", + arguments=[self._name, self._value], + ).dumps() + + class Abstract(pylatex.base_classes.Environment): def __init__( self, diff --git a/aastex/_tests/test_aastex.py b/aastex/_tests/test_aastex.py index b83b91f..4a7176b 100644 --- a/aastex/_tests/test_aastex.py +++ b/aastex/_tests/test_aastex.py @@ -107,6 +107,25 @@ def test_dumps(self, a: aastex.Title): assert isinstance(a.dumps(), str) +@pytest.mark.parametrize( + argnames="a", + argvalues=[ + aastex.Variable("foo", 2), + aastex.Variable("bar", 3 * u.AA), + ] +) +class TestVariable: + + def test_name(self, a: aastex.Variable): + assert isinstance(a.name, str) + + def test_value(self, a: aastex.Variable): + assert isinstance(a.value, (int, float, u.Quantity)) + + def test_dumps(self, a: aastex.Variable): + assert isinstance(a.dumps(), str) + + @pytest.mark.parametrize( argnames="a", argvalues=[ From 4215bb72ac43cae9d59e3be3935b71f497f87d45 Mon Sep 17 00:00:00 2001 From: Roy Smart Date: Tue, 4 Jun 2024 15:06:15 -0600 Subject: [PATCH 2/3] black --- aastex/_tests/test_aastex.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aastex/_tests/test_aastex.py b/aastex/_tests/test_aastex.py index 4a7176b..7f91d36 100644 --- a/aastex/_tests/test_aastex.py +++ b/aastex/_tests/test_aastex.py @@ -112,7 +112,7 @@ def test_dumps(self, a: aastex.Title): argvalues=[ aastex.Variable("foo", 2), aastex.Variable("bar", 3 * u.AA), - ] + ], ) class TestVariable: From d1d2c13bcd0ebc6d2d26f3d91f5dc5b0062101d0 Mon Sep 17 00:00:00 2001 From: Roy Smart Date: Tue, 4 Jun 2024 15:10:22 -0600 Subject: [PATCH 3/3] docs --- aastex/_aastex.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/aastex/_aastex.py b/aastex/_aastex.py index 93ecd92..cd302b6 100644 --- a/aastex/_aastex.py +++ b/aastex/_aastex.py @@ -173,6 +173,9 @@ def dumps(self): @dataclasses.dataclass class Variable(pylatex.base_classes.LatexObject): + """ + A wrapper around the ``\\newcommand`` LaTeX command. + """ name: str """The name of the variable."""