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
Greek letters, \bar{}, and \hat{} #6
Comments
Yes indeed. This kind of modification should be optional, or it will include unintended changes, e.g.:
Especially the second case is quite troublesome, as the underscore ( For the interface, some optional parameter of
I think |
Something like this? Behaviors: Code: # ...
from typing import Callable
# ...
class LatexifyVisitor(ast.NodeVisitor):
def __init__(self, math_symbol=True):
self.math_symbol = math_symbol
super(ast.NodeVisitor).__init__()
def _parse_math_symbols(self, val: str) -> str:
if not self.math_symbol:
return val
else:
greek_and_hebrew = [
'aleph', 'alpha', 'beta', 'beth', 'chi', 'daleth',
'delta', 'digamma', 'epsilon', 'eta', 'gamma', 'gimel',
'iota', 'kappa', 'lambda', 'mu', 'nu', 'omega', 'omega',
'phi', 'pi', 'psi', 'rho', 'sigma', 'tau', 'theta',
'upsilon', 'varepsilon', 'varkappa', 'varphi', 'varpi', 'varrho',
'varsigma',
'vartheta', 'xi', 'zeta'
]
if val.lower() in greek_and_hebrew:
return '\\' + val + ' '
else:
return val
# ........
def visit_FunctionDef(self, node):
name_str = r'\operatorname{' + str(node.name) + '}'
arg_strs = [self._parse_math_symbols(str(arg.arg)) for arg in node.args.args]
body_str = self.visit(node.body[0])
return name_str + '(' + ', '.join(arg_strs) + r') \triangleq ' + body_str
# ........
def visit_Name(self, node):
return self._parse_math_symbols(str(node.id))
# ........
def get_latex(fn, math_symbol=True):
return LatexifyVisitor(math_symbol=math_symbol).visit(ast.parse(inspect.getsource(fn)))
# ........
def with_latex(*args, math_symbol=True):
class _LatexifiedFunction:
def __init__(self, fn):
self._fn = fn
self._str = get_latex(fn, math_symbol=math_symbol)
# ........
if len(args) == 1 and isinstance(args[0], Callable):
return _LatexifiedFunction(args[0])
else:
return lambda fn: _LatexifiedFunction(fn) |
I also suggest the following: # Old:
ast.Mult: (lambda: _wrap(l) + _wrap(r)),
# New:
ast.Mult: (lambda: _wrap(l) + ' ' + _wrap(r)), Otherwise this does not work: function(beta, x):
return beta * x ^ That example is why I use
versus
If you do |
SGTM for the behavior of the You can insert braces as symbol separators: |
Yes. I am considering a better way to traverse ASTs and will change them in some future commits. |
Something like this would be a good idea:
I have an implementation here, but I am not sure if it is good, or if it will do unexpected things:
https://github.com/ryxcommar/latexify_py/tree/master
The text was updated successfully, but these errors were encountered: