A simple Python DSL for serializing paper data into TeX format.
tex_paper_toolkit/tests/readme/minimal_example.py
Lines 4 to 28 in 12c85c7
from tex_paper_toolkit import DefaultToolkit | |
# instantiate a toolkit object | |
tex = DefaultToolkit() | |
# manually add new TeX (constant) definitions (\\newcommand) | |
tex.newcommand("constantOne", 1) | |
# by default, invalid characters are omited from the TeX label, | |
# but we can force serialization of digits via their names | |
tex.newcommand("constant2", 2, spell_digits=True) | |
# add custom TeX strings that should be directly serialized | |
# (the label here is just to get a unique identifier for the toolkit) | |
tex.texstring("emph-msg", r"\emph{Emphasized text}") | |
# specify a custom serialization target for this particular string | |
# (could also be achieved by using different `Toolkit` instances) | |
tex.texstring( | |
"other-file-string", | |
r"\textbf{Text in a different file!}", | |
to_file="tex_texstring_output.tex", | |
) | |
# serialize the saved contents to the given TeX file | |
tex.serialize(to_file="tex_output.tex") |
tex_paper_toolkit/tests/readme/custom_mixins_example.py
Lines 8 to 59 in 12c85c7
from tex_paper_toolkit import DefaultToolkit, ToolkitMixin, Serializable | |
class TextttString(Serializable): | |
""" | |
A custom mixin serializable that holds text that should be formatted in code | |
font. | |
""" | |
def __init__(self, key, texttt_str): | |
super().__init__(key) | |
self.__texttt_str = texttt_str | |
def serialize(self): | |
return f"\\texttt{{{self.__texttt_str}}}" | |
class TextttMixin(ToolkitMixin): | |
""" | |
A custom mixin that simply wraps the passed text in `\texttt{...}`. | |
""" | |
def texttt(self, label, texttt_str): | |
return self.add(TextttString(label, texttt_str)) | |
# define a new toolkit by inheriting DefaultToolkit | |
# DefaultToolkit already includes some mixins, but we can also inject custom ones | |
class MyCustomToolkit(TextttMixin, DefaultToolkit): | |
pass | |
# instantiate a toolkit object | |
tex = MyCustomToolkit() | |
# manually add new TeX (constant) definitions (\\newcommand) | |
tex.newcommand("const", 1) | |
# this overwrites the previous definition of 'const' | |
tex.newcommand("const", 3, mathmode=False) | |
# add custom TeX strings that should be directly serialized | |
tex.texstring( | |
"msg1", | |
r"\textbf{This is just some random tex code that should also appear in the file}", | |
) | |
# call our custom mixin method | |
tex.texttt("code1", "val x = 10") | |
# serialize the saved contents to the given TeX file | |
tex.serialize(to_file="tex_output.tex") |
- Add pandas/numpy-to-table mixin