I am trying to define a tabular column, which automatically applies the \ce{} command from the mhchem package to format the cell content as a chemical formula.
The Google-search-results answer is to use the array and collcell packages together with the obvious \newcolumntype.
However, I cannot achieve what I want, somehow.
Here is what I got so far:
\documentclass{article}
\pagestyle{empty}
\usepackage{tabularx}
\usepackage{siunitx}
\usepackage{csvsimple}
\usepackage{array}
\usepackage{collcell}
\usepackage[version=4]{mhchem}
\def\cewrapmacro#1{% Simple test macro
\ce{{#1}}%
}
\newcommand{\MyChemTableCell}[1]{\ce{#1}}
\newcolumntype{M}{>{\collectmacro\cewrapmacro}l<{\endcollectmacro}}
\newcolumntype{L}{>{\collectcell\MyChemTableCell}l<{\endcollectcell}}
\begin{filecontents}[]{test-file.csv}
mz, Intensity, annotation \
1.20, 56.01, CH3OH \
12.13, 1901.0, CH2OO- + H+
\end{filecontents}
\begin{document}
\csvstyle{MyStyle}{
no head,
before reading=\centering\sisetup{table-number-alignment=center,table-format=3.3,table-auto-round},
tabular={c@{}S[table-format=1.4]S[table-format=1.2]M},
table head=\ \empty & m/z & {Relative Intensity} & Annotation \,
table foot=\,
late after line=\,
}
% the first empty is for using S as first column
\csvreader[MyStyle, filter expr={test{\ifnumgreater{\thecsvinputline}{1}}}]{test-file.csv}{}{\empty & \csvcoli & \csvcolii & \csvcoliii }
\end{document}
As you see, I have arrived at two different paths depending on which example I follow, and defined two column types, L and M.
The M compiles with a few undefined control sequence warnings, without doing any formatting, whereas the L leads to more or less infinite Missing \cr inserted.
I feel like, apart from calling \ce{}, I have directly copy-pasted the results I found from search; what am I doing wrong, what do I have to do to define a column type that automatically formats the cell contents as chemical equations/formulas?

\Xhlinemacro. Maybe try to fix errors one at a time, starting from the first. – Jasper Habicht Apr 06 '23 at 06:45tabularraydocumentation. See thecmdkey. It might help you. – projetmbc Apr 06 '23 at 06:52