# Translate sage input to latex without simplifying

In sage it doesn't seem possible to print latex code of an expression without simplifying it in some way. For example \sage(x/x) should translate to \frac{x}{x} instead of 1, see this question on ask.sagemath: http://ask.sagemath.org/question/9698/latex-function-prevent-simplification/

My idea to circumvent this problem would be to define the expression as a string in sage (using sagesilent in sagetex). Then I can convert it automatically to a sage expression using sage_eval. For typesetting the original formula I want to print it to the latex document and pass it to some latex command which translates it to a proper latex math expression.

So is there any latex package which can do this? If not, do you have other ideas to get this.

Here is some pseudocode which tries to make clearer what I want:

\documentclass{article}

\usepackage{sagetex}
\begin{document}
\begin{sagesilent}
var('x')
fs = '3 + x**2 + x/x'
f = sage_eval(fs, locals={'x':x})
\end{sagesilent}

\sagetotex{fs} %f(x) without simplifying anything
\sagestr{f} %prints simplified f
\sagestr{diff(f,x)} % prints derivative of f

\end{document}


That's an interesting idea. I don't think there's a package that can do this. It would make most sense that this is added to the sagetex package. Perhaps you can request here where Dan Drake has given his e-mail? Assuming I understand what you want, it's easy to implement.

\documentclass{article}
\usepackage{sagetex}
\begin{document}
\newcommand{\sagetotex}[1]{\ensuremath{{#1}}}
\begin{sagesilent}
var('x')
fs = '3 + x**2 + x/x'
f = sage_eval(fs, locals={'x':x})
\end{sagesilent}
\noindent \ensuremath{\sagestr{fs}}\\%f(x) without simplifying
$\sage{f}$\\ %prints simplified f
$\sage{diff(f,x)}$\\ % prints derivative of f
\sagetotex{\sagestr{fs}}
\end{document}


Which gives this output:

Since the string is for a function representing mathematical content a new command (which you suggested as \sagetotex) is created at the beginning of the document by insisting the argument be typeset in math mode so that all math content is typeset using the math font.

Based on my understanding of Python's (and thus, for the most part, Sage's) model for evaluating expressions, this is not possible in general without using something like you've outlined with strings and sage_eval. When you do \sage{some_expr}, Sage will run its latex() function on some_expr...but to do that, it first needs to evaluate it.

Symbolic expressions know enough about their own structure so that the hold=True feature works, but that doesn't work for arbitrary Sage/Python expressions.

Using sage_eval looks like the best way to do what you're trying to do, at least for now.