Skip to content

How do I use %default_mode in a Sage worksheet?

William Stein edited this page Nov 2, 2017 · 5 revisions

Setting mode one cell at a time

Suppose you are using a Sage worksheet (.sagews file) in CoCalc and want to run commands in another interpreter or language such as gap or r.

You can run gap commands in a single cell by doing the following:


and you can run r commands in a cell the same way:


Using %default_mode

If most of the cells in your worksheet use something other than Sage, you can set a default_mode for the worksheet. Note:

  • The cell with the %default_mode command must be executed before the mode is in effect.
  • Start using the new mode in subsequent cells. Do not put calculation statements in the new mode in the cell that sets %default_mode.

After setting a default mode, you can temporarily override it in any cell using any of the usual %-commands.

Here's an example setting default mode, using it in two cells, and then running a cell in another mode:

# run this line by itself before running other cells
%default_mode gap

--- cell runs in gap mode---

--- another gap cell ---
a:= (9 - 7) * (5 + 6);

--- run a sage command while default mode is gap ---

Setting default mode automatically with %auto

If you make %auto the first line of a cell in a .sagews, that cell is executed whenever the worksheet is opened. A common situation when using a default mode is to set that mode automatically, as follows:

%default_mode r

Note that you will have to execute this cell manually if you have just typed it in or restart the worksheet, for it to take effect.

Find out more

For more information on .sagews modes, including how to create your own custom modes (which is surprisingly easy!), see sagews custom modes.

Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.