# Getting LaTeX code from Sage when running %display latex

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 close merge delete

2

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.

( 2020-02-28 22:44:35 +0100 )edit

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

( 2020-03-03 00:08:29 +0100 )edit

Sort by » oldest newest most voted

You should do

print(latex(eq.display()))

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.

( 2020-02-29 20:13:11 +0100 )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!

( 2020-03-03 00:07:24 +0100 )edit

Did you try

sage: latex(eq)


? Because this is usually what works in the terminal.

more

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

( 2020-03-03 00:04:40 +0100 )edit