Is there a way to override LaTeX's errors about double subscripts and superscripts?
Asked Answered
B

3

6

Minor point about LaTeX that bothers me. When one writes

a^b^c, a_b_c  

or

a'^b  

in math mode, LaTeX gives an error message complaining about multiple super/subscripts. This is particularly annoying after replacing a string containing a super/subscript or when using the apostrophe, '.

Is there a way to override the error and have LaTeX simply output

a^{bc} a_{bc} {a'}^b  

and so on?

Budbudapest answered 27/5, 2010 at 12:2 Comment(0)
R
6

The following is what you wish

\catcode`\^ = 13 \def^#1{\sp{#1}{}}
\catcode`\_ = 13 \def_#1{\sb{#1}{}}
Rockbottom answered 27/5, 2010 at 14:18 Comment(0)
P
3

This is one of those cases where you really should be warned, and have to place the braces the way you want them - or write something without a double sub/superscript, if that's what you mean. Generally, when you're using superscript to indicate exponentiation, not indexing, a^b^c = a^{b^c}, so the output you describe is definitely incorrect in some cases. Sure, if they're superscript indices, you might mean a^{bc}, but how's LaTeX to know? And for subscripts, what if you really do mean a_{n_k}, not a_{nk}? (that is, double-indexing vs. a sequence of indices)

(And of course, if this crops up as part of a substitution, you can probably figure out a way to fix it as part of the substitution.)

Pantaloons answered 27/5, 2010 at 12:10 Comment(4)
Good point - it's true that in formulas exponentiation has funny rules in the order of operations. But in LaTeX, the convention is that only the first character after a ^ sign gets read as a superscript (unless there's a backslash or {},) so the logical way to interpret a^b^c would be to treat ^c independently from ^b as a first-order superscript. So while the warning behind the error message is understandable, is there a way to have LaTeX ignore these particular "errors" and interpret them in this way?Budbudapest
@Dmitry: No, I don't believe so. And your reasoning isn't really complete. It's more accurate to just use a single rule: only the first group after a ^ is placed in superscript. This means the real question is the associativity of the ^; you could just as easily bind the b^c first. What I was trying to convey in my answer is that there's so much ambiguity that it'd be bad for the language to ever force it one way or another. And while suppressing warnings is one thing, this is an error message. LaTeX simply requires the syntax to be unambiguous, for better or worse.Pantaloons
@Dmitry: I agree with Dmity's reasoning here. Latex doesn't have a concept of expression, instead it has it's eyes-mouth-stomach metaphor for handling input, and Dmitry's suggestion works fine with that.Lacteal
@charles Stewart: I don't quite agree - it makes sense to be able to define your own notation which does this, and it possibly makes sense to even replace ^ and _ with it as Alexey Malistov's answer suggests (though one has to consider whether this makes the source harder to read). It doesn't make sense to override the error, though.Pantaloons
N
2

While Alexeys answer should be the acceppted one -- it does exactly what the author wants -- let me note there are also Latex packages available doing exactly this, most notably the Tensor package can do this:

Screenshot from the tensor package documentation

You write \Gamma\indices{^\mu_{\nu\rho}} for the first example. In contrast, with Alexeys answer you just can write \Gamma^\mu_{\nu\rho} which is obviously shorter. However, the catcode redefinition of the ^ and _ characters will break any occurence of these characters in regular text which is annoying especially outside of math mode.

Nonperformance answered 14/7, 2018 at 11:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.