-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new translate function to include accents and other latex macros in symbol names #2488
Conversation
Looks good in principle, but please add tests. |
Sorry, I'm very new to all this. By "add tests" do you mean doctest? If so, does my last commit look sufficient? |
No, he also means tests in a test file. |
In other words, test_latex.py. By the way, it would be awesome to see this done for Unicode pretty printing as well. |
Okay, I think I've added a fairly thorough test, which even seems to pass! As for the pretty printer, I'd be happy to help out if it's as easy as this one, and if someone could point me to the right place. But having just looked at it for the first time, I'm totally lost in the pretty printer code... |
Tests pass, but not the doctest. I think you just need to turn the docstring into a raw string.
|
Sorry about that. I didn't know how to run the doctest myself. But Travis CI now reports that it passes. |
|
I guess you should check if the |
Or possibly some of the accents use the combining features of Unicode. |
We don't need |
|
The accent code should also check that it isn't matching the whole symbol name, e.g. |
I agree with your last two comments, and I'll work on them ASAP (next week). As for Regarding "Accents" probably wasn't the best choice of word; I just stole it from the |
No, it doesn't need to block on the Unicode stuff. If you don't want to do that, just open an issue for it. |
I'm not convinced about abs. Couldn't you just do a substitution of your In general, if you want to represent something in SymPy, you're encouraged to represent it as it actually is, i.e., use |
I would agree with you if variables were only ever rvalues, but my variable is almost exclusively an lvalue; I never actually need |
…tly matching modifiers; add related tests
I've made the two changes jrioux suggested about not clobbering other symbols or returning empty accents, and added associated tests to ensure those work as expected. But, as per my comment above, I'm doubling down on my notion that more general things like I'll just open an issue for the Unicode version, because I can't find an easy way into that code. But I will keep looking at it. |
The unicode pretty printer is admittedly much more complicated than the other printers, but I think all you need to do is define a function in pretty_symbology that converts a symbol to the accented version of it (or if you end up just having to write these out manually as dictionaries because there's nothing in |
Actually, I guess the function you need to modify is |
I'm not sure if it makes sense to strip the modifiers if we have no faces. I guess it's OK. I don't have a strong opinion either way. Could you add a test or two for multiple modifiers? We should figure out a way to document this. Can we just import the dictionary into Sphinx? If it looks ugly, maybe create a special variable that is just the keys of the dictionary and import that. |
This looks like a bug In [9]: Symbol('x_dot')
Out[9]: x_dot__
In [10]: Symbol('x^dot')
Out[10]: x___dot Probably it those should give the same thing as |
I agree about the faces issue. I've been going back and forth about it, but I think I've settled on not translating them, just because it removes information. People might legitimately have two variables, like |
I've fixed the extra-underscores bug. But I think |
I've added a couple little tests for multiple modifiers (including a couple doozies from the latex tests). Here's a screenshot from my terminal showing that certain combinations don't come out right. The first and last have their second modifiers shifted way left. But the second and third look just fine. I don't think that's something I can fix, but it's something to be aware of. |
I get the same behavior. What OS are you on? By the way, xvechat works. Maybe there is a good ordering here. Another question, what is the expected behavior for multiname symbols, like |
xhatvec works in Terminal.app but not in iTerm 2. So I guess it's a bug in iTerm 2. The brevecheck one doesn't work in either. |
If you're not on Linux, someone should check there. From what I've seen, the Unicode support in the Linux terminal emulators can be pretty bad. I wouldn't be surprised if this does strange things there. By the way, |
I use OS X Terminal.app. I agree that it's just a bug in the unicode implementations of the terminals. I got similar badness from just For |
I opened https://code.google.com/p/iterm2/issues/detail?id=2639 in the iTerm tracker, so hopefully it will get fixed there (unless it ends up being a Mac OS X rendering issue). |
This looks good to me. I would still like to see some testing on Linux. If things end up being really bad there we may need to add some kind of option to disable this. By the way, another idea would be to make this work for |
Oh, and I couldn't think of any, but people will likely come up with unfortunate coincidences of words that are combinations of these characters (like |
Oh yeah. Look at that. I get the same results as David on OS X Terminal. I usually use Source Code Pro. I guess there's really nothing we can do about that, but it would definitely be worth mentioning in the docs. I didn't do an exhaustive search, but I find that -- in addition to the Lucida Console Semi-Condensed that seems to work, judging from David's screenshot -- Andale Mono, Courier, and Monaco are other fixed-width fonts that also work pretty well. I tested it with: from sympy.printing.pretty.pretty_symbology import modifier_dict
[Symbol('x'+key1+key2) for key1 in modifier_dict for key2 in modifier_dict] Given this potential for ugly results if someone can't or won't use a "good" unicode font, as well as the issue of |
I guess you're right. I use DejaVu Sans Mono, but my output is a little different, but I think it might be because my ASCII font is Menlo, so that may be interfering. |
I would add the option to the printer, and for init_printing just implement https://code.google.com/p/sympy/issues/detail?id=3612. |
Any more comments? I'm fine with merging this as-is. By the way, the @sympy/mechanics may be interested in the ability to print symbols with a dot over them in the terminal. |
I'm happy with it. I won't have any time to work on the |
Add new translate function to include accents and other latex macros in symbol names
Thanks for the contribution! |
I opened https://code.google.com/p/sympy/issues/detail?id=4054 about the option. |
My pleasure. Thanks for your help improving it! |
This commit adds the ability to create more complex symbol names with accents, bolds, etc., and still have them translated into reasonable latex on printing. This is inspired by the old
latex_ex
function from thegalgebra
submodule, from the current release version, but not the dev version. For example, some times I want the vectorL
and its corresponding unit vector to both be defined, and possibly even distinguished from some scalarL
. I can do something like the following:When these are printed to latex, they will just be strings of italic letters. The alternative would be something like
But when these are printed to non-latex things, this comes out looking terrible. For example, with the
ccode
printer, this would give invalid C code.With this patch, a symbol name like
Lhat
gets converted to\hat{L}
when printed to latex, without interfering with other sympy functions. So this patch gives the best of both worlds for latex and ccode printing, for example.The list of recognized macros is stored in the new
accent_keys
list. Multiple accents can be combined, as in the symbol nameLhatdot
, which becomes\dot{\hat{L}}
in latex, whileLdothat
becomes\hat{\dot{L}}
. Also, because some people like CamelCase variables, names likeLDotHat
are processed appropriately. (In fact, any form of capitalization, such as all caps, is accepted.)