Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added aastex.Variable class. #4

Merged
merged 3 commits into from
Jun 4, 2024
Merged
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
34 changes: 34 additions & 0 deletions aastex/_aastex.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"Affiliation",
"Author",
"Acronym",
"Variable",
"Abstract",
"Section",
"Subsection",
Expand Down Expand Up @@ -170,6 +171,39 @@ def dumps(self):
return command


@dataclasses.dataclass
class Variable(pylatex.base_classes.LatexObject):
"""
A wrapper around the ``\\newcommand`` LaTeX command.
"""

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,
Expand Down
19 changes: 19 additions & 0 deletions aastex/_tests/test_aastex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=[
Expand Down
Loading