resultwizard / ResultWizard

Intelligent interface between Python-computed values and your LaTeX work.
https://resultwizard.github.io/ResultWizard
MIT License
7 stars 0 forks source link

Add parentheses to units in `\per` mode in console output #40

Open Splines opened 6 months ago

Splines commented 6 months ago

Consider this:

wiz.res("a911", 1.05, unit=r"\mm\s\per\N\kg").print()

will print:

aNineHundredEleven = 1.1 mm s/N kg

The unit representation might be misleading. Instead, I'd expect something like (mm s)/(N kg). This is also what siunitx is doing when users specify per-mode=symbol in the \sisetup{}.

Edit: My assumptions how siunitx parses the input were wrong in this issue. See the discussion below for how it looks like in reality, i.e. in this example, we'd expect (mm s kg) / N.

paul019 commented 6 months ago

I find this difficult as per-mode=symbol is not the default in siunitx. If we want to generate the output (mm s)/(N kg) as you proposed, we should set per-mode=symbol in results.tex.

Splines commented 6 months ago

I refer to the console output, not the output to results.tex

paul019 commented 6 months ago

I know. But both should be built upon the same logic.

Splines commented 6 months ago

But for siunitx the user should be able to decide on their own what per-mode they want to use, right? E.g. they prefer a fraction over the / delimiter.

However, in the console, we don't have a fraction available. Instead, a meaningful representation of \per with ASCII symbols is only possible with / (or provide me with a different approach if you find something better).

If the user specifies per-mode=symbol, siunitx will automatically add the parentheses if the output would be ambiguous otherwise. In the console, we should reflect this behavior, I guess. E.g. for a product in the denominator, use parentheses.

And last but not least: I don't think we should go as far as allowing customization of the printouts to the console. These printouts just serve as a first glance for the variable and are mainly intended for Jupyter notebooks. The core of ResultWizard is still the output in a siunitx-compatible format.

paul019 commented 6 months ago

I see the following problem: Depending on the mode used in siunitx, a user could mean one of the two things with A \per B C:

  1. A/(BC)
  2. (A/B)C
Splines commented 6 months ago

As far as I know, per-mode only affects how the output is printed. After the first \per, siunitx will always consider the rest to be in a grouping (your case 1).

image

Also see the documentation

paul019 commented 6 months ago

...but only if you use multiple \per's.

Splines commented 6 months ago

Ah, you're right. But I still don't get your full point I guess.

image

Here, the grouping is still consistent within all three options in this case.

siunitx apparently parses it like this: \per + <unit> is one group, e.g. A \per B C is treated like A (\per B) C. And with multiple \pers, it's the same way: A \per B \per C is treated like A (\per B) (\per C). Reflect that in the console to conform with siunitx.

Splines commented 6 months ago

Now I see the confusion: my original comment in this post is then wrong, that's true.

\mm\s\per\N\kg\per\T should be printed as mm s kg / (N T). Maybe put everything that is in the denominator after the /. There should be only one / in the console output to make things clear. If the denominator contains more than one unit specifier, put parentheses around them.

You probably have to build a small Lexer to achieve this.

Splines commented 6 months ago

maybe use the same "parentheses logic" for the numerator as well to print (mm s kg) / (N T) instead of mm s kg / (N T). That makes it better to read in my view.

paul019 commented 6 months ago

This, I don't get again. We can only do such grouping, if we analyze the user's unit string more deeply, i.e. detect whether there are multiple \per's etc.

Splines commented 6 months ago

Yes, exactly, that's what I mean with

You probably have to build a small Lexer to achieve this.

If you like, I can support you with this task as I have a tiny bit of knowledge in compiler construction

paul019 commented 6 months ago

I added a small lexer.