Home    Personal    Work    Computers    Miscellaneous

LaTeX    Software Projects    Rendering Tutorial    UNIX Commands    Using CVS    Case Modding    Base Wiki

Subscript and superscrint in Text Mode

The problem

The common way in Latex to have super- subscript is to use ^ and _ in math mode. As an example,

$a_x b^y$

writes out a with subscript x and b with superscript y.

The Latex super- and subscript operators ^ and _ can only be used in math mode. It makes no sense to have

Welcome to the 21^{st} century.

in your text since the superscript operator is not recognized in the text mode we're in.

Working toward a solution

One solution to the problem is to write

Welcome to the 21$^{st}$ century.

in order to bring Latex into math mode in order to write the superscript. But there's some problems with simply having text in math mode. For one, it is written in a different script (usually italic), and words are collated when they are offered to math mode with spaces. To solve this, we can ask math mode to go back to text mode and than write our text, i.e. with

Welcome to the 21$^{\textrm{st}}$ century.

It seems a bit weird, but we're in fact asking math mode to bring us in superscript mode, and then go back to text mode to write our text.

Encapsulating this as commands

If you're writing about centuries, or ordinal numbers such as first, second, third, ... you might want to consider writing the following new commands and put them in your Latex document's preamble. (Greggory Gruen suggested adding sufficient {}s, to ensure it works with all La Te X versions)

\newcommand{\superscript}[1]{\ensuremath{^{\textrm{#1}}}}
\newcommand{\subscript}[1]{\ensuremath{_{\textrm{#1}}}}

so you can write

Welcome to the 21\superscript{st} century.

other such quick commands can be written with

\newcommand{\th}[0]{\superscript{th}}
\newcommand{\st}[0]{\superscript{st}}
\newcommand{\nd}[0]{\superscript{nd}}
\newcommand{\rd}[0]{\superscript{rd}}

so you can write

Carl took 1\st place. Elly, Tom and Ann came in 2\nd, 3\rd and 4\th.

\textsuperscript

Stephan Bode wrote in to tell about the \textsuperscript that allows to put superscript in text, without having to resort to our own commands. Unfortunately, there is no such thing as a \textsubscript in La Te X's defaults ...

Macros extending superscripts and subscripts to text mode

This code first defines the \textsubscript analogous to \textsuperscript, then reimplements ^ and _ as active characters so that they work in text mode as well. (written by Steve Hicks)

\makeatletter
\newcommand\textsubscript[1]{\@textsubscript{\selectfont#1}}
\def\@textsubscript#1{{\m@th\ensuremath{_{\mbox{\fontsize\sf@size\z@#1}}}}}
\newcommand\textbothscript[2]{%
  \@textbothscript{\selectfont#1}{\selectfont#2}}
\def\@textbothscript#1#2{%
  {\m@th\ensuremath{%
    ^{\mbox{\fontsize\sf@size\z@#1}}%
    _{\mbox{\fontsize\sf@size\z@#2}}}}}
\def\@super{^}\def\@sub{_}

\catcode`^\active\catcode`_\active
\def\@super@sub#1_#2{\textbothscript{#1}{#2}}
\def\@sub@super#1^#2{\textbothscript{#2}{#1}}
\def\@@super#1{\@ifnextchar_{\@super@sub{#1}}{\textsuperscript{#1}}}
\def\@@sub#1{\@ifnextchar^{\@sub@super{#1}}{\textsubscript{#1}}}
\def^{\let\@next\relax\ifmmode\@super\else\let\@next\@@super\fi\@next}
\def_{\let\@next\relax\ifmmode\@sub\else\let\@next\@@sub\fi\@next}
\makeatother

Edit (locked) - History - Recent Changes - Search - Statistics
All contents copyrighted 2000-2006 Anthony Liekens unless otherwise noted. Powered by PmWiki
Page last modified on February 27, 2008, at 02:05 PM.