17

This question already has an answer here:

I'm writing my dissertation and that involves typing a lot of $\mathrm{CO_2}$. I'd like to make a macro for it that's a lot easier to type, but if I put \newcommand{\co2}{\mathrm{CO_2}}, for example, it gives me the error that \mathrm can only be used in math mode. But using \newcommand{\co2}{$\mathrm{CO_2}$} doesn't work either.

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

marked as duplicate by clemens, Phelype Oleinik, Stefan Pinnow, Sebastiano, siracusa Oct 15 at 3:58

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.

  • Have you tried \newcommand{\cotwo}{\mathrm{CO_2}} see tex.stackexchange.com/questions/44545/… also tex.stackexchange.com/questions/393855/… – user170109 Mar 20 at 2:48
  • 7
    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 at 15:35
  • Oh I did not realize you couldn't have digits in macro names! Cool, thanks! – Hayley Mar 21 at 0:36
  • About the digits: \def\co2{CO$_2$} The level of \co2 in London is .... – Fran Apr 8 at 9:15
18

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.

enter image description here

\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.

  • 1
    Or use \coo{} in text mode, which avoids worries about the space getting eaten by something. – David Richerby Mar 20 at 20:35
  • 3
    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 at 7:23
57

You want easy?

\documentclass{article}  
\usepackage{chemformula}
\begin{document}
\ch{CO2}
\end{document}
  • 21
    For completeness, you might mention also mhchem – egreg Mar 20 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. – Dustin Wheeler Mar 27 at 12:53
  • 2
    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. – Dustin Wheeler Mar 27 at 12:59
28

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.)

examples

more examples

more examples

more examples

2

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

enter image description here

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

\begin{document}

\cotwo

\end{document}

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