formelsammlung/scripts/formulary.py

82 lines
2.5 KiB
Python
Raw Normal View History

2025-02-17 02:16:22 +01:00
#!/usr/bin env python3
import os
import matplotlib.pyplot as plt
import numpy as np
import math
import scipy as scp
import matplotlib as mpl
mpl.rcParams["font.family"] = "serif"
mpl.rcParams["mathtext.fontset"] = "stix"
mpl.rcParams["text.usetex"] = True
mpl.rcParams['text.latex.preamble'] = r'\usepackage{amsmath}\usepackage{siunitx}'
if __name__ == "__main__": # make relative imports work as described here: https://peps.python.org/pep-0366/#proposed-change
if __package__ is None:
__package__ = "formulary"
import sys
filepath = os.path.realpath(os.path.abspath(__file__))
sys.path.insert(0, os.path.dirname(os.path.dirname(filepath)))
from util.mpl_colorscheme import set_mpl_colorscheme
import util.colorschemes as cs
from util.gen_tex_colorscheme import generate_latex_colorscheme
# SET THE COLORSCHEME
# hard white and black
# cs.p_gruvbox["fg0"] = "#000000"
# cs.p_gruvbox["bg0"] = "#ffffff"
# COLORSCHEME = cs.gruvbox_light()
# COLORSCHEME = cs.gruvbox_dark()
# cs.p_tum["fg0"] = cs.p_tum["alt-blue"]
COLORSCHEME = cs.tum()
# COLORSCHEME = cs.legacy()
tex_src_path = "../src/"
img_out_dir = os.path.join(tex_src_path, "img")
filetype = ".pdf"
skipasserts = False
full = 8
size_half_half = (full/2, full/2)
size_third_half = (full/3, full/2)
size_half_third = (full/2, full/3)
def assert_directory():
if not skipasserts:
assert os.path.abspath(".").endswith("scripts"), "Please run from the `scripts` directory"
def texvar(var, val, math=True):
s = "$" if math else ""
s += f"\\{var} = {val}"
if math: s += "$"
return s
def export(fig, name, tight_layout=True):
assert_directory()
filename = os.path.join(img_out_dir, name + filetype)
if tight_layout:
fig.tight_layout()
fig.savefig(filename, bbox_inches="tight", pad_inches=0.0)
@np.vectorize
def smooth_step(x: float, left_edge: float, right_edge: float):
x = (x - left_edge) / (right_edge - left_edge)
if x <= 0: return 0.
elif x >= 1: return 1.
else: return 3*(x*2) - 2*(x**3)
# run even when imported
set_mpl_colorscheme(COLORSCHEME)
if __name__ == "__main__":
assert_directory()
s = \
"""% This file was generated by scripts/formulary.py\n% Do not edit it directly, changes will be overwritten\n""" + generate_latex_colorscheme(COLORSCHEME)
filename = os.path.join(tex_src_path, "util/colorscheme.tex")
print(f"Writing tex colorscheme to {filename}")
with open(filename, "w") as file:
file.write(s)