LaTeX in Empathy and Adium

Last updated on Mon, 02/12/2013 - 20:11

This is a little tutorial on how to embed MathJax in Empathy's and Adium's chat windows, making it possible to use LaTeX formulas in chats without resorting to plugins or calling external scripts.

I tested this change only in Empathy 3.6 on Ubuntu 12.10 with the "Ubuntu" message style, and other configurations may require different changes. Do not forget to make a backup of the files you are going to modify.

TO DO: call "Typeset" on the new fragment only, attach a screenshot (Nvidia's fault).

Embedding MathJax into an AdiumMessageStyle

Empathy and Adium use HTML+Javascript templates to display the content of the chat windows. Since they actually use Gecko and Webkit, it is possible to embed pretty much any code you like.

MathJax is a handy Javascript library that converts LaTeX formulas inside $$ ... $$ and \[ ... \] into MathML or images, depending on the configuration. In order to enable the MathJax library inside an Empathy chat window it is enough to do the following.

Install the MathJax library somewhere (either via your package manager or downloading it), copy/extract your favourite message style into the folder ~/.local/share/adium/message-styles/ and modify its Template.html in the following way:

  1. add the line <script type="text/javascript" src="/~mantova/io/%3Cstrong%3E%5Blocation%20of%20MathJax%5D%3C/strong%3E/MathJax.js?config=default"></script> just before the first <script> tag; this will import MathJax in the message style;
  2. add the line MathJax.Hub.Queue(["Typeset", MathJax.Hub]); inside the body of the appendMessage, near the end of the function, after the new message has been added to the page but before any other formatting commands (e.g., a call to alignChat); this tells MathJax to look for new LaTeX code every time a new message is displayed on the screen;
  3. repeat the last step inside the body of the function appendMessageNext, unless the function calls appendMessage.

I suggest to modify the file Info.plist as well appending a "+ MathJax" to the style name.

Instructions for Ubuntu 12.10

If you use Ubuntu 12.10 and the "Ubuntu" message style in Empathy, you can use the attached patch.

  1. Download the attached ubuntu-message-style+mathjax.patch
  2. Install libjs-mathjax:
    sudo apt-get install libjs-mathjax
  3. Create a directory in your home folder to host the new message style:
    mkdir -p ~/.local/share/adium/message-styles
  4. Copy the default Ubuntu message style to your home folder:
    cp -r /usr/share/adium/message-styles/ubuntu.AdiumMessageStyle ~/.local/share/adium/message-styles/ubuntu+mathjax.AdiumMessageStyle
  5. Go inside the directory of the new message style:
    cd ~/.local/share/adium/message-styles/ubuntu+mathjax.AdiumMessageStyle
  6. Apply the patch:
    patch -p1 < [location of the downloaded patch]
  7. Choose the new theme "Ubuntu + MathJax" in Empathy's preferences.
  8. Open a new chat tab and write some LaTeX code inside $$ ... $$ or \[ ... \]
  9. Enjoy!