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 1 '16 at 1:09
  • At least nesting is addressed by egreg's answer, isn't it? – cfr Jun 1 '16 at 1: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 Jun 1 '16 at 1: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 1 '16 at 1:36
  • Do you know tex.stackexchange.com/questions/70860? – clemens Jan 18 '17 at 22:27

Your Answer

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

Browse other questions tagged or ask your own question.