mathlive icon indicating copy to clipboard operation
mathlive copied to clipboard

[Feature]: "latex-full" output format

Open MatousAc opened this issue 2 years ago • 0 comments

Feature Description

The getValue() function described here, returns the value of a mathfield. You can specify any of the output formats documented here. This feature request is for a new output format which can be called "latex-full" or "latex-invalid" (open to naming suggestions). This format would be similar to the default "latex" format, but would not ignore parent atoms in partial selections.

What this means exactly:

One of the cool things you can do with getValue() is specify a range if you want only a part of the mathfield's value: mf.getValue(0, 16, "latex"). This is obviously useful in many contexts. Note that the second argument, 16, specifies a caret position in the mathfield. As demonstrated in this CodePen when you get a portion of your mathfield's value, "partially-selected" atoms are ignored. So if you select up to the 18 in the fraction below, you won't see the \frac{...}{ in the LaTeX returned by mf.getValue(0, 16, "latex"). image

let mf = document.querySelector("math-field")
console.log(mf.getValue(0, 16, "latex"))  // "2^3-12+13+218"
console.log(mf.value.substring(0, 16))    // "2^3-12+\frac{13+"

What the new output format, "latex-full", would do is output invalid LaTeX so that partial selections of atoms would include things like \frac{...}{. So in the example above you could do this with the new output format:

let mf = document.querySelector("math-field")
console.log(mf.getValue(0, 16, "latex-full"))  // "2^3-12+\frac{13+2}{18"

16 would be the caret offset, so the output could contain more than 16 characters.

Why would I ever want to do this?

As discussed in issue #1865, this is useful if you want to tell what part of a mathfield a user clicks on. Maybe when they double-click you want to change the color of the text around what they clicked on. In order to do this, you have to manipulate the LaTeX value of the mathfield. To do this properly you need to know where in the LaTeX source the user clicked. You can do part of the job with mf.offsetFromPoint() (documented here), but you can't reliably convert a caret position into an index within the LaTeX source code without this new output format.

This CodePen demonstrates this issue a bit better: offsetToPosition

MatousAc avatar Mar 24 '23 20:03 MatousAc