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}}.
  • 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, 2013 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, 2013 at 20:53
  • 1
    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 Jan 30, 2013 at 21:00
  • 4
    @barbarabeeton still has nightmares decades later that someone sent her xii.tex to publish in tugboat. Jan 30, 2013 at 21:12
  • 1
    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, 2013 at 21:29

2 Answers 2


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}}$.

  • 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, 2013 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. Jan 30, 2013 at 22:34

(The following answer was originally posted to address the query Making underscore persistent in LaTeX math mode (for long subscripts). It was moved here after that query was closed as a duplicate of the present query.)

The following, LuaLaTeX-based solution assumes that the sub/superscript-related material is terminated either by whitespace or a +, -, =, or $ symbol. It further assumes that sub/superscript material consists exclusively of upper- and lowercase letters, numerals, and commas. I.e., no + or - symbols inside the sub/super-script. (Of course, the Lua code employed below may be changed to suit your needs.) Observe that the code also works with subscripts and superscripts associated with \sum and \int expressions.

enter image description here


function do_sub_super_grouping ( s )
   return ( string.gsub ( s , "([_%^])([%w%,]+)([%s%$%-%+%=])", "%1{%2}%3" ) )
luatexbase.add_to_callback ( "process_input_buffer" , 
   do_sub_super_grouping , "do_sub_super_grouping" )

$\sum_i,j,k ^100 x_ijk y^ijk+a_12,34 ^56 b^7890- c^u,9,v d_v7,v7$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .