2

I would like to use the column specification syntax of memoir's tabular environment to automatically wrap cells of a table in a macro. Is that possible? I tried the following

\documentclass{memoir}
\newcommand\bracketit[1]{[#1]}
\begin{document}
\begin{tabular}{l>{\bracketit\bgroup}c<{\egroup}}
    1 & A \\
    2 & B \\
\end{tabular}
\end{document}

which fails with

! Missing } inserted

I had hoped it would be equivalent to

\documentclass{memoir}
\newcommand\bracketit[1]{[#1]}
\begin{document}
\begin{tabular}{lc}
    1 & \bracketit{A} \\
    2 & \bracketit{B} \\
\end{tabular}
\end{document}

but without the typing. Of course, with the simple \bracketit, there is no need for a macro, I could insert the brackets directly. However, the macro that I want to apply is not that simple and out of my hands (mhchem.sty's \ce). Is what I try to do possible at all?

2

This is possible with the collcell package:

\documentclass{memoir}
\usepackage{collcell}
\newcommand\bracketit[1]{[#1]}
\begin{document}
  \begin{tabular}{l>{\collectcell\bracketit}c<{\endcollectcell}}
    1 & A \\
    2 & B \\
  \end{tabular}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.