# Typing CO_2 easily [duplicate]


Is there a way for me to make a macro that makes typing CO_2 easier, even if it doesn't use \mathrm?

• Have you tried \newcommand{\cotwo}{\mathrm{CO_2}} see tex.stackexchange.com/questions/44545/… also tex.stackexchange.com/questions/393855/…
– user170109
Mar 20, 2019 at 2:48
• For anyone new to latex reading this question: There are two separate issues here. One is that \mathrm can only be used in math mode. The second one is that you can't (usually) have digits in macro names.
– sgf
Mar 20, 2019 at 15:35
• Oh I did not realize you couldn't have digits in macro names! Cool, thanks! Mar 21, 2019 at 0:36
• About the digits: \def\co2{CO$_2$} The level of \co2 in London is ....
– Fran
Apr 8, 2019 at 9:15

I'm not sure if this is a question about how to use \co2 as a macro name or just a question about how to subscript in text mode. If you use \coo, the macro

\newcommand{\coo}{\ensuremath{\mathrm{CO_2}}}


works fine in both text and math mode. If you're only planning to use the macro in text mode, CO\textsubscript{2} works.

\documentclass{article}

\newcommand{\coo}{\ensuremath{\mathrm{CO_2}}}
\newcommand{\cooo}{CO\textsubscript{2}}

\begin{document}

With ensuremath: Text \coo\ and math $\coo$ both look fine.

With textsubscript: Text \cooo\ is fine, but math $\cooo$ isn't.

\end{document}


Don't forget the trainling \ and space after the call in text mode.

• Or use \coo{} in text mode, which avoids worries about the space getting eaten by something. Mar 20, 2019 at 20:35
• Especially when using macros, I think that using xspace is really convenient. You put an \xspace at the end of your macro and it will decide if a space is necessary or not.
– Ian
Mar 21, 2019 at 7:23

You want easy?

\documentclass{article}
\usepackage{chemformula}
\begin{document}
\ch{CO2}
\end{document}

• For completeness, you might mention also mhchem Mar 20, 2019 at 9:04
• ChemFormula is part of the excellent ChemMacros suite by clemens. If you continue writing documents in the field of chemistry, I’d highly recommend spending some time skimming the documentation. Mar 27, 2019 at 12:53
• Also worth mentioning that chemformula (and I believe mhchem) are robust with respect to math mode (i.e., no need to modify the code inside vs. outside math environments. Mar 27, 2019 at 12:59

I would use the mhchem package. It makes a lot of chemistry things easier, including reactions as well as formulas.

Put the line

\usepackage{mhchem}


in the header section of your document, and then CO2 is just \ce{CO2}. You can use that in math mode or text mode, it works either way. Here are some examples of other formulae from the package documentation, to give an idea of what you can do. (Note from the very first example how easy it is to write a simple reaction.)

(Note also that, as mentioned in John Kormylo's answer, there is also a package chemformula. It seems to be very similar, and allows you to do the same things in very similar ways. It is a more recent package, but I have no experience with it.)

Using isotope package with the macro \cotwo you can write easily and quickly CO_2:

\documentclass{article}
\usepackage{isotope}
\newcommand{\cotwo}{$\isotope{CO}_2$}

\begin{document}

\cotwo

\end{document}