latex3 / latex2e

The LaTeX2e kernel
https://www.latex-project.org/
LaTeX Project Public License v1.3c
1.93k stars 267 forks source link

amsmath(?): Addition of `\mathup` command #772

Open oscargus opened 2 years ago

oscargus commented 2 years ago

Brief outline of the enhancement

LaTeX2e generally cannot add new features without an extreme amount of care to accommodate backwards compatibility. Please do not be offended if your request is closed for being infeasible.

There is, as far as I can tell, no canonical way to generate upright text using the math-font in math mode, independent of the font family used. Especially, this becomes a problem when the concept of using upright for subscripts that are actually words (as opposed to indices) is applied. For example $I_{\mathup{load}}$ for the load current and one would like to write the equations in such a way that they can be reused between different document styles and these styles have sanserif text and serif math (or vice-versa).

Here are some example discussions from Stack Exchange:

I have also had several similar discussions regarding how to generate LaTeX from SymPy and after quite a few years, I've come to the conclusion that the best way forward is to request for a \mathup command.

Drawbacks: There is yet another option among \text, \mathrm, \operatorname, \mathsf etc for users to get confused with. Advantage: There is a canonical way to get the upright math font.

As this is an addition, I cannot really see any compatibility issues (except that people do not, yet, have a new enough version of amsmath installed).

With limited insight, I still have the impression that this should be reasonably straightforward to realize. Primarily, as there are "hacks" to obtain it in specific situations. But rather than adding (and possibly modifying) code in the preamble every time, I believe it is useful to have it as an existing feature, possibly in amsmath as that is maybe a more realistic location as opposed to introducing it in the core.

Minimal example showing the current behaviour

I hope that the above description is enough, but can provide screen shots of problematic combinations of classes (either because one is converting between different document styles for re-submission or one prepares a presentation based on a paper).

davidcarlisle commented 2 years ago

Speaking personally I think what you describe here is \mathrm.

Unlike text fonts where the different axes can be selected independently, so \textup can switch to upright shape, leaving the other font axes unchanged, in math there are just 16 (in classic tex) slots and they are specified individually. The math font commands do not "combine" and are also independent of any text fonts specified.

So I think you want \mathup to mean either \mathrm or \mathsf depending on some condition, but it isn't clear how to specify that condition. TeX does not really "know" if the main document font is serif or sans serif that is just a visual property of the font. In a document setup that is primarily sans serif I would expect that it would not be enough just to make \mathup be sans serif you would want the operator font used by \log and \sin to be sans serif as well and that is (usually) done in a way that also makes \mathrm sans serif.

It is true that using the name \mathrm when the font is sans serif may be a little awkward but it should not cause major issues. That said, it would be possible to have a \mathup that is aliased to \mathrm but open to be redefined by some document classes to be \mathsf. This is basically the approach taken by unicode-math which documents

Note that \mathup and \mathrm are aliases of each other and cannot be configured separately.

Bringing unicode-math and classic TeX math commands into closer alignment is a general aim as well so this would be something to consider.

oscargus commented 2 years ago

Thanks you for your answer!

While preparing an example, I suddenly realized that \operatorname actually works for subscript as well. So what I guess I now really would like is an alias of \operatorname as \mathup, ideally without the additional spacing (which really isn't an issue as far as I can tell for the subscript use case). The actual naming of it and the often related spacing discussion seem to have even prohibited me from trying (and I do not seem to be the only one).

(I was about to write something like "what about the magic in operatorname, but without the spacing?".)

Code example:

\begin{equation}
\operatorname{operatorname} \text{text}\ \mathrm{mathrm}\ \mathsf{mathsf}\  \sin 
\end{equation}
\begin{equation}
I_{\operatorname{load}}, I_{\text{load}},\ I_{\mathrm{load}}, I_{\mathsf{load}},
\end{equation}

(\sin for reference as the "standard math font")

Conference template with sans-serif text font and serif equation font: image

Beamer default: image

oscargus commented 2 years ago

So to conclude. It seems that \operatorname does what I was after, although the spacing will probably bite me at some stage. Most StackExchange answers focus on \text vs \mathrm, so I guess that people in general do not even consider \operatorname (although admittedly the questions often also do that).

I think it would be nice to have a command with a name that "makes sense", that does the font part of \operatorname, but not the spacing part. Maybe \mathup is problematic due to the existence in unicode-math, but otherwise that sounds like a natural choice to me.

davidcarlisle commented 2 years ago

As I say I'd probably make \mathrm be sans serif in the beamer case (but that doesn't mean beamer can or should change its default now)

You can get the effect of \operatorname without the \mathop spacing using {\operatorname{load}} or {\operatorfont load} . It's true there is not an argument form "out of the box". While a \mathoperatorfont{load} (or whatever) command would be easy to define I don't think that should be called \mathup , as \mathup more or less directly hints of an upright shape whereas \operatorfont and \operatorname are "the font used by \log " and for example the unicode-math package documentation shows an example of that being italic if required for some special settings. (see \setoperatorfont\mathit)

FrankMittelbach commented 2 years ago

My feeling is that you are misunderstanding the underlying mechanisms @oscargus. Something like \operatorfont might appear to you as giving the right results but conceptually that is pure chance based on the math layouts you have looked at. The font used by \operatorfont just says that this is the font for "textual operators" but what that is in a certain styles may not be at all what you think it is. If LaTeX would define \mathup it would only help if all math setups would follow and make a definition for it because LaTeX can't guess what it should be, it could be \mathrm in one but \mathsf in another style. At the moment I don't think we want to add something like this without doing the work on getting "classic" and "unicode-math" closer together at the same time.

My suggestion to you is for now is to do

\newcommand\mathup{\mathsf}    % or \mathrm depending layout used in the document

on a per document basis, after loading whatever font packages you want to use (or providecommand in case you hope that one day the math font packages come around setting it up correctly).

oscargus commented 2 years ago

Thanks for the further clarification and the {\operatorname{}} suggestion @davidcarlisle .

I totally agree with you. I do not think \mathopertorfont{} is really a better name, btw. And \mathop is also taken...

@FrankMittelbach you are correct that I have learnt a lot today. I was fully aware of the redefinition approaches, but those are what I (and I assume "everybody") would like to avoid, if possible. Of course for personal convenience, when submitting manuscripts in source format (some publishers can be a bit stingy with these things), but also if one would like to generate LaTeX code that to the largest possible extent renders as expected.

But I now understand that it is fundamentally impossible the way the math fonts are defined. So it comes down to what is the most likely thing to work (as in providing upright text in a font similar to the "math font"). Which as far as I can tell still is {\operatorname{}}. The fact that one can redefine it to an italic face seems to be a issue for any approach (\mathrm and \mathsf as well)? But most "well-behaved" styles seems to define \operatorfont as an upright version of the "math font" (and at least some I have encountered do not redefine \mathrm in the case a sans-serif font is used, beamer of course, but I think also some other, although if I understand correctly that is, sort of, a bug).

Feel free to close this if you prefer and thanks again both of you for your insightful answers!

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity.