我正在尝试将计算器输入转换为LaTeX。如果用户输入以下内容:
(3x^(x+(5/x)+(x/33))+y)/(32 + 5)
我必须将其转换为:
frac{3x^(x+frac{5}{x}+frac{x}{33})+y}{32 + 5x}
然而,我在确定分子何时开始和结束时遇到了问题。有什么建议吗?
我正在尝试将计算器输入转换为LaTeX。如果用户输入以下内容:
(3x^(x+(5/x)+(x/33))+y)/(32 + 5)
我必须将其转换为:
frac{3x^(x+frac{5}{x}+frac{x}{33})+y}{32 + 5x}
然而,我在确定分子何时开始和结束时遇到了问题。有什么建议吗?
已存在用于此类转换的包:Py2Tex公司
如果您想重用此包,可以使用py2tex.Interpret
类来实现这一点。
看看编译器
compiler.parse( (3*x**(x+(5/x)+(x/33))+y)/(32 + 5) )
退货
Module(None, Stmt([Discard(Div((Add((Mul((Const(3), Power((Name( x ), Add((Add((Name( x ), Div((Const(5), Name( x ))))), Div((Name( x ), Const(33))))))))), Name( y ))), Add((Const(32), Const(5))))))]))
其可以更容易地转换为LaTeX代码。您必须编写方法,以递归方式处理每个代码(Div、Add、Const、Name、Power…)及其参数,并返回适当的LateX代码。
如果你想使用问题的确切语法,那么你可以写计算器的解析器:
#!/usr/bin/env python
from operator import add, div, mul, sub
from lepl import Any, Delayed, Digit, Drop, DroppedSpace, Eos
from sympy import Symbol, latex, pprint
def Power(tokens):
"""x**(y**z)"""
return reduce(lambda p, b: pow(b, p), reversed(tokens))
def Arithm(tokens):
"""(x op1 y) op2 z ..."""
OP = { * : mul, / : div, + : add, - : sub, }
tokens = iter(tokens)
a = next(tokens)
for op, b in zip(tokens, tokens):
a = OP[op](a, b)
return a
def makeparser():
expr = Delayed()
number = Digit()[1:,...] >> int # d+
symbol = Any( xyz ) >> (lambda x: Symbol(bytes(x))) # unicode -> str
muldiv_op = Any( */ )[:1] > (lambda x: x[0] if x else * ) # xy -> x*y
power_op = Drop( ^ ) | Drop( ** ) # both stand for pow(x, y)
with DroppedSpace(): # ignore spaces
term = number | symbol | Drop( ( ) & expr & Drop( ) )
power = term & (power_op & term)[:] > Power
factor = power & (muldiv_op & power)[:] > Arithm
expr += factor & (Any( -+ ) & factor)[:] > Arithm
line = expr & Eos()
return line.get_parse()
parse = makeparser()
[expr] = parse( (3x^(x+(5/x)+(x/33))+y)/(32 + 5) )
pprint(expr)
print(latex(expr))
34⋅x 5
──── + ─
33 x
y 3⋅x
── + ───────────
37 37
$frac{1}{37} y + frac{3}{37} x^{frac{34}{33} x + frac{5}{x}}$
一般来说,最好使用Python等现有语言的语法。
Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...
I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...
Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...
Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...
I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...
Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...
Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...
I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...