arnog / mathlive

A web component for easy math input
https://cortexjs.io/mathlive
MIT License
1.56k stars 277 forks source link

Unnecessary space after comma decimal separator #2210

Closed truebluepl closed 9 months ago

truebluepl commented 9 months ago

Current behaviour.

Typing comma puts space after it and before next cypher.

Expected behaviour.

There shouldn't be space after comma decimal separator.

arnog commented 9 months ago

I can't really tell what you're talking about, but if you're talking about "1,2", according to LaTeX rules, there is indeed a space before and after the comma. If you want to use it as a decimal separator, you should use "1{,}2". You can also change the decimal separator in MathLive. See https://cortexjs.io/mathlive/guides/customizing/#decimal-marker

truebluepl commented 9 months ago

Yes, I'm talking about 1,2. I use comma as decimal separator by setting MathfieldElement.decimalSeparator = ",". So even if it set, space is added after comma?

1{,}2 gives me 2 (["Multiply", 1, "Nothing", "Nothing", 2]) when evaluated in ComputeEngine.

arnog commented 9 months ago

It's working for me. Here's what I get after setting MathfieldElement.decimalSeparator = ",":

Screenshot 2023-12-12 at 08 03 15

The MathJSON is also correct:

Screenshot 2023-12-12 at 08 03 24

I suspect that the code to change the decimal format is not being executed.

Could you output to the console the value of MathfieldElement.decimalSeparator before evaluating with the compute engine?

Also, if you select the formula and press ESC you should see this:

Screenshot 2023-12-12 at 08 04 50

truebluepl commented 9 months ago

I'm sorry, I had latex "," property set as keycup, not label "[,]". Now there is no space.

But underlying values are: 3{,}14 - .value ['Multiply', 3, 'Nothing', 'Nothing', 14] - ce.parse(expr).json 42 - .N().value

How did you get proper 3.1415 value?

arnog commented 9 months ago

I don't know where you got the ce instance from. Did you set it to use a comma as a decimal separator?

MathfieldElement.decimalSeparator = ",";
console.log(mf.expression.value);
truebluepl commented 9 months ago

Ok, here is the code:

<!doctype html>
<html lang="en" class="no-js">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<script defer src="https://unpkg.com/mathlive"></script>
<script defer src="https://unpkg.com/@cortex-js/compute-engine"></script>
<meta name="viewport" content="initial-scale=1, width=device-width" />
</head>

<body>
<math-field id="mf"></math-field>
<button id="button">Console</button>

<script>
window.addEventListener('DOMContentLoaded', function() {

  MathfieldElement.decimalSeparator = ",";

  const ce = new ComputeEngine.ComputeEngine();

  document.getElementById('button').addEventListener('click', function(){
    const mf=document.getElementById('mf');
    console.log(mf.value);
    console.log(ce.parse(mf.value).simplify().latex);
    console.log(ce.parse(mf.value).N().latex);
  });
});

</script>
</body>
</html>
arnog commented 9 months ago

Oh, wow, thank you! This is so much easier when I can read the code that is giving you trouble rather than having to guess.

In this case you are using a custom instance of the Compute Engine. It's not clear to me why. You could use ce = MathfieldElement.computeEngine; that is the instance of Compute Engine shared by the mathfields.

When you set the decimal separator of the mathfield, the mathfield also updates its Compute Engine instance so that this compute engine instance knows how to interpret decimal separators.

Since you're using a custom Compute Engine instance, it defaults to interpreting "." as the decimal separator. You need to configure your custom Compute Engine with:

ce.latexOptions.decimalMarker = '{,}';

Or you could just use ce = MathfieldElement.computeEngine.

truebluepl commented 9 months ago

Yes! Now it's clear and works properly. Thank you for your support and time:)

truebluepl commented 9 months ago

There is something strange in the field while typing comma. When you type 1 comma 2 using virtual keyboard, there is no space after comma - which is OK. Deleting all using backspace or selecting all and deleting, and typing 1 comma 2 using physical keyboard, causes no space after comma.

arnog commented 9 months ago

Sorry, I don't follow. What seems to be the problem?

truebluepl commented 9 months ago

After typing comma using virtual keyboard there is no space after typed comma from physical keyboard. Steps to reproduce:

  1. Type 1,2 using physical keyboard - there is space after comma (OK).
  2. Delete typed string from field (do not refresh page).
  3. Type 1,2 using virtual keyboard - there is no space after comma (OK).
  4. Same step as 2nd.
  5. Type 1,2 using physical keyboard - there is no space after comma (only if comma is between cyphers), why?
arnog commented 9 months ago

I can't reproduce this. Is that something you're seeing when using https://cortexjs.io/mathlive/demo/? Or is that with a mathfield you've created? If so, what configuration are you using with this mathfield?

What is the layout of your keyboard?

At step (3), you press the comma keycap that's on the Symbols keyboard or somewhere else?

Screenshot 2023-12-18 at 11 25 22

I don't understand why you would expect not to have a space after the comma at step (3). It should be the same as step (1).

truebluepl commented 9 months ago

Ok. My fault was setting decimal separator while activating virtual keyboard (inside onClick event). That was the cause. So space after comma at first typing from physical keyboard is not standard behaviour. I thought when typing from physical keyboard space is added always, and from virtual there is no space.