Using mhchem with array

I would like to use the array package to format a column filled with chemical formula in order to avoid writing \ce{X} on each line. I don't know if it is even possible. I started from the trick used to hide a column, but didn't get very far. My code is :

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{array}
\newcolumntype{F}{>{\hbox\bgroup\ce{}}l<{\egroup}@{}}
\newcolumntype{H}{>{\setbox0=\hbox\bgroup}c<{\egroup}@{}}
\newcolumntype{N}{>{\raggedright\arraybackslash}p{3cm}}
\newcolumntype{M}{>{\centering\arraybackslash}p{2cm}}
\usepackage{booktabs}
\usepackage[version=3]{mhchem}

\begin{document}

\begin{array}{N F M}
\toprule
Name & Formula & MW\\
&& (Da) \\
\midrule
glycol & C2H6O2 & 62.07\\
glycerol & C3H8O3 & 92.09\\
& & \\
\bottomrule
\end{array}

\end{document}


What I am looking for, is a command like \newcolumntype{F}{????} which can format the content of the column as a chemical formula

• @David Carlisle The \raggedleft doesn't really matter, I will replace c by l, but what I don't know is how to apply the \ce{} command which argument should be the content of the column. But maybe it is simply not possible ? – DRi Sep 30 '16 at 15:45
• Have a look at the collcell package – clemens Oct 1 '16 at 7:59

Your MWE generates an error about array not in math mode, but youwant tabular here.

As the \ce is not in the last column, you can pick up the cell contents with a delimited argument.

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{array}
\newcolumntype{F}{>{\zzz}l<{\relax}@{}}
\newcolumntype{H}{>{\setbox0=\hbox\bgroup}c<{\egroup}@{}}
\newcolumntype{N}{>{\raggedright\arraybackslash}p{3cm}}
\newcolumntype{M}{>{\centering\arraybackslash}p{2cm}}
\def\zzz#1\relax{\ce{#1}}
\usepackage{booktabs}
\usepackage[version=3]{mhchem}

\begin{document}

\begin{tabular}{N F M}
\toprule
Name & Formula & MW\\
&& (Da) \\
\midrule
glycol & C2H6O2 & 62.07\\
glycerol & C3H8O3 & 92.09\\
& & \\
\bottomrule
\end{tabular}

\end{document}

• You are right, I meant the tabular environment (I got confused with the array package). This is exactly the kind of code I was looking for. Thanks – DRi Oct 2 '16 at 16:13

You could use the collcell package:

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

% tabular material:
\usepackage{array,booktabs,collcell}
\newcolumntype{F}{>{\collectcell\ce}l<{\endcollectcell}}

% sciences:
\usepackage[version=4]{mhchem}% version 4 is the current one
\usepackage{siunitx}% provides column type S' for aligned numbers

\begin{document}

\begin{tabular}{lFS[table-format=2.2]}
\toprule
Name & Formula & {MW} \\
& & {(Da)} \\
\midrule
glycol   & C2H6O2 & 62.07 \\
glycerol & C3H8O3 & 92.09 \\
\bottomrule
\end{tabular}

\end{document}


The example above also uses the siunitx package and its column type S` for aligned numbers.

• I didn't know the collcell package, but it seems interesting since it avoid the creation of a new macro. I knew the use of siunitx with array. Thanks – DRi Oct 2 '16 at 16:21