6

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

\makeatletter
\begingroup\catcode`\_=\active\protected\gdef_{\@ifnextchar|\subtextup\sb}\endgroup
\def\subtextup|#1|{\sb{\mathrm{#1}}} 
\AtBeginDocument{\catcode`\_=12 \mathcode`\_=32768}
\makeatother

Why?

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}}.
Manuel
  • 27,118
  • 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
  • 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 – David Carlisle Jan 30 '13 at 21:00
  • I write my documents alone, so I don't have to change the language, or tell other people this. If I have to collaborate, I will write it the usual way. This would be like a treat with myself (and I think it makes no problem reading code). By the way, I don't know the tex internals, so I believe you that the } will make it very fragile. – Manuel Jan 30 '13 at 21:07
  • if you get your wish, you must promise never, never, never to give one of these files to someone else, and especially not to send it to any publisher! please have pity on us who are involved in book and journal production. – barbara beeton Jan 30 '13 at 21:08
  • @barbarabeeton I promise. – Manuel Jan 30 '13 at 21:10
  • 4
    @barbarabeeton still has nightmares decades later that someone sent her xii.tex to publish in tugboat. – David Carlisle Jan 30 '13 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 '13 at 21:29

2 Answers2

8

enter image description here

I ask @barbarabeeton's forgiveness.

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

 \documentclass{article}

\makeatletter
\catcode`\_\active
\catcode`\^\active

\def_{\sb\bgroup\xcollectsub}
\def^{\sp\bgroup\xcollectsub}
\def\xcollectsub{\afterassignment\collectsub\global\let\tmp= }
\def\collectsub{%
\def\next\tmp{\tmp\xcollectsub}%
\ifx\tmp\@sptoken\let\next\egroup\fi
\ifx\tmp$\let\next\egroup\fi
\ifx\tmp\egroup\let\next\egroup\fi
\ifx\tmp\bgroup\def\next{\global\let\tmp}\fi
\next\tmp}


\makeatother

\begin{document}

$\frac{1}{a_ijk}$

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


\end{document}
David Carlisle
  • 757,742
  • 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
3

(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

\documentclass{article}

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

\begin{document}
$\sum_i,j,k ^100 x_ijk y^ijk+a_12,34 ^56 b^7890- c^u,9,v d_v7,v7$
\end{document} 
Mico
  • 506,678