Comments (6)
Yes indeed. This kind of modification should be optional, or it will include unintended changes, e.g.:
xi
...\xi
orx_i
y_hat
...\hat{y}
ory_{hat}
Especially the second case is quite troublesome, as the underscore (_
) completely behaves differently.
For the interface, some optional parameter of with_latex
/get_latex
would help to switch the behavior:
@with_latex(math_symbol=True, diacritic=True)
def foobar(...):
...
I think math_symbol
is okay to add, but consider diacritic
is not for now. Probably we need to support subscripts before diacritics.
from latexify_py.
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 return '\\' + val + ' '
:
beta * x
--> \betax
(bad)
versus
beta * x
--> \beta x
(good)
If you do ast.Mult: (lambda: _wrap(l) + ' ' + _wrap(r)),
instead, then it is no longer necessary to do '\\' + val + ' '
; you will only need '\\' + val
from latexify_py.
SGTM for the behavior of the math_symbol
.
You can insert braces as symbol separators: {\beta}{x}
. I think every identifier should be put in a brace for safety.
from latexify_py.
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)
from latexify_py.
Need to be careful with wrapping everything in {}
. I realized if you do that for every self.visit_Name
, you get this:
I am not good with the ast
library, so I'm not sure how you would do it 100% correctly. For now I am going to be safe, and only convert a name if it is in greek_and_hebrew
.
from latexify_py.
Yes. I am considering a better way to traverse ASTs and will change them in some future commits.
from latexify_py.
Related Issues (20)
- Should `def(x): func(x)` generate `\func x` or `\func \mathopen{}\left( x \mathclose{}\right)`? HOT 2
- Fine-grained control over function name replacements
- Designing "plugin" interface HOT 3
- Use `latex.py` for to standardize codegen HOT 2
- Feedback From a User HOT 14
- Please add support for Python >3.11 HOT 2
- IPython extension to automatically use conversions on displayed objects HOT 1
- Better Identifier For Multi Index and RHS HOT 4
- Can you sub in values for show work? HOT 1
- Сonverting expressions or strings to latex format HOT 5
- Release New Version HOT 6
- Support for sqrt-like nth-roots when rendering x**(1/p)? HOT 2
- Support for log1p and expm1? HOT 4
- `if-elif` statements break if there's no `else` HOT 1
- Typo in \mathopen HOT 3
- Counterintuitive (wrong?) parenthesis when combining exp() and powers HOT 16
- Function docstring and reduce_assignments enabled does not play nice HOT 2
- Include .tex output examples in latexify_py/examples/examples.ipynb HOT 3
- math.pow not working properly on google colab HOT 2
- Non working link on the pypi page of the project HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from latexify_py.