Text Formattingmhchem | Trouble with new Command

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
ghostanime2001
Posts: 402
Joined: Fri May 20, 2011 9:41 am

mhchem | Trouble with new Command

Post by ghostanime2001 »

I cannot use my new defined command \ces inside \ce{} I get an error. However i can use my defined command outside the array environment but not inside it. What am I doing wrong with this new command? If it can work outside the array environment why isn't it working inside a math environment like array ? I have my command definition enclosed in $ $.

MWE:

Code: Select all

\documentclass[fleqn]{article} 
\usepackage[version=3]{mhchem}
\usepackage{cancel}

\newcommand{\ces}[2]{$\cancel{\ce{#1}}\ce{_{(#2)}}$}

\begin{document}
$\begin{array}[t]{l}
\ce{H2O + \ces{O2}{g} -> H2O + Cl2}
\end{array}$ \\ \\
\ces{O2}{g} 
\end{document}
Last edited by ghostanime2001 on Fri Dec 16, 2011 11:43 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

mhchem | Trouble with new Command

Post by cgnieder »

Using commands inside \ce can be a pain. "Hiding" it between $...$ should work:

Code: Select all

\documentclass[fleqn]{article}
\usepackage[version=3]{mhchem}
\usepackage{cancel}

\newcommand{\ces}[2]{$\cancel{\ce{#1}}\ce{_{(#2)}}$}

\begin{document}
$\begin{array}[t]{l}
\ce{H2O + $\ces{O2}{g}$ -> H2O + Cl2}
\end{array}$ \\ \\
\ces{O2}{g}
\end{document}
site moderator & package author
ghostanime2001
Posts: 402
Joined: Fri May 20, 2011 9:41 am

Re: mhchem | Trouble with new Command

Post by ghostanime2001 »

It works if you lose the $ $ in the preamble \newcommand definition. This is so strange! it works with the $ $ surrounding the \ces command but not inside the command definition itself since you would expect everything inside the $ $ to be "mathematicized"
Post Reply