2

I have data presented in tabular environments and I want to process it, in LaTeX, in ways that require for multiple cells to be analyzed together. That's in addition to typesetting the cells themselves.

For example, my source file has things like

\begin{mytabularpresentation}
  stuff & more stuff & yes \\
  data & more data & no \\
\end{mytabularpresentation}

In addition to showing “stuff”, “yes”, etc., I want to process “stuff” and “yes” (i.e. I want to run \somecommand{stuff}{yes}), “data” and “no”, and so on.

Here's a M-ish WE with a toy example. I use global variables to store cell contents and recall them later on the same row.

\documentclass{article}
\usepackage{calc}
\usepackage{array}
\usepackage{collcell}
\newcommand{\labelcell}[1]{#1}
\newcolumntype{L}{>{\collectcell\labelcell}l<{\endcollectcell}}
\newcommand{\aye}[1]{\gdef\ayecontents{#1}#1}
\newcolumntype{A}{>{$\collectcell\aye}r<{\endcollectcell$}}
\newcommand{\bee}[1]{\gdef\beecontents{#1}#1}
\newcolumntype{B}{>{$\collectcell\bee}r<{\endcollectcell$}}
\newcolumntype{R}{>{$}r<{$}}
\newcounter{result}
\newcommand{\calcvalue}[1]{\setcounter{result}{#1}\theresult}
\begin{document}
\begin{tabular}{LABR}
  sum & 3 & 2 & \calcvalue{\ayecontents+\beecontents} \\
  product & 3 & 2 & \calcvalue{\ayecontents*\beecontents} \\
  negative & 3 & & \calcvalue{-\ayecontents} \\
  square root & \omit & 4 & \sqrt{\beecontents} \\
  \omit & 76 & 89 & \calcvalue{\ayecontents*(42-\beecontents)} \\
\end{tabular}
\end{document}

This is pretty fragile. In my real use cases, I run into complications such as cells with \omit (which must be detected, rather than reusing the content from the previous row) and nested tables.

One thing I want to do, which the example above doesn't do, is collect information for the whole row. I thought of \everycr, but it isn't really supported in LaTeX, and when I tried forcing it, it caused errors, I think because I have some nonexpandable \everycr content which ends up at the beginning of the next row's first cell. MNWE:

\documentclass{article}
\usepackage{array}
\makeatletter
\newcommand{\ialignwitheverycr}[1]{%
}
\newenvironment{tabularwitheverycr}[1]{%
  \let\orig@ialign\ialign%
  \def\ialign{\everycr{#1}\tabskip\z@skip\let\ialign\orig@ialign\halign}%
  \tabular%
}{%
  \endtabular%
}
\makeatother
\begin{document}
\begin{tabularwitheverycr}{\def\stuff{}}{rl}
  one & two \\
  \multicolumn{1}{l}{hello} & world \\
\end{tabularwitheverycr}
\end{document}

Error: Misplaced \omit. on the \multicolumn call (plus a flurry of consequences).

How can I collect data from multiple columns in each row and act on them? Is there a ready-made package for that? If I need to roll my own, is there a better way than global variables, and how can I “finish up” a row?

Note that I want to keep the source as is. I am not changing my source to \somekindofrow{sum}{3}{2}{\calcvalue{\ayecontents+\beecontents}}.

  • Why do you want to do data processing in LaTeX rather than with a tool designed for the job? At best, you'll end up with a relatively slow, relatively inflexible and relatively fragile solution because this isn't really what the system is designed for. – cfr Jun 01 '16 at 01:09
  • At least nesting is addressed by egreg's answer, isn't it? – cfr Jun 01 '16 at 01:13
  • @cfr Please don't judge the usefulness of the requirements on the toy example used to demonstrate the problem! I have data in LaTeX format (as I wrote, I don't want to change the source format), and the end consumer of the analysis is also LaTeX (not just typeset information, but also “inner” things like automatic generation of labels). – Gilles 'SO- stop being evil' Jun 01 '16 at 01:19
  • It is not a judgement of usefulness but of the suitability of the tool. Even if the input and output are LaTeX code, it doesn't follow that the best tool to produce the latter from the former is LaTeX. – cfr Jun 01 '16 at 01:36
  • Do you know http://tex.stackexchange.com/questions/70860/? – cgnieder Jan 18 '17 at 22:27
  • @clemens No, and I hadn't heard of spreadtab, either, so thank you. But can it be useful for what I want to do? At first glance I don't see anything that would help with collecting data from multiple cells in the same row. – Gilles 'SO- stop being evil' Jan 18 '17 at 22:30
  • @Gilles ? Your first table with spreadtab: \begin{spreadtab}{{tabular}{l*{3}{>{$}r<{$}}}} @ sum & 3 & 2 & b1+c1 \\ @ product & 3 & 2 & b2*c2\\ @ negative & 3 & & -1*b3 \\ @ square root & & 4 & \sqrt{:={c4}} \\ & 76 & 89 & b5*(42-c5) \\ \end{spreadtab} – cgnieder Jan 18 '17 at 22:53
  • @clemens Read further. That works for my ultra-simplified example to set the tone, but not for my real use case, and I give a second slightly less simplified example to convey this. “One thing I want to do, which the example above doesn't do, is collect information for the whole row. …” – Gilles 'SO- stop being evil' Jan 18 '17 at 22:54
  • “collect information for the whole row” is a very vague statement. You also asked “How can I collect data from multiple columns in each row and act on them” which is possible with spreadtab as long as the data are numbers. But anyways – if spreadtab can't be of use for you don't use it. :) I don't know it much better then you do – I have rarely if ever used it myself… – cgnieder Jan 18 '17 at 23:02

0 Answers0