Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Alright, I'm trying to make a command to insert a proton. This code works fine:

\documentclass[12pt,letterpaper]{article}
\usepackage{amssymb,mathtools}
\usepackage[version=3]{mhchem}

\newcommand{\tphoton}{$\gamma$}

\begin{document}
\tphoton
\end{document}

and this code works fine:

\documentclass[12pt,letterpaper]{article}
\usepackage{amssymb,mathtools}
\usepackage[version=3]{mhchem}

\begin{document}
\ce{$\gamma$}
\end{document}

Why doesn't this code work?

\documentclass[12pt,letterpaper]{article}
\usepackage{amssymb,mathtools}
\usepackage[version=3]{mhchem}

\newcommand{\tphoton}{$\gamma$}

\begin{document}
\ce{\tphoton}
\end{document}

It gives a "! Missing $ inserted.", 3 "! Extra }, or forgotten $."

share|improve this question
    
\tphoton or \tproton, you decide. But example 1 throws an error right now, too. ;-) –  lockstep Jan 15 '12 at 21:32
    
@lockstep Fixed: I pasted an old testcase in by accident.I switched to \tphoton since it was more minimal. –  Canageek Jan 15 '12 at 21:34
add comment

1 Answer 1

up vote 7 down vote accepted

The argument of mhchem's \ce already is a "hidden math mode". \ce{\bfseries B} will give

! LaTeX Error: Command \bfseries invalid in math mode.

Try \ensuremath instead:

\documentclass[12pt,letterpaper]{article}
\usepackage{amssymb,mathtools}
\usepackage[version=3]{mhchem}

\newcommand{\tphoton}{\ensuremath{\gamma}}

\begin{document}
\ce{\tphoton}
\end{document}
share|improve this answer
    
Why does \ce{$\gamma$} work then? (my guess is that it becomes ...$$\gamma$$..., which is also valid). –  Bruno Le Floch Jan 15 '12 at 21:43
    
I guess that the \ce gobbles the $. \ce{$\gamma$}, \ce{$$\gamma$$} and even \ce{$$$\gamma$$$} work fine. –  cgnieder Jan 15 '12 at 21:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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