To enter Embedded mode, position the Emacs point (cursor) on a
formula in any buffer and press M-# e (calc-embedded
).
Note that M-# 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 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:
See section 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 moves forward or backward (respectively) by that many lines to find the other end. 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 scans 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. M-# w
(calc-embedded-word
) is equivalent to C-u M-# e.
When you enable Embedded mode for a formula, Calc reads the text
between the delimiters and tries to interpret it as a Calc formula.
It's best if the current Calc language mode is correct for the
formula, but 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 M-# e or M-# 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 M-# o if you find you need to see the entire stack.
For example, typing M-# 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 M-# 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 M-# o).
Typing M-# 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 M-# 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 M-# 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.