2

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?

  • 3
    Welcome to TeX.SX! "Undefined command" messages are never just "warnings". Altough TeX tries hard to continue compilation, it almost always will result in bad output. So take these "warnings" as what they are: errors. Apart from that, you MWE does not compile for me due to an undefined \Xhline macro. Maybe try to fix errors one at a time, starting from the first. – Jasper Habicht Apr 06 '23 at 06:45
  • 2
    See page 21 of the tabularray documentation. See the cmd key. It might help you. – projetmbc Apr 06 '23 at 06:52

2 Answers2

3

As it turns out, the issue seems to be, that the two packages collcell and csvsimple just do not work together, as seen e.g. in this answer: https://tex.stackexchange.com/a/83410/164869

A workaround would be to use the tabularray package, e.g., like so:

\documentclass{article}
\pagestyle{empty}

\usepackage{tabularray} \usepackage{siunitx} \usepackage{csvsimple-l3} \usepackage{array} \usepackage{collcell} \usepackage[version=4]{mhchem}

\UseTblrLibrary{siunitx}

\begin{filecontents}[]{test-file.csv} mz, Intensity, annotation 1.20, 56.01, CH3OH 12.13, 1901.0, CH2OO- + H+ 1.20, 56.01, CH3OH \end{filecontents}

% required by tabularray \NewTableCommand\toprule{\hline[0.08em]} \NewTableCommand\midrule{\hline[0.05em]\vspace{1pt}} \NewTableCommand\bottomrule{\hline[0.08em]}

\begin{document}

\csvstyle{MyStyle}{ no head, before reading=\centering\sisetup{table-number-alignment=center,table-format=3.3,table-auto-round}, tabularray={ colspec={c@{}S[table-format=1.4]S[table-format=1.2]l}, row{1} = {guard}, stretch = 0, column{4} = {cmd=\ce} }, table head=\toprule\empty & m/z & {Relative Intensity} & Annotation \\midrule, table foot=\bottomrule, 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}

2

Not exactly what you're after maybe, but you can place the \ce macro directly into the body of the table and this way circumvent the problems resulting from defining a new column type. You only need to take care of the right order of expansion.

Some other remarks:

  1. There shouldn't be any \\ at the end of lines inside the CSV file. Removing them solves the problem of spurious lines in your table.
  2. Don't use the S column type in the option tabular. As noted in the csvsimple manual, it is more robust to use the \tablenum macro inside the tabular body. This solves the problem in your code which you tried to circumvent by adding a new column using c @{}.
  3. Don't use no head if the CSV actually has a header row. You can safely remove this option (once your CSV is properly formatted, see 1.) and also remove the filter expr option in your code.
  4. You should probably remove the `\empty´ macros since they don't do anything here.

Full MWE:

\documentclass{article}
\pagestyle{empty}

\usepackage{siunitx} \usepackage{csvsimple} \usepackage[version=4]{mhchem}

\begin{filecontents}{test-file.csv} mz, Intensity, annotation 1.20, 56.01, CH3OH 12.13, 1901.0, CH2OO- + H+ 12.13, 1901.0, CH2OO- + H+ \end{filecontents}

\begin{document} \csvstyle{MyStyle}{ before reading={\centering\sisetup{table-number-alignment=center, table-auto-round}}, tabular={c c c}, table head={\hline m/z & Relative Intensity & Annotation \ \hline}, table foot={\hline}, late after line={\} } % the first empty is for using S as first column \csvreader[MyStyle] {test-file.csv} {} { \tablenum[table-format=1.4]{\csvcoli} & \tablenum[table-format=1.2]{\csvcolii} & \edef\csvcoliiitemp{\noexpand\ce{\csvcoliii}}\csvcoliiitemp }

\end{document}

enter image description here