4

This question already has an answer here:

Is there at all a way to make underscores and carets persistent in LaTeX math mode? What I mean, is to be able to write several-character sub/superscripts without using curly brackets. Could LaTeX maybe understand that a white space determines the end of my sub/superscript?

A quick example. I'd like to have something like

x_ij y_ij

Compile to the same result as

x_{ij}y_{ij}

I understand that this is probably not a really reliable thing, but I'm looking to take notes faster during class!

Many thanks

marked as duplicate by Werner math-mode Sep 20 '16 at 21:19

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

migrated from stackoverflow.com Sep 20 '16 at 18:12

This question came from our site for professional and enthusiast programmers.

  • 1
    No, it's not reliable. You're very likely to forget a space when typing something like Let $x_i$ be the least... which would lead to a long string of weird errors. It would be easier something like x_ij_ so the subscript is delimited by another _ – egreg Sep 20 '16 at 18:17
  • 1
    Related, if not equal: tex.stackexchange.com/q/96080/21930. – Manuel Sep 20 '16 at 19:40
1

Making _ and ^ active to check wether the next token is { or not, and if it is, act as always _{asdf}, but if it's not, look until the next space (you cannot end math with $ for instance without leaving a space before).

\documentclass{scrartcl}

\makeatletter

\newcommand*\newsb{\@ifnextchar\bgroup{\sb}{\sbauto}}
\def\sbauto#1 {\sb{#1}}
\newcommand*\newsp{\@ifnextchar\bgroup{\sp}{\spauto}}
\def\spauto#1 {\sp{#1}}

\catcode`\^=\active
\catcode`\_=\active

\AtBeginDocument
 {\catcode`\^=\active
  \catcode`\_=\active
  \let_\newsb
  \let^\newsp}

\makeatother

\begin{document}

$\sum_{i,j,k}x_ijk y^ijk $ \quad $a_1234 b^4567 - c^u9v d_v77p $

\end{document} 
  • May be more robust is this other solution by David. I just remembered that I had asked the same question some time ago. – Manuel Sep 20 '16 at 19:41