-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate equation into its own file. User should not have to fire up …
…a onedoc class only to convert functions to latex/word snippets
- Loading branch information
Showing
5 changed files
with
52 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from .common import MY_DOCX_TMPL, MY_DOCX_TMPL_BLANK | ||
from .document import OneDoc | ||
from .equations import Equation | ||
|
||
__all__ = [MY_DOCX_TMPL, MY_DOCX_TMPL_BLANK, OneDoc] | ||
__all__ = [MY_DOCX_TMPL, MY_DOCX_TMPL_BLANK, OneDoc, Equation] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass, field | ||
from typing import Callable, List | ||
|
||
from .common import MarkDownFile | ||
|
||
|
||
@dataclass | ||
class Equation: | ||
name: str | ||
func: Callable | ||
custom_eq_str_compiler: Callable = None | ||
add_link: bool = True | ||
include_python_code: bool = False | ||
md_instances: List[MarkDownFile] = field(default_factory=list) | ||
docx_instances: List[object] = field(default_factory=list) | ||
|
||
def to_latex(self, print_latex=False, print_formula=False, flags=None): | ||
if self.custom_eq_str_compiler is not None: | ||
return self.custom_eq_str_compiler(self.func) | ||
|
||
from inspect import getsource, getsourcelines | ||
|
||
import pytexit | ||
|
||
lines = getsourcelines(self.func) | ||
eq_latex = "" | ||
matches = ("def", "return", '"') | ||
dots = 0 | ||
for line in lines[0]: | ||
if any(x in line for x in matches): | ||
dots += line.count('"') | ||
dots += line.count("'") | ||
continue | ||
if dots >= 6 or dots == 0: | ||
eq_latex += pytexit.py2tex(line, print_latex=print_latex, print_formula=print_formula) + "\n" | ||
eq_str = eq_latex | ||
|
||
if self.add_link: | ||
eq_str += f"{{#eq:{self.name}}}" | ||
|
||
if self.include_python_code: | ||
eq_str = f"\n\n```python\n{getsource(self.func)}\n```\n\n" + eq_str | ||
return eq_str |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters