Skip to content

Commit

Permalink
Added aastex.Bibliography class.
Browse files Browse the repository at this point in the history
  • Loading branch information
byrdie committed Oct 30, 2023
1 parent 3070ff7 commit a672c48
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 35 deletions.
12 changes: 12 additions & 0 deletions aastex/_aastex.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"Marker",
"Label",
"Figure",
"Bibliography",
]

text_width_inches = 513.11743 / 72
Expand Down Expand Up @@ -317,6 +318,7 @@ def __init__(
data=data,
)
self.escape = False
self.preamble.append(pylatex.Command('bibliographystyle', 'aasjournal'))

def set_variable_quantity(
self,
Expand Down Expand Up @@ -351,3 +353,13 @@ def set_variable_quantity(
)
),
)


class Bibliography(pylatex.base_classes.CommandBase):
def __init__(
self,
sources: str,
):
super().__init__(
arguments=sources,
)
10 changes: 10 additions & 0 deletions aastex/_tests/test_aastex.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,13 @@ def test_set_variable_quantity(
)

assert name in a.dumps()


@pytest.mark.parametrize(
argnames="a",
argvalues=[
aastex.Bibliography("sources"),
]
)
class TestBibliography:
pass
56 changes: 21 additions & 35 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,59 +64,45 @@ Here is a simple example showing some of the basic features of :mod:`aastex`.
abstract.append(r"\lipsum[1-1]")
doc.append(abstract)

# Define the introduction of the article
intro = aastex.Section("Introduction")
intro.packages.append(aastex.Package("lipsum"))
intro.append(r"\lipsum[2-4]")
doc.append(intro)
# Define the speed of light as a variable that can be used in the document
doc.set_variable_quantity(
name="speedOfLight",
value=astropy.constants.c.to(u.km / u.s),
scientific_notation=True,
digits_after_decimal=4,
)

# Define a column-width figure with random data
fig, ax = plt.subplots(
figsize=(aastex.column_width_inches, 2),
constrained_layout=True,
)
data = np.random.normal(size=(11, 11))
ax.plot(data)
x = np.linspace(-6, 6, num=101)[..., np.newaxis]
y = np.sinc(x) + np.random.normal(scale=0.1, size=(101, 11))
ax.plot(*np.broadcast_arrays(x, y))
figure = aastex.Figure("data")
figure.add_fig(fig, width=None)
plt.close(fig)
figure.add_caption(aastex.NoEscape(
r"Here is a figure caption. \lipsum[5-5]"
))
doc.append(figure)

# Define the speed of light as a variable that can be used in the document
doc.set_variable_quantity(
name="speedOfLight",
value=astropy.constants.c.to(u.km / u.s),
scientific_notation=True,
digits_after_decimal=4,
)
# Define a new section of this article with some references
methods = aastex.Section("Methods")
methods.append(
# Define the introduction of the article
intro = aastex.Section("Introduction")
intro.packages.append(aastex.Package("lipsum"))
intro.append(
rf"Here is a citation \citep{{knuth:1984}}. "
rf"The speed of light is \speedOfLight. "
rf"Here is a reference to Section {intro}. "
rf"Here is a reference to Figure {figure}. "
rf"\lipsum[2-2]"
)
doc.append(methods)
doc.append(intro)
intro.append(figure)
intro.append(r"\lipsum[3-5]")

# Define a text-width figure with random data
fig2, ax2 = plt.subplots(
figsize=(aastex.text_width_inches, 2),
constrained_layout=True,
)
x = np.linspace(-6, 6, num=101)[..., np.newaxis]
y = np.sinc(x) + np.random.normal(scale=0.1, size=(101, 11))
ax2.plot(*np.broadcast_arrays(x, y))
figure2 = aastex.FigureStar("data2")
figure2.add_fig(fig2, width=None)
plt.close(fig2)
figure2.add_caption(aastex.NoEscape(
r"Here is another figure caption. \lipsum[6-6]"
))
doc.append(figure2)
# Add the bibliography from sources.bib
doc.append(aastex.Bibliography("sources"))

# Compile the document into a PDF
path_pdf = pathlib.Path("an_interesting_article.pdf")
Expand Down
10 changes: 10 additions & 0 deletions docs/sources.bib
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@article{knuth:1984,
title={Literate Programming},
author={Donald E. Knuth},
journal={The Computer Journal},
volume={27},
number={2},
pages={97--111},
year={1984},
publisher={Oxford University Press}
}

0 comments on commit a672c48

Please sign in to comment.