Watexy – Latex robot for Google Wave

icon3 , , , ,
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

I finally got access to a Google Wave sandbox account, making it possible to start experimenting. The first thing I made was a Latex robot. By adding the robot, it’s possible to write Latex in Waves. Afterwards, simply by putting Latex between two $’s:

$$\lim_{n \to \infty} \frac{1}{n} = 0$$

Watexy then changes the Wave and inserts the Latex as an image instead of the text.

You can try it yourself by adding watexy [this-funny-curly-a] appspot.com to your Wave-conversations. You can download the source here.

There’s still a quite big bug: It’s only the first Latex-image that gets substituted correctly. The following gets inserted some positions wrong, and the deletion of the text isn’t correct neither. But the images itself are correct, only the substitution isn’t working properly. But I’m working on that!

The more technical part: the robot is written in Python, hence using the Google Wave Python API. It’s the first time I’ve tried that (thought it was a good possibility to learn it). The robot basically works by getting triggered when a new blip is submitted. Then it searches for Latex-code in that and substitutes it with pictures. Although the code is quite simple, it shows some very basic concepts on how to alter/change the contents of Waves on-the-fly (deleting text and inserting images, but it could be other things as well) with robots. The Latex-source-to-image is done by the http://www.forkosh.dreamhost.com/source_mathtex.html#webservice service.

The core Python comprising the robot is this:

# Python reference:
# http://wave-robot-python-client.googlecode.com/svn/trunk/pydocs/index.html
 
# Shortcut to the important OpBasedDocument
# http://wave-robot-python-client.googlecode.com/svn/trunk/pydocs/waveapi.ops.OpBasedDocument-class.html
 
""" 
Known bugs:
- From (and including) the second Latex-fragment, the positioning gets wrong
  > Probably the document needs some kind of updatering
  > Even though it's a bit strange since the re matches all the Latex correctly
"""  
 
__author__ = 'mikl@mikl.dk (Mikkel Meyer Andersen)'
 
import re
 
from waveapi import events
from waveapi import model
from waveapi import robot
from waveapi import document
 
def OnRobotAdded(properties, context):
  """Invoked when the robot has been added."""
  root_wavelet = context.GetRootWavelet()
  root_wavelet.CreateBlip().GetDocument().SetText("Hi. My name is Watexy and I'm here to help you presenting Latex in waves. Just put the latex between $$ and $$, e.g. $$2+2=5$$. This robot uses the http://www.forkosh.dreamhost.com/source_mathtex.html#webservice service.")
 
def OnBlipSubmitted(properties, context):
  """Invoked when a blip has been added."""
  blip = context.GetBlipById(properties['blipId']) 
  blip_text_view = blip.GetDocument()
 
  latex_regex = re.compile('\$\$(.+?)\$\$')
  m = latex_regex.search(blip_text_view.GetText())
 
  """ 
  Only replace one Latex-fragment at a time, because replaceing one fragment
  changes the text positions in the rest. That's why re.finditer isn't used.  
  """
  while m != None:
    """
    The +/- 2 is because of the length of the $$'s. 
    If not removed, the loop will run infintely! 
    """
    blip_text_view.DeleteRange(document.Range(m.start(1)-2, m.end(1)+2))
    image = document.Image('http://www.forkosh.dreamhost.com/mathtex.cgi?' + m.group(1), caption=m.group(1))
    blip_text_view.InsertElement(m.start(1)-2, image)    
    m = latex_regex.search(blip_text_view.GetText())
 
if __name__ == '__main__':
  myRobot = robot.Robot('watexy', 
      image_url='http://watexy.appspot.com/assets/icon.png',
      version='9',
      profile_url='http://watexy.appspot.com/')  
  myRobot.RegisterHandler(events.WAVELET_SELF_ADDED, OnRobotAdded)
  myRobot.RegisterHandler(events.BLIP_SUBMITTED, OnBlipSubmitted)
  myRobot.Run()

27 Responses

  1. Mikkel Meyer Andersen Says:

    A live-demo is available at:
    https://wave.google.com/a/wavesandbox.com/#restored:wave:wavesandbox.com!w%252BnW6AFIIa%2525A

  2. Mikkel Meyer Andersen Says:

    Well, you have to copy the entire URL – for some reason WordPress apparantly doesn’t think ! is valid in a URL.

  3. .yankee Says:

    If you added linefeed and caret return to the regex, wouldn’t it work with multi-line entries as well? That’d be very useful for matrices for example.

  4. Mikkel Meyer Andersen Says:

    Yes, if the LaTeX-webservice supports it. But I’ll give it a try. Thanks!

  5. Mikkel Meyer Andersen Says:

    I’ll also change the way the robot introduces itself so that the waves doesn’t get too messy/spammed.

  6. Google Wave Blogger Says:

    Tried it out. Cool Robot. Good work!

    Your invited to join our forums at http://www.googlewaveblogger.com and share your development efforts. I then blog about the developers that have joined the forums.

    Cheers! Good luck with the work.

  7. Gustav Delius Says:

    The robot works well. Are you planning to develop it further? For example it would be even nicer if it could show the original LaTeX source when I am editing a blip.

  8. Mikkel Meyer Andersen Says:

    Hi Gustav. Thanks. Yes, I am planning to develop it further, but have been really busy the last couple of weeks, but hopefully I’ll find the time very soon :) .

  9. Nathan Triplett Says:

    This is great, I’m going to have to give it a go soon. If you’ll allow me to share my dream LaTeX plug-in, perhaps it will inspire someone.

    First start off with your robot.

    Now let your robot help me when I’m confused. Say I type in helpme or something.

    Then I’d love it to do something like this:
    http://detexify.kirelabs.org/classify.html

    While just having it link me to that site would be ok, in my dream I imagine that it would include a drawing pad into the wave. Then let me draw a symbol. And then I could click the one I like and it would include the correct LaTeX for me.

    /dream

    Cheers and thanks for the plug-in

  10. AcidFlask Says:

    Try http://chart.apis.google.com/chart?cht=tx&chs=1×0&chf=bg,s,FFFFFF00&chco=000000&chl=

    Someone on Reddit mentioned it.

  11. AcidFlask Says:

    I mean,

    http://www.reddit.com/r/math/comments/9pxpm/looks_like_google_docs_added_latex_support/

  12. alejandro Says:

    Hi,
    I like your bot. If there is one major thing it needs its this. I’m frequently revising the LaTeX command and the bot deletes it. It would be great if it inserted the original command in small grey print so I can reuse it. alejandro.erickson googlewave.com

  13. Jonathan Hunt Says:

    Hi,

    I’ve made a similar robot, but also an Equation gadget to go with it, this means rather than replacing the equations with images, I replace them with an editable gadget. I’d be keen to collaborate with others on making Wave more useful for scientists.

    If you have access to Wave preview you can learn more about the gadget/bot here:
    https://wave.google.com/wave/#restored:wave:googlewave.com!w%252B4muyQgqQR.2

    otherwise I will be putting more info up soon at http://waveyscience.appspot.com .

  14. Bry Says:

    Hi,

    I had some time to spare today and I decided to port the robot over to Java. I hope you don’t mind. ;)

    I also added the grey out feature that some of the commenters here requested. It’s a bit wonky due to the current state of wave, though.

  15. Fleischlego Says:

    I just thought about the advances of a bot like this in my car today.

    Amazing work, thank you!

  16. Dmitriy Says:

    Great robot. One thing that I’m really missing when using it is the ability to define my own commands (in the preamble). For instance, I like to give the symbols I commonly use short names (e.g. \newcommand{\Rsp}{\mathbb{R}}).

    Is it possible to add such an ability to watexy?

    I skimmed through the mathtex page, they clearly let one add \usepackage, but I don’t know about \newcommand.

    Ideally, I think one could put commands into preamble within watexy’s original response (or in reply to it), and they’d be used in all subsequent parsings.

  17. miniBill Says:

    THIS ROBOT ROCKS!!!

  18. Michael Says:

    Hi! Great robot!

    The 2 equations problem is a bit annoying, though. It is hard to write only one eq. per blip.

    Did you try to go through m in reverse order? Last to first? Then the positions of all the preceding equations will not be changed…

  19. Mikkel Meyer Andersen Says:

    Please notive the update at
    http://www.scienco.org/2009/watexy-version-12-now-supporting-multiple-equations-in-one-blip/

  20. Maarten Derickx Says:

    It would be nice to also have a version of watexy which behaves more like a latex editor.

    I.e, that watexy replies to your wave, and watexy updates it’s reply as soon as you edit your original message. In this way you could use watexy really as a collaborative latex editing tool.

    Like watexy is now it’s easy for in chat like an mail like usage. But the ability to be able to edit your latex code afterwards would really enable collaborative edditing.

  21. lostella Says:

    i agree with Maarten Derickx. also, will it say hello with that message every time i’m gonna add it to a wave? that would so boring

  22. Mikkel Meyer Andersen Says:

    Thanks for the comment, lostella. You’re probably right. I better remove it, but because I’m travelling at the moment it’ll take some weeks before it’s done.

  23. Pablo Arias Says:

    Congratulations, great robot!!

    I have I suggestion. Has anyone though about having different waves for the source (.tex wave) and the compiled version of it (.dvi wave)?

    The latter could be read-only.

    Watexy as it is now is a great tool for discussion.

    However, if at a certain point you decide you want to write a proper latex document, you cannot do it in collaboration.

    Furthermore, now you can’t reuse the wave to write a tex. At least not easily. Having access to the source would allow its reuse.

    I just throw the idea. Don’t have any clue on its viability.

    Tanks for watexy!
    Pablo Arias

  24. Claus Says:

    Very nice bot! In order to be able to use the document which has been elaborated in the wave, it would be very nice to be able to export the text with the original latex code contained. The \[ \] mode seems to store the original latex, so it should be quite easy to get the document with latex code out of the wave? (In order to run it with latex and pubish the paper for example) The bot could answer to a command with this complete text+latex code.

  25. Oyvind Says:

    Nice!

    $$ doesn’t work for me, and the images aren’t completely rendered always, but heck; wave isn’t out yet :)

  26. Mikkel Meyer Andersen Says:

    That sounds a bit strange. I just checked it and the images showed up as usual. I haven’t checked the actual $$ to image conversion, but I haven’t change anything. But maybe Wave have been updated and bugs have been introduced. It’s certainly been seen before. Let’s give it some time.

  27. Domenico Says:

    First of all, thanks for your work. I have tosay that I have the
    same problem here too. Formulas between $$ don’t show at all and these in \[ only show as missing picture icons.

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.