latex expressions as symbol stings?

219 views
Skip to first unread message

Carsten Knoll

unread,
Feb 20, 2012, 9:30:37 AM2/20/12
to sy...@googlegroups.com
Hi,

I am writing a script in which I generate the an expression. This
expression I want to use 1) with preview and 2) with lambdify.


Lets say

expr = phidd**2

And I want preview to use the latex code '\ddot{\varphi}' for rendering
of phidd. Therfore I defined:

phidd = sp.Symbol(r'\ddot{\varphi}').


However, this causes problems when I call

sp.lambdify(phidd, expr)


File "<string>", line 1
lambda \ddot{\varphi}: (\ddot{\varphi}**2)
^
SyntaxError: unexpected character after line continuation character

A manual workarround would be to substitute the critical symbol with
some uncritical one.

However Im wondering if there is another (cleaner) way.

I would like to use the code in a tutorial session, therefore I would
like minimize the ballast.


Best regards,
Carsten

Aaron Meurer

unread,
Feb 20, 2012, 3:25:55 PM2/20/12
to sy...@googlegroups.com
The easiest way to do this is to use the symbol_names flag that was
recently added to latex(). I think this may have been added since
0.7.1, so you'll need to use a development version to get it (see
https://github.com/sympy/sympy/wiki/getting-the-bleeding-edge). With
that, you can get:

In [462]: latex(phidd**2, symbol_names={phidd:r'\ddot{\varphi}'})
Out[462]: \ddot{\varphi}^{2}

If you don't want to type that each time, you can define the dict
before hand and do latex(expr, symbol_names=translation_dict), or if
you just want to make things fast for an interactive demo, you can
redefine latex at the beginning as

def latex(*args, **kwargs): # or def my_latex if you don't want to override it
kwargs['symbol_names'] = {phidd:r'\ddot{\varphi}'}
return sympy.latex(*args, **kwargs)

which will make things a little more confusing, but will allow for
faster typing (so it depends on what your goal is).

If you're using automatic latex in the IPython notebook or qtconsole,
you'll have to modify the sympy profile.

Aaron Meurer

> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to
> sympy+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/sympy?hl=en.
>

Carsten Knoll

unread,
Feb 21, 2012, 4:49:14 AM2/21/12
to sy...@googlegroups.com
Hi Aaron,


On 02/20/2012 09:25 PM, Aaron Meurer wrote:
> The easiest way to do this is to use the symbol_names flag that was
> recently added to latex(). I think this may have been added since
> 0.7.1, so you'll need to use a development version to get it (see
> https://github.com/sympy/sympy/wiki/getting-the-bleeding-edge). With
> that, you can get:
>
> In [462]: latex(phidd**2, symbol_names={phidd:r'\ddot{\varphi}'})
> Out[462]: \ddot{\varphi}^{2}

that is the solution I was looking for. Thank you.

However, as far as I see, there is no possibility to pass the
symbol_names argument to preview.

https://github.com/ness01/sympy/blob/master/sympy/printing/preview.py#L114


I think it should not be hard to modify the preview signature from

def preview(expr, output='png', viewer=None, euler=True):

to

def preview(expr, output='png', viewer=None, euler=True, **latex_settings):


pass these settix to the latex function.

Would that make sense?

Carsten.

Aaron Meurer

unread,
Feb 21, 2012, 5:27:31 AM2/21/12
to sy...@googlegroups.com
Yes, that's a great idea. Can you submit a pull request implementing it?

Aaron Meurer

Carsten Knoll

unread,
Feb 21, 2012, 8:45:10 AM2/21/12
to sy...@googlegroups.com
On 02/21/2012 11:27 AM, Aaron Meurer wrote:
> Yes, that's a great idea. Can you submit a pull request implementing it?
>
> Aaron Meurer

I'll try it in the next days.

in printing/tests there is no file covering the preview module. So I
think it will not be necessary to implement a test.


Carsten.

Aaron Meurer

unread,
Feb 21, 2012, 12:10:24 PM2/21/12
to sy...@googlegroups.com
Great! Let us know if you have any questions.

Aaron Meurer

Reply all
Reply to author
Forward
0 new messages