Pexpect output capture

I'm having problems with pexpect . I am trying to get the output from tralics , which reads in latex equations and emits a MathML representation, for example:

 1 ~/ % tralics --interactivemath This is tralics 2.14.5, a LaTeX to XML translator, running on tlocal Copyright INRIA/MIAOU/APICS/MARELLE 2002-2012, Jos\'e Grimm Licensed under the CeCILL Free Software Licensing Agreement Starting translation of file texput.tex. No configuration file. > $x+y=z$ <formula type='inline'><math xmlns='http://www.w3.org/1998/Math/MathML'><mrow><mi>x</mi> <mo>+</mo><mi>y</mi><mo>=</mo><mi>z</mi></mrow></math></formula> > 

So I'm trying to get the formula with pexpect:

 import pexpect c = pexpect.spawn('tralics --interactivemath') c.expect('>') c.sendline('$x+y=z$') s = c.read_nonblocking(size=2000) print s 

The output has a formula, but with the original input at the beginning and some control characters at the end:

 "x+y=z$\r\n<formula type='inline'><math xmlns='http://www.w3.org/1998/Math/MathML'><mrow><mi>x</mi><mo>+</mo><mi>y</mi><mo>=</mo><mi>z</mi></mrow></math></formula>\r\n\r> \x1b[K" 

I can clear the output line, but I have to miss something basic. Is there a cleaner way to get MathML?

+4
source share
1 answer

From what I understand, you are trying to get this from pexpect:

 <formula type='inline'><math xmlns='http://www.w3.org/1998/Math/MathML'><mrow><mi>x</mi> <mo>+</mo><mi>y</mi><mo>=</mo><mi>z</mi></mrow></math></formula> 

You can use regexp instead of ">" to match to get the expected result. This is the simplest example:

 c.expect("<formula.*formula>"); 

After that, you can access the matched string by calling the pexpect matching attribute:

 print c.match 

You can also try different regular expressions, because the one I posted is greedy, and this may interfere with your execution if the formulas are large.

+4
source

Source: https://habr.com/ru/post/1438983/


All Articles