13

I take it it is possible to redefine ^ (and _) to yield \textsuperscript in text mode and work normally in math mode (is there a good reason why this is not the default behaviour?), but I don't know how. This is what I tried:

\documentclass{article}

\begin{document}

\let\oldhat^
\def^{\ifmmode\expandafter\oldhat
  \else\expandafter\textsuperscript\fi}

$x^n$

% indulgence^2

indulgence\textsuperscript{2}

\end{document}
13

There are good reasons for not using ^ in text mode. However, if you want to live dangerously,

\documentclass{article}

\catcode`^=\active
\protected\def^{\ifmmode\expandafter\sp\else\expandafter\textsuperscript\fi}

\begin{document}

Indulgence^2

$x^2$

\end{document}

enter image description here

  • 6
    Care to share what those reasons are? – Toothrot Mar 21 '18 at 11:53
  • @Toothrot You lose the ability of specifying a character with the ^^ notation (it's more relevant for XeTeX or LuaTeX, though). This also can conflict with some babel modules. – egreg Mar 21 '18 at 11:55
  • One \expandafter is redundant here. – wipet Mar 21 '18 at 12:05
  • 2
    @wipet Yes, I know, but decided to leave it for two reasons: symmetry is one, annoying you is the other. – egreg Mar 22 '18 at 7:36
  • 1
    @egreg I know that you knows. I have put this comment only as an exercise for other readers. – wipet Mar 22 '18 at 8:23
7

I'll try to answer your second part of your question is there good reason why it is not default behavior. TeX works in horizontal mode or vertical mode or math mode. In horizontal mode (you call it as text mode), TeX puts typesetting material horizontally side by side. The exception from this horizontal direction is possible (by \lower, \raise primitives) but it is not common. In math mode, TeX puts typesetting material in very special way including special behavior when superscript or subscript is created. So, internally, this is always done in math mode by TeX. You can check in latex.ltx that \textsuperscript is defined in LaTeX using transition to math mode.

  • Maybe I missed it, but I don't see a "why not" here. This seems mostly "why." (Maybe that question is too broad.) – Gossar Mar 21 '18 at 22:05
  • @Gossar I felt the same about this answer too, initially, but the “why not” is implicit in “it is not common”. Basically, if you consider Knuth designing the typesetting system in 1977 for his TAOCP book (and at most having in mind a typical book), then no books use superscripts for text. If a ^ is encountered in non-math mode, it would be more helpful to the user to remind them they've probably forgotten to enter math mode, than to blindly raise text and exit math mode immediately, because what came before and after the ^ is also probably math. – ShreevatsaR Mar 22 '18 at 0:27

protected by Community Mar 21 '18 at 16:11

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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