2

I am trying to create a table in which a column consists entirely of chemical formulae. I would like to use the array package to automatically insert the \ce{...} command, taking the cell's content as argument.

I understand from “\bgroup and \egroup? ” that \bgroup and \egroup only work for replacements for low level commands like \textbf. Unfortunately I do not understand how to use the realboxes package provided there to make it work. I cannot assess if it is appropriate for that task in the first place.

Here is a minimal not working example of what I am trying to do:

\documentclass{article}
\usepackage[version=4]{mhchem}
\usepackage{array}
\begin{document}
  \begin{tabular}{>{\textbf\bgroup}l<{\egroup}%
                  >{\ce\bgroup}l<{\egroup}}\hline
    R1 & -C6H5 \\
    R2 & -C6H4F \\\hline
  \end{tabular}
\end{document}

1 Answers1

5

The package collcell is your friend for this:

\documentclass{article}
\usepackage[version=4]{mhchem}
\usepackage{array,booktabs,collcell}

\begin{document}
  \begin{tabular}{>{\bfseries}l
                  >{\collectcell\ce}l<{\endcollectcell}}\toprule
    R1 & -C6H5 \\
    R2 & -C6H4F \\\bottomrule
  \end{tabular}
\end{document}

enter image description here

Note that I also used booktabs for the table rules. Last but not least, you don't need \textbf, you can use \bfseries which does not take an argument.

yo'
  • 51,322