Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}}.
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
    
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
3  
@barbarabeeton still has nightmares decades later that someone sent her xii.tex to publish in tugboat. –  David Carlisle Jan 30 '13 at 21:12
show 3 more comments

1 Answer

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

 \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}
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
add comment

Your Answer

 
discard

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.