5

In a documentation application, where the version history of the document is listed in a tabular environment, I'd like to be able to silently extract the value of the last version number supplied, to use elsewhere. The people who type in the metadata can't (and shouldn't be able to) add markup to what they enter, so I thought of something like this:

Minimal NON-working example

\documentclass{article}
\usepackage{array}
\begin{document}
\def\foo{0}
\begin{tabular}{>{\gdef\foo\bgroup}c<{\egroup}cc}
Ver&Stuff&More stuff\\\hline
0.1&this&that\\
0.2&this&that\\
0.3&this&that\\
\end{tabular}
\end{document}

I am trying to end up with \foo as '0.3'. This is probably the wrong way to go about it, but has anyone a solution?

5

You're not far: collcell provides the trick.

\documentclass{article}
\usepackage{collcell}

\newcommand{\deffoo}[1]{\gdef\foo{#1}#1}
\def\foo{0}

\begin{document}

\begin{tabular}{>{\collectcell\deffoo}c<{\endcollectcell}cc}
Ver&Stuff&More stuff\\\hline
0.1&this&that\\
0.2&this&that\\
0.3&this&that\\
\end{tabular}

\foo

\end{document}

enter image description here

Your approach cannot work, because the replacement text for a \def (or friends) cannot be delimited by \bgroup and \egroup, only by explicit braces. Instead, we inject \collectcell\deffoo at the start of the cell and \endcollectcell at the end. Essentially, \collectcell is defined by

\def\collectcell#1#2\endcollectcell{#1{#2}}

so what's executed is \deffoo{<cell contents>}, which does what we need.

  • Oooh! Thanks, I hadn't come across this one before: I was searching for 'capture' rather than 'collect'. – Peter Flynn Nov 14 '16 at 9:31

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.