Problem adding subscripts and superscripts [duplicate]

Possible Duplicate:
Cursive text after _

I need to have some subscripted and superscripted text in my thesis document. But when I try to do it the way as H_{2O} or S^{2}. I get an error at that point and if I compile it anyway I get text with so white spaces for the rest of the line. Like,

H_{2O}helloworldwhyisthishappening


Do I need to use some package to have this functionality? I thought it was standard latex. Or may be it is due to conflict with some package. I'm using the university template so don't quite understand what the problem is. I have put the header section where the packages are called below. Please help.

\usepackage{fancyhdr}
\usepackage{verbatim}
\usepackage{acronym}
\usepackage{natbib}
\usepackage{color}
\usepackage[pdftex]{graphicx}
\usepackage{url}
\usepackage{appendix}
\usepackage[font=small,format=plain,labelfont=bf,up,textfont=it,up]{caption}
\usepackage{tikz}
\usetikzlibrary{arrows,automata}
\usepackage{amsfonts,amssymb,amsbsy,amsmath}
\usepackage{listings}


marked as duplicate by clemens, Werner, egreg, Mensch, lockstepFeb 1 '13 at 19:55

If you are typesetting chemical formula, look up the mhchem package. This allows you to typeset chemical formulae easily.

For example: \ce{CO2} has the "2" as a subscript. Neat.

\documentclass{article}
\usepackage{mhchem}
\begin{document}
\ce{H2SO4}
\end{document}


Yields:

• +1 For chemical formula this is the way to go (Sorry Seamus, I'm already out of votes for today). My answer was more about the general case. – Martin Scharrer Feb 18 '11 at 15:28
• One could also use the package chemmacros and its command \ch (not that there's anything wrong with mhchem :) ) – clemens Sep 30 '12 at 9:03
• The chemmacros package looks a lot more powerful than mhchem, but for what was required for this question, I think mhchem is the simpler, smaller, cheaper option. – Seamus Oct 1 '12 at 10:40

The _ and ^ character only work in math-mode. For text you need to use \textsubscript (needs the fixltx2e package) and \textsuperscript instead.

The reason why all the whitespace after H_{2O} is missing is that TeX changing into math-mode at the _ and then all the letters afterwards are set as variables. Also I think you mean H_{2}O (BTW: the braces can be skipped when they include only one character).

No special package but you need to be in math-mode. Use  or \ensuremath{}.

• This is not a good solution. You shouldn't use mathmode for things that aren't maths. There is a textsubscript option mentioned in a different answer that is the Right Way to do this... – Seamus Feb 18 '11 at 15:18
• @Seamus: is this the reason, why you never use footnotes ;-) – user2478 Feb 18 '11 at 16:12
• @Herbert Is that how footnotes work? Huh. I am going to start boycotting footnotes now! But seriously, since footnotes are marked up "semantically" it's ok, even if they abuse mathmode internally... – Seamus Feb 21 '11 at 12:36

The easiest ways I have found to include super- and subscripts in text mode are shown below. The example is the chemical formula for silicon dioxide:

Method 1:

$\rm SiO_2$


Method 2:

SiO$_2$


I am using MikTeX 2.4 and dvips + ps2pdf, and my PDF output looks good: The letters are definitely in text mode, not math, and the results of the two methods look the same in PDF.

An addendum: If the super- or subscript is an expression, do the following:

C$n$H${2n+2}$

which yields the chemical formula for polyethylene.