6

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?

1 Answer 1

6

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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .