Ask Your Question

Getting LaTeX code from Sage when running %display latex

asked 2020-02-28 18:40:02 +0200

thethinker gravatar image

updated 2020-02-28 19:01:12 +0200

I typically use

%display latex

at the beginning of all of my worksheets, so that the equations are rendered in LaTeX (when using .display(), for example). However, now I want to copy some of these equations into a LaTeX document, and I can't figure out how to "revert" them back to code. All my view(eq), print(eq), latex(eq.display()) all seem to spit out the rendered tex, or sometimes information about the object, like "rank 2 tensor g".

Is there a way I can do this without removing the display command at the beginning and re-running the sheet?

EDIT: Maybe more info is needed. I'm running Jupyter notebooks, Sage 8.7 on a linux machine. I'm pretty sure I used the Ubuntu binaries in this case to install.

edit retag flag offensive close merge delete



Right click on any equation to open the MathJax contextual menu. Choose Show Math As and then TeX Commands. This opens a small window with the LaTeX source used to render the equation. You can copy the contents of this window.

By the way, to revert the effect of %display latex, you can use %display default or %display plain. However, you then need to reevaluate the cells.

Juanjo gravatar imageJuanjo ( 2020-02-28 22:44:35 +0200 )edit

I marked below as answer, but this totally works as well.

thethinker gravatar imagethethinker ( 2020-03-03 00:08:29 +0200 )edit

2 Answers

Sort by » oldest newest most voted

answered 2020-02-29 16:27:35 +0200

eric_g gravatar image

You should do

edit flag offensive delete link more


Checked on 9.1.beta5 (Python 3-based).

A much better (and lazier !) solution is to type your Sage code directly in your \LaTeX source and use SageTeX to weave your computation results in your text. This guarantees that the results you get in your output are the result of the execution by Sage of your exact input, with no possible transcription error.

For more on this not unimportant subject, Google reproducible research. SageTeX is but one solution to this problem. I use more and more org-mode's babel mode which nicely complements emacs's possibilities.

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2020-02-29 20:13:11 +0200 )edit

I see the reproducibility argument, and have never seen not one single paper in my field that comes even remotely close to that. Maybe I will be the first!

thethinker gravatar imagethethinker ( 2020-03-03 00:07:24 +0200 )edit

answered 2020-02-29 15:19:10 +0200

Sébastien gravatar image

updated 2020-03-10 08:43:52 +0200

Did you try

sage: latex(eq)

? Because this is usually what works in the terminal.

edit flag offensive delete link more


Nope, this just spits back the exact same thing as "eq" by itself (pretty rendered latex)

thethinker gravatar imagethethinker ( 2020-03-03 00:04:40 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2020-02-28 18:40:02 +0200

Seen: 476 times

Last updated: Mar 10 '20