Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Hello, @perfectly_odd! I concur with @Emmanuel Charpentier in that there seems to be a direct way to "prettify". However, there are a few things that you can do in order to make your results smaller.

  1. Use sympy's latex() function: You can import sympy and call its latex() function with the root_notation option set to False. Here is an example:

    import sympy as sym
    expr = 3/7*sqrt(2)*sqrt(pi)*(5)^(8/7) + 2
    sym.latex(expr, root_notation=False)
    

    This will return $2 + \frac{15 \cdot 2^{\frac{1}{2}} \cdot 5^{\frac{1}{7}} \pi^{\frac{1}{2}}}{7}$, which is not exactly what you wanted, but at least will eliminate the radicals, making your expressions shorter.

    In order to use this with sageTeX, you can use the following LaTeX code right after your \begin{document}:

    \begin{sagesilent}
        import sympy as sym
        def prettify(expr):
            return sym.latex(expr, root_notation=False)
    \end{sagesilent}
    

    Then, you just need to use sageTeX's \sagestr{} command. For example, \sagestr(prettify(expr)) in our case. You can also this directly in your Sage/Python script.

  2. Define a couple of functions that take care of this purpose: This is quite difficult (not a surprise, considering that there is no such thing in Sage by default!) However, considering that the expressions your are working with seem to be relatively simple, this is doable. I have written a quite complicated piece of software that should (in theory) work for you. You can take a look at it here. Consider that this code is not verified and it is possible that it won't work with very complicated expressions. It has been tested just with a few examples. Although I have written a quite complicated code in order to cover any reasonably complex expression, and I have tested it with some examples that are a little bit more complex than the one in your question, there is no guarantee that I will work flawlessly. However, if you feel brave enough, feel free to use it; but test it first! It is heavily commented in case you need to modify it or really want to understand it. (Unfortunately, I don't know your level of programming experience.) Perhaps the code can be improved, but unfortunately, I didn't have more free time available to expend on this matter.

    In order to use this code, you can put it in a sagesilent environment in your LaTeX document, and call it with something like \sagestr{prettify(expr)}, where expr is some expression. Also,you can use it directly in your Sage/Python script.

  3. You could use LaTeX tweaks: If your expressions are way too large for a LaTeX table, you can use the \small command (or some of its brothers) in order to make the expressions occupy less space (this is barely noticeable in the case of \small). This can be programmed in your Sage/Python script. I would also recommend using \small for the whole table (i.e., surround your table with \bgroup\small and \egroup.) If more space is needed, use the landscape package (or any of its brothers, like lscape.) If even more space is needed, use the longtable package (or any of its brothers, like supertabular.)

I hope this helps!

Hello, @perfectly_odd! I concur with @Emmanuel Charpentier in that there seems to be a direct way to "prettify". However, there are a few things that you can do in order to make your results smaller.

  1. Use sympy's latex() function: You can import sympy and call its latex() function with the root_notation option set to False. Here is an example:

    import sympy as sym
    expr = 3/7*sqrt(2)*sqrt(pi)*(5)^(8/7) + 2
    sym.latex(expr, sym.latex(expr._sympy_(), root_notation=False)
    

    This will return $2 + \frac{15 \cdot 2^{\frac{1}{2}} \cdot 5^{\frac{1}{7}} \pi^{\frac{1}{2}}}{7}$, which is not exactly what you wanted, but at least will eliminate the radicals, making your expressions shorter.

    In order to use this with sageTeX, you can use the following LaTeX code right after your \begin{document}:

    \begin{sagesilent}
        import sympy as sym
        def prettify(expr):
            return sym.latex(expr, sym.latex(expr._sympy_(), root_notation=False)
    \end{sagesilent}
    

    The _sympy_() method is necessary to convert Sage's objects to Sympy's syntax.

    Then, you just need to use sageTeX's \sagestr{} command. For example, \sagestr(prettify(expr)) in our case. You can also this directly in your Sage/Python script.

  2. Define a couple of functions that take care of this purpose: This is quite difficult (not a surprise, considering that there is no such thing in Sage by default!) However, considering that the expressions your are working with seem to be relatively simple, this is doable. I have written a quite complicated piece of software that should (in theory) work for you. You can take a look at it here. Consider that this code is not verified and it is possible that it won't work with very complicated expressions. It has been tested just with a few examples. Although I have written a quite complicated code in order to cover any reasonably complex expression, and I have tested it with some examples that are a little bit more complex than the one in your question, there is no guarantee that I will work flawlessly. However, if you feel brave enough, feel free to use it; but test it first! It is heavily commented in case you need to modify it or really want to understand it. (Unfortunately, I don't know your level of programming experience.) Perhaps the code can be improved, but unfortunately, I didn't have more free time available to expend on this matter.

    In order to use this code, you can put it in a sagesilent environment in your LaTeX document, and call it with something like \sagestr{prettify(expr)}, where expr is some expression. Also,you can use it directly in your Sage/Python script.

  3. You could use LaTeX tweaks: If your expressions are way too large for a LaTeX table, you can use the \small command (or some of its brothers) in order to make the expressions occupy less space (this is barely noticeable in the case of \small). This can be programmed in your Sage/Python script. I would also recommend using \small for the whole table (i.e., surround your table with \bgroup\small and \egroup.) If more space is needed, use the landscape package (or any of its brothers, like lscape.) If even more space is needed, use the longtable package (or any of its brothers, like supertabular.)

I hope this helps!