CoCalc Public Files15 - Writing in Sage Assignment / Writing in Sage Notes.sagewsOpen with one click!
Author: Aaron Tresham
Views : 355
Compute Environment: Ubuntu 18.04 (Deprecated)
This material was developed by Aaron Tresham at the University of Hawaii at Hilo and is Creative Commons License
licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Writing in Sage

We want to write text that includes specialized mathematical symbols. For this purpose, Sage utilizes LaTeX\LaTeX{}, a free program for typesetting technical documents. It is common in scientific fields in general and mathematics in particular. For more information about LaTeX\LaTeX{} click here.

There are two ways to use Sage and LaTeX\LaTeX{}: (1) Use LaTeX\LaTeX{} commands in a Sage worksheet to make things look nicer; (2) Use Sage commands within a LaTeX\LaTeX{} document.

We will look at the first way today. In Math 206 (Calculus 2), we will explore the second option. The advantage of option (2) is that hard copies look nicer.

Producing Text Output

To write plain text without any special characters, simply begin an input cell with %md, hit [ENTER], and then type whatever you want.

When you're done, press the green "Run" button. This will produce text output. To hide the input, click on the small triangle next to the line number to the left.

Try to produce some text in the blank input cell below.

The %md tells Sage to interpret the cell using Markdown. Markdown was designed for writing web pages - it's a shortcut for html (which Sage uses for text).

For more about Markdown click here.

Basics of Markdown

I will review a few of the high points from the web page cited above.


To make different levels of headings, precede the heading with one or more pound signs (#). The more pound signs, the lower the level of heading. [Note: for some reason the Markdown page calles these "headers" rather than "headings."]

For example, typing

# Heading Level 1

results in

Heading Level 1


#### Heading Level 4

results in

Heading Level 4

Note: There needs to be a space after the pound signs.


If you want to emphasize something (italics), use asterisks:

Input: I want to *emphasize* this.

Output: I want to emphasize this.

For stronger emphasis (bold), double the asterisks:

Input: I want to **emphasize** this.

Output: I want to emphasize this.


To make a bulleted list, use an asterisk for each item (followed by a space):

* Item 1

* Item 2

* Item 3


  • Item 1

  • Item 2

  • Item 3

To make a numbered list, use numbers followed by periods:

  1. Item 1

  2. Item 2

  3. Item 3

Editing Text

If you want to edit text output, and the input is hidden, then click on the arrow to the left of the line just above the text. This will open the input that produced the text, which you can then edit. Try it with the text below.

When you're finished, hit the "Run" button to produce the output again.

Try to edit this text.

Deleting Text Cells

If you want to delete an entire text cell, position the cursor before it and hit [DELETE] until it's gone (this will delete both the ouput and the hidden input that produced it).

You may have to create a new input cell in order to position the cursor. Try to delete the text output below.

Be careful when you delete! When you delete the horizontal gray line right before text output, Sage will delete the input that produced the text, along with the output.

If you want to delete the output and keep the input, then click on the arrow to open the input, position the cursor before the output, and hit [DELETE]. (This may merge the current input cell with the following input cell, so watch out.)

Delete this text.

Basics of LaTeX\LaTeX

Now we're ready to type some math.

Whatever you want Sage to interpret using LaTeX\LaTeX should be enclosed by dollar signs ($).

Compare f(x)=x^2+3x+2 with f(x)=x2+3x+2f(x)=x^2+3x+2.

The first is what you get if you just type plain text. The second is what you get if you type $f(x)=x^2+3x+2$.

The dollar signs tell Sage to use LaTeX\LaTeX, which typesets the formula in a much more appealing way.

Using single dollar signs produces typeset math inline. If you have an equation or formula that you want set off from the rest of the text, enclose it in double dollar signs (display mode).

For example, typing $$f(x)=x^2+3x+2$$ produces f(x)=x2+3x+2f(x)=x^2+3x+2

Miscellaneous Symbols

Here are various symbols that may be useful in math. Notice the common theme: LaTeX\LaTeX commands are preceded by a backslash (\).

To get "approximately equal to," use \approx. For example, $x\approx 20$ produces x20x\approx20.

To get the plus or minus symbol, use \pm. For example, $\pm 3$ produces ±3\pm3.

To get a Greek letter, type a backslash and write out the name of the letter. For example, $\pi$ produces π\pi and $\alpha$ produces α\alpha.

For the capital Greek letters, capitalize the first letter: $\Pi$ produces Π\Pi and $\Sigma$ produces Σ\Sigma.

[Note: If the capital Greek letter is the same as the Latin capital, then the Greek won't work. For example, $\Alpha$ will produce an error, since capital alpha is AA.]

To get \ge and \le, type $\ge$ or $\le$. To get < or >, simply use the keyboard.

To display fractions, square roots, etc., you need to know the LaTeX\LaTeX commands for producing these. Some of these are similar to Sage commands, but there are often important differences.


To produce a fraction, type $\frac{numerator}{denominator}$.

For example, typing $\frac{3}{7}$ produces 37\frac{3}{7}.

Typing $\frac{x^2+3x+9}{2x-4}$ produces x2+3x+92x4\frac{x^2+3x+9}{2x-4}.

If these fractions are too small, you can make them larger by typing \displaystyle at the beginning:

Typing $\displaystyle\frac{x^2+3x+9}{2x-4}$ produces x2+3x+92x4\displaystyle\frac{x^2+3x+9}{2x-4}.

Note that \displaystyle is not required when you use double dollar signs (display mode).

Typing $$\frac{x^2+3x+9}{2x-4}$$ produces x2+3x+92x4\frac{x^2+3x+9}{2x-4}


If you have an exponent that is only one character, you can simply type a caret. For example, typing $x^2$ produces x2x^2.

If your exponent has more than one character (whether it's 1-1, 2525, or x23x+2x^2-3x+2), enclose the exponent in curly brackets.

Compare what happens with $3^-2$ and $3^{-2}$:

323^-2 versus 323^{-2} [in the first case, only the negative symbol is in the exponent]

Here's another example. To get 3x28x+63^{x^2-8x+6} you type $3^{x^2-8x+6}$.

It may be safer to get in the habit of always using curly brackets after the caret.


To get a square root, use \sqrt{}.

For example, $\sqrt{2}$ produces 2\sqrt{2}.

And $\sqrt{3x^2-5x+1}$ produces 3x25x+1\sqrt{3x^2-5x+1}.

If you want a cube root, 4th root, etc., enclose the root number in square brackets after \sqrt.

For example, $\sqrt[3]{2}$ produces 23\sqrt[3]{2}.

And $\sqrt[8]{\frac{3x+1}{9x+2}}$ produces 3x+19x+28\sqrt[8]{\frac{3x+1}{9x+2}}.

Or $\displaystyle\sqrt[8]{\frac{3x+1}{9x+2}}$ produces 3x+19x+28\displaystyle\sqrt[8]{\frac{3x+1}{9x+2}}.


To display a limit symbol, you type \lim. To get text below the limit symbol, use an underscore followed by curly brackets.

Here's an example:

$\lim_{x\to 1}f(x)$ produces limx1f(x)\lim_{x\to 1}f(x). [Notice that \to produces the arrow.]

This usually looks better using \displaystyle:

$\displaystyle\lim_{x\to 1}f(x)$ produces limx1f(x)\displaystyle\lim_{x\to 1}f(x).

Here's another example:

$\displaystyle\lim_{h\to 0}\frac{f(x+h)-f(x)}{h}$ produces limh0f(x+h)f(x)h\displaystyle\lim_{h\to 0}\frac{f(x+h)-f(x)}{h}.

To get a limit at infinity, use \infty:

Typing $\displaystyle\lim_{x\to\infty}f(x)$ produces limxf(x)\displaystyle\lim_{x\to\infty}f(x).

Or typing $\displaystyle\lim_{x\to -\infty}f(x)$ produces limxf(x)\displaystyle\lim_{x\to -\infty}f(x).

For left and right limits, use the caret (^) to put a plus or minus sign in the right spot:

For example, $\displaystyle\lim_{x\to 1^+}f(x)$ produces limx1+f(x)\displaystyle\lim_{x\to 1^+}f(x).

And $\displaystyle\lim_{x\to 1^-}f(x)$ produces limx1f(x)\displaystyle\lim_{x\to 1^-}f(x).


There is no problem using prime notation for derivatives.

For example, type $f'(x)$ to get f(x)f'(x). [There's an apostrophe after the f - it's hard to see.]

For the second derivative, type $f''(x)$ to get f(x)f''(x).

For the third derivative, type $f'''(x)$ to get f(x)f'''(x).

[Caution: If "auto brackets" is checked in your Sage settings, then Sage may produce two single quote marks when you type the apostrophe (one to begin the quote and one to end the quote - Sage assumes you're trying to enclose something in quote marks). Make sure you have the number of "primes" you want.]

For higher order derivatives, use the caret (^).

For example, $f^{(5)}(x)$ produces f(5)(x)f^{(5)}(x). [Don't forget the curly brackets around the exponent.]

If you want to use Leibniz's notation, type ddx\frac{d}{dx} just like a regular fraction.

For example, $\frac{d}{dx}f(x)$ produces ddxf(x)\frac{d}{dx}f(x).

Here is an example for the 3rd derivative: $\frac{d^3}{dx^3}f(x)$ produces d3dx3f(x)\frac{d^3}{dx^3}f(x).

Summation Notation

We won't need this until later, but I'll include it here for reference.

To get a summation symbol, type \sum.

For example, $\sum i^2$ produces i2\sum i^2.

To add a range of values for the sum, use _ and ^.

For example, $\sum_{i=0}^{10} i^2$ produces i=010i2\sum_{i=0}^{10} i^2. [Note the curly brackets around i=0 and 10.]

This will look better with \displaystyle or double dollar signs:

i=010i2\displaystyle\sum_{i=0}^{10}i^2 from $\displaystyle\sum_{i=0}^{10}i^2$, or

i=010i2\sum_{i=0}^{10}i^2 from $$\sum_{i=0}^{10}i^2$$


To get the integral symbol, type \int.

For example, $\int f(x)$ produces f(x)\int f(x).

If you want to add the dx, you should add an extra space by typing , before dx:

$\int f(x),dx$ produces f(x)dx\int f(x)\,dx. [Without , you get f(x)dx\int f(x) dx - no space between f(x) and dx]

Here's what you get if you add \displaystyle: f(x)dx\displaystyle\int f(x)\,dx.

If you want a definite integral, you add a lower limit after an underscore and an upper limit after a caret.

For example, typing $\int_a^b f(x),dx$ produces abf(x)dx\int_a^b f(x)\,dx.

Or, with displaystyle you get abf(x)dx\displaystyle\int_a^b f(x)\,dx.

If a limit of integration is more than one character, then enclose it in curly brackets:

Typing $\displaystyle\int_{-2}^{18}f(x),dx$ produces 218f(x)dx\displaystyle\int_{-2}^{18} f(x)\, dx.

Here's what the same integral looks like in display mode (double dollar signs): 218f(x)dx\int_{-2}^{18} f(x)\, dx

Text within LaTeX\LaTeX

If you put text within dollar signs, all spaces are ignored and everything is placed in italics.

To avoid this, use \text. Compare the following:

$f(x) and g(x)$ produces f(x)andg(x)f(x) and g(x)

$f(x) \text{ and } g(x)$ produces f(x) and g(x)f(x) \text{ and } g(x) [Notice the spaces around "and"]

[Note: you can see that "and" is not the same size as other text. You may want to avoid have "and" within LaTeX\LaTeX entirely. You could type instead: $f(x)$ and $g(x)$]

Getting LaTeX\LaTeX{} Code from Sage

If you have a mathematical expression in Sage, you can convert it to LaTeX\LaTeX code using the latex(...) command.

For example, in a Sage worksheet type latex(3/4) and hit "Run." The output will be \frac{3}{4}.

Or suppose you run derivative(cos(1/x^5)) in a Sage worksheet, which produces the output 5*sin(x^(-5))/x^6.

If you want to convert this answer to LaTeX\LaTeX, simply type latex(_) in the next cell and hit "Run" [remember, _ recalls the last output; you could also copy and paste to do latex(5*sin(x^(-5))/x^6)].

Then Sage produces the output \frac{5 , \sin\left(\frac{1}{x^{5}}\right)}{x^{6}}

[You may notice that the LaTeX\LaTeX code produced by Sage is more complicated than your own, but it shouldn't be a problem.]

Please let me know if I've left off any important symbols, or if you need help with something specialized.

Of course, extensive documentation and examples are available online.

Note: If you click on the arrows next to any of the text cells in the lecture notes, you can see the code that produced it. Feel free to mimic my text.

A final remark: If you have several mathematical things together, you only need one set of dollar signs around the whole thing; you don't need dollar signs around each individual item.

For example, $f(x)=\frac{(x+1)(x+2)}{x+1}=x+2$ [just one set of dollar signs].

You do not have to type $f(x)$=$\frac{(x+1)(x+2)}{x+1}$=$x+2$