connorferster / handcalcs

Python library for converting Python calculations into rendered latex.
Apache License 2.0
5.66k stars 432 forks source link

Additional brackets around equation don't hide substitution / symbolic steps #79

Open michaellisitsa opened 3 years ago

michaellisitsa commented 3 years ago

Usually placing brackets around the equation will hide any substitution. However, when there are internal brackets, the substitution is still shown.

How to reproduce image

Expected Result image

Potential Solution Could we parse the brackets to see which one matches with which. Then if the outside pair enclose the whole line (minus quotes), we can remove substitution steps. Something like this link which I've copied below:

text = 'aaaa(bb()()ccc)dd'
istart = []  # stack of indices of opening parentheses
d = {}

for i, c in enumerate(text):
    if c == '(':
         istart.append(i)
    if c == ')':
        try:
            d[istart.pop()] = i
        except IndexError:
            print('Too many closing parentheses')
if istart:  # check if stack is empty afterwards
    print('Too many opening parentheses')
print(d)

Result:

In [58]: d
Out[58]: {4: 14, 7: 8, 9: 10}

Then loop through d to see whether any match {0:len(expression)}

connorferster commented 3 years ago

Hi @michaellisitsa

Yes, I am finding that this "parentheses feature" has a tendency to create some unexpected behaviour. I am re-evaluating whether it is a good idea or not. Thank you for your code sample. I will give it a try. I am going to start implementing some fixes and features into handcalcs shortly. I really want to get this "parentheses feature" behaviour locked down.

In my experience with it, there have been times where it has been convenient for me but I find I am using it less and less, in favour of using param cells more. Would like to hear your thoughts on it. Do you find yourself using the feature often?

Thanks for your continued feedback and support!

michaellisitsa commented 3 years ago

Hi Connor, I have used the brackets very often, especially when I want to mix params only and substitutions in one Jupyter cell. It is also very handy when I just do something like 100 * u.MPa where the back-substitution is quite pointless, so I just want to silence that line. I think some sort of method to silence the back-substitution on a line by line basis is still handy, even if it doesn't involve brackets, but of course separating into separate Jupyter cells is always possible.

connorferster commented 3 years ago

@michaellisitsa Yeah, I agree. It is useful and quite handy. I am working on an update to make export templates compatible with Jupyter Lab v3.0 which I am quite excited about (custom exporter, will be able to "Export HTML with Handcalcs" from the File -> Export as... menu in Jupyter Lab).

I will add the parentheses fix to the list, also!