arnog / mathlive

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

[FEATURE] allow selection of content rendered with `renderMathInElement()` #443

Open hubertlepicki opened 4 years ago

hubertlepicki commented 4 years ago

MathLive has great ability to select and copy LaTeX code, when editor is used. This also works if I initialize the editor in read-only mode, which is my current workaround:

MathLive.makeMathField(el, {readOnly: true}) editor

This does not seem to work, however, if I don't use editor mode and instead use render math:

MathLive.renderMathInElement(el); math

Would adding an option to allow the selection / copy be doable? I am sorry if it's already possible and please correct me if this is the case.

If not implemented, and yet useful, I could have a look and give a shot on implementing it if given some high level instructions how to approach it :)

arnog commented 4 years ago

Thanks for the question and for offering to help with this.

If you need the formula to still be selectable so you can extract the latex out of it, you need to use the readOnly mode of a MathField.

When using renderMathInElement the formula is converted to HTML markup, but in the process of doing so the "connection" between Latex and the markup is lost. Furthermore, in order to correctly select the portions of a formula, you need to "know" about the structures of the formula (i.e. you need to know that a fraction has a numerator and denominator). That information is also lost when using renderMathInElement. A MathField in read-only mode preserves the information necessary to support the correct selection of the formula and to extract the Latex corresponding to the selection.

One possible approach would be to add a userSelect property to AutoRenderOptions, then modify renderMathInElement() to create a MathField in readOnly mode when this option is set (see the createAccessibleMarkupPair() function). Let me know if you're interested in giving this a go.

hubertlepicki commented 4 years ago

@arnog thank you and sorry for not getting back earlier to you. I think you can close this issue now.

arnog commented 4 years ago

No worries. I'm going to keep this open for now as a potential future feature.