Skip to content
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

Add tab triggered calculation #25

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sympy
latex2sympy2
36 changes: 36 additions & 0 deletions tex.snippets
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,42 @@ snip.rv = subprocess.check_output(['wolframscript', '-code', code])
`
endsnippet

priority 10000
context "math()"
snippet '^(.*(?:= |\$))(.*[^\s=])\s*$' "latex-sympy-calculator-expression" wr
`!p
import re
from latex2sympy2 import latex2latex, latex2sympy, var, variances, set_variances, set_real, latex
from sympy import simplify
prefix = match.group(1)
code = match.group(2)
reg = re.match(r'^(.*)\&\s*=(?:.*)$', prefix)
sep = '\n' + ' ' * len(reg.group(1)) + '&= ' if reg else ' = '
try:
snip.rv = prefix + code + sep + latex(simplify(latex2sympy(code).subs(variances)))
except:
snip.rv = prefix + code + sep
`
endsnippet

priority 10000
context "math()"
snippet '^(.*(?:= |\$))(.*)=\s*$' "latex-sympy-calculator-numeric" wr
`!p
import re
from latex2sympy2 import latex2latex, latex2sympy, var, variances, set_variances, set_real, latex
from sympy import simplify
prefix = match.group(1)
code = match.group(2)
reg = re.match(r'^(.*)\&\s*=(?:.*)$', prefix)
sep = '\n' + ' ' * len(reg.group(1)) + '&= ' if reg else ' = '
try:
snip.rv = prefix + code + sep + latex(simplify(latex2sympy(code).subs(variances).doit().doit()).evalf(subs=variances))
except:
snip.rv = prefix + code + sep
`
endsnippet

snippet == "equals" iA
&= $1 \\\\
endsnippet
Expand Down