obsidian-latex-suite icon indicating copy to clipboard operation
obsidian-latex-suite copied to clipboard

Feature Request: Faster Fraction typing

Open vreithinger opened this issue 1 year ago • 8 comments

Right now a+b/c gets translated to a+\frac{b}{c}. If one wants to write \frac{a+b}{c} one has to write {a+b}/c. To be faster, would it be possible to have a+b/c be translated to \frac{a+b}{c} but a+[space]b/c be translated to a+\frac{b}{c}? That way, the bracket typing could be avoided.

vreithinger avatar Jul 04 '22 21:07 vreithinger

Yeah, this is intended behaviour, since if I want to write $a+\frac{b}{c}$ then I can just type a+b/c; I don't have to bother adding a space after the +. I agree it's not ideal when you want to write $\frac{a+b}{c}$!

Sadly I can't think of a clean and simple solution that would allow one to quickly type both $\frac{a+b}{c}$ and $a+\frac{b}{c}$.

I can add a setting where users choose "breaking characters" that distinguish the start of a new fraction. Then, removing the + character from this list would achieve the behaviour you describe.

artisticat1 avatar Jul 04 '22 23:07 artisticat1

But wouldnt typing one space in between + and b be faster/more convenient than having to type brackets in the other case? I had a bigger idea in mind that would make typing fractions very convenient:

Whenever one types "/" everything until the most recent space gets inserted in the numerator part of the fraction: a[space]bc/ -> a\frac{bc}{[cursor]} abc/ -> \frac{abc}{[cursor]} a-[space]bc/ -> a-\frac{bc}{[cursor]} a[space]-bc/ -> a\frac{-bc}{[cursor]} As the cursor is then in the denominator part of the fraction, there is no similar ambiguity in the denominator that has to be specified, one just types whatever goes into the denominator in the brackets and tabs out (just as now). One problem arises if one wants to put a product of variables and greek letters in the numerator. Without a space in between e.g. \alpha and i (i.e. \alphai) the latex compiler throws an error. Right now, when i type @ai the plugin automatically adds a space in between \alpha and i. If i type @a[space]i, the plugin does not add another space. One could use that behaviour to fix the problem: @ai -> \alpha[u00A0]i @a[space]i -> \alpha[space]i Where [u00A0] is an unbreakable space added by the plugin. That space renders in latex (to my knowledge) just like a normal space. However, by distinguishing between the two kinds of spaces, the plugin knows if both \alpha and i should go in the numerator, or just i. For the user, it is a nobrainer they just know: if space between characters -> breaking of fraction. So for example: @ai/ -> \alpha[u00A0]i/ -> \frac{\alpha i}{[cursor]} @a[space]i/ -> \alpha[space]i/ -> \alpha \frac{i}{[cursor]} Same behaviour could be implemented for \bar or \hat. Everything until the most recent [space] goes under the bar/the hat.

That being said, i am not a pro in programming at all and have no idea if these changes are hard to implement. I just wanted to get the idea out there, your solution sounds good too. Thank you again for all the work the plugin is great.

vreithinger avatar Jul 05 '22 12:07 vreithinger

But wouldnt typing one space in between + and b be faster/more convenient than having to type brackets in the other case?

Yeah, you're completely right! Perhaps I just write $a+\frac{b}{c}$ more often than the other case. I can consider changing the default behaviour.

Whenever one types "/" everything until the most recent space gets inserted in the numerator part of the fraction...

That's an interesting idea. It does seem like it'd make typing fractions very convenient. I can think of a couple small issues though:

  • When typing @ai, a space is automatically inserted between the a and i, as you say. This is actually achieved via a snippet (as can be seen in the default snippets). So unless we force all users to press the "reset to default snippets" button, all previous users of the plugin will still have the old snippet that inserts a normal space. We want to insert a non-breaking space, but most users will be stuck with the old behaviour.
  • A space and non-breaking space would look identical to the user, but exhibit slightly different behaviours.
  • We probably want to use normal spaces in our LaTeX code.

Thank you again for all the work the plugin is great.

No problem, glad you enjoy the plugin :)

artisticat1 avatar Jul 05 '22 22:07 artisticat1

I see your point. Could it maybe become an option that one can turn on?

vreithinger avatar Jul 06 '22 10:07 vreithinger

That seems reasonable. It'd take a bit of effort to implement. Maybe in a future release!

artisticat1 avatar Jul 08 '22 15:07 artisticat1

sounds good :)

vreithinger avatar Jul 08 '22 17:07 vreithinger

Now that i have thought longer about it i think the unbreakable space approach is probably not necessary. I think it would suffice to be able to decide weather a+b/c should produce a+\frac{b}{c} or \frac{a+b}{c} via a switch in the settings.

vreithinger avatar Jul 17 '22 12:07 vreithinger

I see! I will try to implement this in one of the next releases.

artisticat1 avatar Jul 18 '22 11:07 artisticat1

I can add a setting where users choose "breaking characters" that distinguish the start of a new fraction. Then, removing the + character from this list would achieve the behaviour you describe.

This is added in 1.5.2.

artisticat1 avatar Oct 08 '22 14:10 artisticat1

Amazing :)

vreithinger avatar Oct 08 '22 17:10 vreithinger