TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Well, I liked the idea expressed in the question alternative syntax of subscripts using tex. But I want it to work in a sightly different way.

I just want to expand the usual _ and ^. How? So that the code escapes from the sup/subscript if there is a space (see exceptions at the bottom):

  1. a_11 + b_123ijk + c_k\ell should be the same as a_{11} + b_{123ijk} + c_{k\ell}.
  2. And the same for superscripts: m^qwpoeriu equal to m^{qwpoeriu}.

At this moment, I have this code in my preamble (it helps if you want to write F_\mathrm{max} you can do it like F_|max|), so the solution should't break it

\AtBeginDocument{\catcode`\_=12 \mathcode`\_=32768}


I usually space my code \biggl( a_b \cdot \cos(x - c) = 1 \times \vec v rather than \biggl(a_b\cdot\cos(x-c)=1\times\vec v, because I find it much more easier to read. So this would easily work well for me since in so many times I wouldn't need to write {}. Of course, there are moments where I still need to do it, i.e., \alpha_k\ell + \alpha_{\ell k} = 1.

EDIT: More things. I'm not particularly interested in a_b_c_d + e to be like a_{b_{c_d}} + e (and the same with ^), but I think it would be great if that also worked (which means that the space should escape all the sub/superscripts).

More things, NOT only the space should escape the sub/superscript, also this:

  • the alternative sup/subscript, i.e.: a_b^c + 1 = 0 should work as a_{b}^{c} + 1 = 0, and a^blk_ijk as a^{blk}_{ijk},
  • the ending of math $a_bcde$ should be like $a_{bcde}$,
  • and also a closing brace: \frac{1}{a_ijk} equal to \frac{1}{a_{ijk}}.
share|improve this question
Failing to have a space at the end of the sub(super)script will make TeX very unhappy. I can understand the abbreviated `_|max| (although I wouldn't use it), but not the syntax you propose. – egreg Jan 30 '13 at 20:44
@egreg I know that there is a risk, but I plan to assume it. I think it is worth it. In my editor is too easy to automatically add open/close braces, but thinking, I need to change the position of my hand, which stops me a little bit. By the way, in my case, this only expands its usability (I think). – Manuel Jan 30 '13 at 20:53
The code in the referenced answer would basically work here but the comments there saying why it's a bad idea also apply. Especially with the extra requirements to cope with } without a space make the code increasingly fragile – David Carlisle Jan 30 '13 at 21:00
@barbarabeeton still has nightmares decades later that someone sent her xii.tex to publish in tugboat. – David Carlisle Jan 30 '13 at 21:12
My experience has been that it's best to stick with the community's standards. Readability will come with practice. I had to learn the hard way. After writing tons of documents for myself that I never intended to share, then I wanted to share years later.... What a nightmare cleaning up all those files so someone else could use them. Moral: you never know what you may one day want (need) to share with someone else. – A.Ellett Jan 30 '13 at 21:29
up vote 5 down vote accepted

enter image description here

I ask @barbarabeeton's forgiveness.

I just this to give you a test, resist the temptation to use it...



\def\xcollectsub{\afterassignment\collectsub\global\let\tmp= }




    $a_11 + b_123ijk + c_k\ell$

 should be the same as

$ a_{11} + b_{123ijk} + c_{k\ell}$.

    And the same for superscripts:

$ m^qwpoeriu $ equal to $ m^{qwpoeriu}$

$\frac{1}{a_ijk}$ equal to $\frac{1}{a_{ijk}}$.

share|improve this answer
I need it to work correctly together with the code I wrote in the question. But seems nice. EDIT: mmm, not perfect, because if I have to use {} in the subscript it doesn't work, i.e., a_\hat{i}jk = 1 should be a_{\hat{i}jk} = 1. Moreover a_k\ell + 1 = 0 doesn't work good. But may be this is an overkill. – Manuel Jan 30 '13 at 22:15
The code in the other answer would stop at + but you specified (originally) just to stop on space so I changed it, you then added } back (there is no space token after \ell ) It also doesn't work if the math ends with \end{align} instead of $, actually none of those things is hard to fix (just an extra \ifx test for each thing more or less) but it really isn't a good idea to make it more and more complicated. – David Carlisle Jan 30 '13 at 22:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.