Next: , Previous: Embedded Mode, Up: Embedded Mode

16.1 Basic Embedded Mode

To enter Embedded mode, position the Emacs point (cursor) on a formula in any buffer and press C-x * e (calc-embedded). Note that C-x * e is not to be used in the Calc stack buffer like most Calc commands, but rather in regular editing buffers that are visiting your own files.

Calc will try to guess an appropriate language based on the major mode of the editing buffer. (See Language Modes.) If the current buffer is in latex-mode, for example, Calc will set its language to LaTeX. Similarly, Calc will use TeX language for tex-mode, plain-tex-mode and context-mode, C language for c-mode and c++-mode, FORTRAN language for fortran-mode and f90-mode, Pascal for pascal-mode, and eqn for nroff-mode (see Customizing Calc). These can be overridden with Calc's mode changing commands (see Mode Settings in Embedded Mode). If no suitable language is available, Calc will continue with its current language.

Calc normally scans backward and forward in the buffer for the nearest opening and closing formula delimiters. The simplest delimiters are blank lines. Other delimiters that Embedded mode understands are:

  1. The TeX and LaTeX math delimiters ‘$ $’, ‘$$ $$’, ‘\[ \]’, and ‘\( \)’;
  2. Lines beginning with ‘\begin’ and ‘\end’ (except matrix delimiters);
  3. Lines beginning with ‘@’ (Texinfo delimiters).
  4. Lines beginning with ‘.EQ’ and ‘.EN’ (eqn delimiters);
  5. Lines containing a single ‘%’ or ‘.\"’ symbol and nothing else.

See Customizing Embedded Mode, to see how to make Calc recognize your own favorite delimiters. Delimiters like ‘$ $’ can appear on their own separate lines or in-line with the formula.

If you give a positive or negative numeric prefix argument, Calc instead uses the current point as one end of the formula, and includes that many lines forward or backward (respectively, including the current line). Explicit delimiters are not necessary in this case.

With a prefix argument of zero, Calc uses the current region (delimited by point and mark) instead of formula delimiters. With a prefix argument of C-u only, Calc uses the current line as the formula.

The C-x * w (calc-embedded-word) command will start Embedded mode on the current “word”; in this case Calc will scan for the first non-numeric character (i.e., the first character that is not a digit, sign, decimal point, or upper- or lower-case ‘e’) forward and backward to delimit the formula.

When you enable Embedded mode for a formula, Calc reads the text between the delimiters and tries to interpret it as a Calc formula. Calc can generally identify TeX formulas and Big-style formulas even if the language mode is wrong. If Calc can't make sense of the formula, it beeps and refuses to enter Embedded mode. But if the current language is wrong, Calc can sometimes parse the formula successfully (but incorrectly); for example, the C expression ‘atan(a[1])’ can be parsed in Normal language mode, but the atan won't correspond to the built-in arctan function, and the ‘a[1]’ will be interpreted as ‘a’ times the vector ‘[1]’!

If you press C-x * e or C-x * w to activate an embedded formula which is blank, say with the cursor on the space between the two delimiters ‘$ $’, Calc will immediately prompt for an algebraic entry.

Only one formula in one buffer can be enabled at a time. If you move to another area of the current buffer and give Calc commands, Calc turns Embedded mode off for the old formula and then tries to restart Embedded mode at the new position. Other buffers are not affected by Embedded mode.

When Embedded mode begins, Calc pushes the current formula onto the stack. No Calc stack window is created; however, Calc copies the top-of-stack position into the original buffer at all times. You can create a Calc window by hand with C-x * o if you find you need to see the entire stack.

For example, typing C-x * e while somewhere in the formula ‘n>2’ in the following line enables Embedded mode on that inequality:

     We define $F_n = F_(n-1)+F_(n-2)$ for all $n>2$.

The formula ‘n>2’ will be pushed onto the Calc stack, and the top of stack will be copied back into the editing buffer. This means that spaces will appear around the ‘>’ symbol to match Calc's usual display style:

     We define $F_n = F_(n-1)+F_(n-2)$ for all $n > 2$.

No spaces have appeared around the ‘+’ sign because it's in a different formula, one which we have not yet touched with Embedded mode.

Now that Embedded mode is enabled, keys you type in this buffer are interpreted as Calc commands. At this point we might use the “commute” command j C to reverse the inequality. This is a selection-based command for which we first need to move the cursor onto the operator (‘>’ in this case) that needs to be commuted.

     We define $F_n = F_(n-1)+F_(n-2)$ for all $2 < n$.

The C-x * o command is a useful way to open a Calc window without actually selecting that window. Giving this command verifies that ‘2 < n’ is also on the Calc stack. Typing 17 <RET> would produce:

     We define $F_n = F_(n-1)+F_(n-2)$ for all $17$.

with ‘2 < n’ and ‘17’ on the stack; typing <TAB> at this point will exchange the two stack values and restore ‘2 < n’ to the embedded formula. Even though you can't normally see the stack in Embedded mode, it is still there and it still operates in the same way. But, as with old-fashioned RPN calculators, you can only see the value at the top of the stack at any given time (unless you use C-x * o).

Typing C-x * e again turns Embedded mode off. The Calc window reveals that the formula ‘2 < n is automatically removed from the stack, but the ‘17’ is not. Entering Embedded mode always pushes one thing onto the stack, and leaving Embedded mode always removes one thing. Anything else that happens on the stack is entirely your business as far as Embedded mode is concerned.

If you press C-x * e in the wrong place by accident, it is possible that Calc will be able to parse the nearby text as a formula and will mangle that text in an attempt to redisplay it “properly” in the current language mode. If this happens, press C-x * e again to exit Embedded mode, then give the regular Emacs “undo” command (C-_ or C-x u) to put the text back the way it was before Calc edited it. Note that Calc's own Undo command (typed before you turn Embedded mode back off) will not do you any good, because as far as Calc is concerned you haven't done anything with this formula yet.