Make typing mathematical equations in LaTeX easier with Emacs’ Calc

(This is a quick draft note, I will update it later.)

If you type mathematical equations in the excellent Emacs editor a lot (e.g., in AucTeX mode or Org-mode), you may find the Calc package very useful. There is a minor-mode in Calc called embedded mode, in which you can use Calc in any other major mode and operate directly on the mathematical text inside that document. There are a lot of things you can do: you can do calculation, you can take derivatives or integrals, you can even draw graphs. However, the feature that I use the most is for converting between languages. Calc understands mathematical equations in various languages, including natural language, big natural language (I call this “pretty-printing language”), TeX/LaTeX, C programming language…  The basic procedure to use the embedded mode is:

• In your document, move the cursor to the inside of a mathematical equation. The equation should be bounded by blank lines or some LaTeX-like delimiters. Press C-x * e to enter the embedded mode.
• From now on, all keys you type will be interpreted by Calc. Calc will update your document, in real time, with the current top-of-stack value/expression.
• After you finish, press C-x * e again to stop the embedded mode.

To change the language, press d, then press the shortcut character of the language: N for normal language, L for LaTeX, T for TeX, C for C language, B for big normal language, etc. The new language will be reflected on the representation immediately.

For example, I want to implement the code y := sqrt(x^3 + alpha*x) in C (note that := means assignment), but also want to write my comment in LaTeX. First, I type the above equation. Then I press C-x * d to duplicate the equation and start the embedded mode. Then I type d C to convert the expression into C code, which changes the second expression to: y = sqrt(pow(x, 3) + alpha*x). I then quit the embedded mode (C-x * e) and move up to the first equation, press C-x * e to enter embedded mode again, then d L to convert to LaTeX code: y = \sqrt{x^3 + \alpha x}.

There are a lot more you can do with Calc and its embedded mode.