# Using LaTeX in Sage

## 2729 days ago by kcrisman

First things first.  Do you remember how to open a new text cell?  Do you remember how to edit this one?

Here is a nasty integral - so nasty that Sage can't even compute it partially.

integrate(sin(x^2)/e^(2^x^2),x)
 integrate(e^(-2^(x^2))*sin(x^2), x) integrate(e^(-2^(x^2))*sin(x^2), x)

How can I insert this in my text, though?  For that, we just need dollar signs and the latex() command.

latex(integrate(sin(x^2)/e^(2^x^2),x))
 \int e^{\left(-2^{\left(x^{2}\right)}\right)} \sin\left(x^{2}\right)\,{d x} \int e^{\left(-2^{\left(x^{2}\right)}\right)} \sin\left(x^{2}\right)\,{d x}

This looks forbidding!  But let's do it.

\$\int e^{\left(-2^{\left(x^{2}\right)}\right)} \sin\left(x^{2}\right)\,{d x}\$

\int e^{\left(-2^{\left(x^{2}\right)}\right)} \sin\left(x^{2}\right

If I cut and paste that in this cell, I get:

$\int e^{\left(-2^{\left(x^{2}\right)}\right)} \sin\left(x^{2}\right)\,{d x}$

Wow!

Such mathematical typesetting is the beginnings of what we call LaTeX (pronounced "lah-tech" or "lay-tech", hard ch).  LaTeX is one of the best ways to do mathematically typesetting, and is very powerful.

• Today, we'll see how to do math itself in LaTeX.
• Next time, we'll start seeing how to organize thoughts using LaTeX.

But it is nice that Sage can do the hard part for us to make it look nice.

f(x) = e^(-x^2+sin(abs(x)))*sinh(x^2+1) latex(f(x)); latex(derivative(f(x)))
 e^{\left(-x^{2} + \sin\left({\left| x \right|}\right)\right)} \sinh\left(x^{2} + 1\right) {\left(\frac{x \cos\left({\left| x \right|}\right)}{{\left| x \right|}} - 2 \, x\right)} e^{\left(-x^{2} + \sin\left({\left| x \right|}\right)\right)} \sinh\left(x^{2} + 1\right) + 2 \, x e^{\left(-x^{2} + \sin\left({\left| x \right|}\right)\right)} \cosh\left(x^{2} + 1\right) e^{\left(-x^{2} + \sin\left({\left| x \right|}\right)\right)} \sinh\left(x^{2} + 1\right) {\left(\frac{x \cos\left({\left| x \right|}\right)}{{\left| x \right|}} - 2 \, x\right)} e^{\left(-x^{2} + \sin\left({\left| x \right|}\right)\right)} \sinh\left(x^{2} + 1\right) + 2 \, x e^{\left(-x^{2} + \sin\left({\left| x \right|}\right)\right)} \cosh\left(x^{2} + 1\right)

The derivative of $e^{\left(-x^{2} + \sin\left({\left| x \right|}\right)\right)} \sinh\left(x^{2} + 1\right)$ is ${\left(\frac{x \cos\left({\left| x \right|}\right)}{{\left| x \right|}} - 2 \, x\right)} e^{\left(-x^{2} + \sin\left({\left| x \right|}\right)\right)} \sinh\left(x^{2} + 1\right) + 2 \, x e^{\left(-x^{2} + \sin\left({\left| x \right|}\right)\right)} \cosh\left(x^{2} + 1\right)$.

Hmm, maybe those are long enough that they need their own lines.  If I use \$\$ on each side, I get that.  Like \$\$x^2\$\$ becomes $$x^2\; .$$

e^{\left(-x^{2} + \sin\left({\left| x \right|}\right)\right)} \sinh\left(x^{2} + 1\right)

e^{\left(-x^{2} + \sin\left({\left| x \right|}\right)\right)} \sinh\left(x^{2} + 1\right)
{\left(\frac{x \cos\left({\left| x \right|}\right)}{{\left| x \right|}} - 2 \, x\right)} e^{\left(-x^{2} + \sin\left({\left| x \right|}\right)\right)} \sinh\left(x^{2} + 1\right) + 2 \, x e^{\left(-x^{2} + \sin\left({\left| x \right|}\right)\right)} \cosh\left(x^{2} + 1\right)

The derivative of $$e^{\left(-x^{2} + \sin\left({\left| x \right|}\right)\right)} \sinh\left(x^{2} + 1\right)$$ is $${\left(\frac{x \cos\left({\left| x \right|}\right)}{{\left| x \right|}} - 2 \, x\right)} e^{\left(-x^{2} + \sin\left({\left| x \right|}\right)\right)} \sinh\left(x^{2} + 1\right) + 2 \, x e^{\left(-x^{2} + \sin\left({\left| x \right|}\right)\right)} \cosh\left(x^{2} + 1\right)$$.

Note how there is a trailing period, because I did like \$\$x^2\$\$. instead of \$\$x^2 .\$\$.  We'll get there!

But there is a lot more we can do.  So let's play around a little - try to figure out what you want to typeset, maybe from some of your "exploration" problems (!) and then we'll get going.

A few helpful links we'll probably look at.

• Dana Ernst has a nice variation on others' pages here
• This is the "short course" version of the book that actually helps you learn LaTeX.  It has a very comprehensive symbol table in the appendix.
• ScribTeX is a new place you can do documents without having to download anything.
• Though you'll probably want to eventually download something, like MikTeX/TeXnicCenter on Windows or TeXShop on Mac
• Two students also did a great Math Forum on this last year.