Math operators as objects, subscript rendering, and a mild copy-pasting bug

Hello everyone,

This post is about how Texmacs deals with math operators like sup, inf, exp and such.

I would like to express my belief that it is slightly anti-intuitive (this is an opinion) and also highlight an actual bug.

Background and examples

LaTeX wants operators like \sup, \inf, and \lim to be macros. Texmacs does not encode these operators as macros: to input sup you just type the letters that spell sup in math mode.

However, Texmacs does have a bunch of special logic to deal with these operators.

Example 1:

For example, start by writing u p _ t in math mode. You get
image
Now move your cursor to the beginning
image
and type an s:
image

VoilĂ : Texmacs recognizes that sup is special and its indexes should be rendered appropriately.

Here is a comparison with the source code:
image
with source given by
image

Example 2:

Other interesting logic is built into the exporter to LaTeX and into hybrid input mode. When exporting to LaTeX, Texmacs detects that sup is special and converts it to a LaTeX macro. When using hybrid mode, you can try to input a non-existent macro e.g. by typing \ t e s t + <RET>
to obtain
image
and after pressing <RET> you get
image
Texmacs, correctly, does not recognize the macro test and displays it as an unknown macro.

However, if you try to do this with \ s u p + <RET> Texmacs recognizes that something special is going on. It does not insert a special macro but it just inserts the letters sup:
image
and then with <RET> you get
image

If you look at the source, here is a comparison of the results of the two procedures above:
image
is encoded as
image

Example 3:
Generally, Texmacs encodes normal math operators using the <math-up|???> macro. For example, if I wanted to use the operator conv (e.g. to denote the convex hull of a set) I would do:

  • put myself in math mode;
  • click on image;
  • type in the operator: image;

If I wanted to reuse this operator often, I could define a macro in the preamble:


And now I can insert conv either by assigning it a keyboard shortcut or using hybrid mode by typing \ c o n v <RET>:
image
becomes image

Example 4:

Following up on Example 3, suppose that I go out of my way to define sup to be a math-up - type math operator in the preamble:
image

If I try to enter my new macro using hybrid-mode then Texmacs actually does not allow this to happen and just inserts the letters s u p instead of my macro.
Type: \ s u p:image
Follow it by <RET>: image
with this as corresponding source: image

Issues, thoughts, and questions
1) I think logic and structure should be emphasized and sup inf and the rest should be special tags/macros instead of just having ad-hoc logic. This would also allow eliminating ad-hoc logic from the LaTeX converter to search for keywords as substrings of math.

2) Hybrid mode should give priority to user defined macros rather than latex keywords. Doing what is described in Example 4 should give the defined macro, rather than revert to the logic of “LaTeX” symbols. User defined preferences should have higher priority!

3) How would one define a macro that renders subscripts below in the same way as sup does?
More precisely, suppose that I want to define the operator fancylim that has the same visualization of lim when followed by an underscript (see Example 1). The only way I see of doing it now is by manually using “Insert->Script->Script below” and even then the spacing is off:


with code:

4) According to example 4, is there any way to insert my newly defined sup operator without assigning a special shortcut and without using the menu (keyboard only)?

5) Erasing operator is, I believe, suboptimal. If you have image
erasing removes the p. This is mostly not what one wants. When I am erasing sup I am probably not looking for the operator su that probably is not defined. It would be, I think, more ergonomic, in that case to erase sup and write the mysterious su operator. This would be solved by defining sup as a tag/macro

6) See the bug below

Bug with selecting and copy pasting math operators

Bug 1
Suppose I start with the following math-mode equation
image

I would like to copy the limit to somewhere. So I press <SHIFT> and start moving the cursor backwards. If I am in front of the whole limit, this works well. One backwards arrow gives
image
and one copy+paste later I have image on a new line.

If I start before the subscript but in front of the lim then this happens:image after two <SHIFT>+<LEFT ARROW> becomes
image
that is not very ergonomic but it is NOT A BUG.

However, suppose I start with the function and I want to copy paste with the limit with the function. If I do it with the keyboard, everything works like expected:
image
followed by just enough <SHIFT>+<LEFT ARROW> gives
image
If you do a copy paste at this point, everything works as expected:
image

However if you accidentally go one step backwards more with the selection this is what happens:
image
and a copy paste gives:
image

This is a mild bug. One can see what is happening: the highlighting goes until the m and does not include the li part. However, the visual representation of this is off. The editor suggests that the whole lim is highlighted while only the m is.

This is much a bigger issue when you are using a mouse. If you select by dragging here is what you get:image
A copy+past gives a truncated limit:
image
Since aiming with the mouse is very unproductive, this essentially makes mouse selection useless in formulas with things like lim, sup and such.

Note that this does not happen with operators enclosed by math-up. Even though the cursor can end up in the middle of the operator:
image
A copy paste yields:
image

Furthermore, if conv is defined as a macro in the preamble it works even better: the cursor sticks to the boundary of the tag/macro and does not go inside!!
image

(Please forgive me for the meaningless math above)

TL; DR

Please make sup inf and such into a tag/macro instead of using ad-hoc logic for rendering these math operators. I believe this is more logical and avoids some buggy behavior.

Also, I think one should be encouraged to “define” math operators in the preamble if one uses them consistently through a paper. I would appreciate a pointer on how to encode the behavior of super/subscripts close to such macros.

Summary
Here is a screenshot of three math equations with:

  • the operator conv
  • the ad-hoc defined operator sup
  • the default result of typing \ s u p <RET>
    I believe sup and similar should be like in the second math entry rather than in the third one. Texmacs however really prefers the third option (second version of sup) and I think that is counterproductive.
    image
    Source:
    image

EDIT: One shortcoming of my proposal comes to mind: suppose you are a new user typing math and you just write s u p to insert a sup. Then this would just insert the letters sup and not a special operator. Maybe logic to detect these operators when typed should be implemented (it already is) but, for example, once space is pressed (that should be pressed at the end of the operator sup) then it converts it to a macro?

Thank you very much in advance!!!

1 Like

I am going to read the details of your interesting message in the very near future, for the moment I have two very mild comments.

  • it may be that the background cause of the anti-intuitive behaviour of TeXmacs is that some relevant points of its logic are not made clear enough or at all by the author—this is linked to the very low discoverability of some TeXmacs features, which at the moment seems to be a concern for me and you only :wink:
  • I have a hint in particular that there is a logic behind sup and other operators with the same behaviour, in that \ s u p + <RET> is only the hybrid mode representation of the sup operator, and that this hybrid mode representation “needs” to be there to have a “LaTeX to TeXmacs mapping” but is completely separated by the way the operators are represented in TeXmacs

If I have more comments after I read the rest of your post I will write again.