Accessing values from tables via "grid search"

I don't have the calculator package but this defines expandable macros with each of the cell contents.

enter image description here

\documentclass{scrartcl}

\usepackage{array,booktabs,filecontents,lmodern,siunitx}

\sisetup{round-mode=places,round-precision=2}

\makeatletter
\newcommand{\setvalue}[2]{%
  \@ifdefinable{#1}{\gdef#1{#2}}#2
}

\newcount\rowc
\newcount\colc
\def\xsavedata{%
}

\long\def\savedata#1{%
\setbox0\vbox\bgroup
\let\\\cr
\tracingall
\let\midrule\@empty
\let\multicolumn\@gobbletwo
\everyeof{\noexpand}%
\halign\bgroup
\global\colc\z@
\global\advance\rowc\@ne\@gobble{##}&&%
\global\advance\colc\@ne
\expandafter\xdef\csname data-#1(\the\rowc,\the\colc)\endcsname
{\zap@space## \@empty}\cr
\@@input #1 
\crcr\egroup\egroup}

\def\usedata#1#2{%
\csname data-#1(#2)\endcsname}

\makeatother


\DeclareRobustCommand{\estauto}[3]{%
 \vspace{.75ex}{%
 \begin{tabular}{l*{#2}{#3}}%
 \toprule%
 #1%
 \bottomrule%
 \addlinespace[.75ex]%
 \end{tabular}%
 }
}

\begin{filecontents}{mytable.tex}
                          & \multicolumn{1}{c}{(1)}   & \multicolumn{1}{c}{(2)}   & \multicolumn{1}{c}{(3)}   \\
\midrule
\emph{First Control Set}  &                           &                           &                           \\
Variable 1                &  1.85                     & 0.92                      & 1.11                      \\
                          & (0.34)                    & (0.24)                    & (0.14)                    \\
Variable 2                & 0.07                      & 0.07                      & 0.07                      \\
                          & (0.01)                    & (0.02)                    & (0.01)                    \\
Variable 3                & -0.02                     & -0.01                     & -0.01                     \\
                          & (0.01)                    & (0.00)                    & (0.00)                    \\
\end{filecontents}

\begin{document}
%
\savedata{mytable.tex}


 This does not work because it calls the value before the table is included (\usedata{mytable.tex}{3,3}).

\begin{table}\centering\footnotesize
    \caption{Consumer Credit Portfolios for Borrowers}
    \label{tab:portfolio}
    \estauto{\input{mytable.tex}}{3}{c}
\end{table}

This does work and the value is of Variable 1 in (1) is \usedata{mytable.tex}{3,3} and in
(2) \usedata{mytable.tex}{5,3}. %The spacing is not correct after the values are called.

Let's try some maths: The difference of (1) and (2) of Variable 1 is
%\SUBTRACT{\usedata{mytable.tex}{3,3}}{\usedata{mytable.tex}{5,3}}\DiffAB \num{\DiffAB}
% package calculator also has a round option, but I don't really understand the syntax

\end{document}

Here's an option that uses LuaTeX. There are three macros:

  • \myarray{name}{table} stores the table.
  • \usearray{name} returns the table.
  • \arrayitem{name}{row}{column} returns the item in position [row,column] of the table stored as name.

I don't have calculator either, so I made a small evaluation function just for example using expl3.


enter image description here


\documentclass{article}
\usepackage{luacode}
\usepackage{xparse}

\begin{luacode*}
myarrays = {}
function myarray(name,s)
    myarrays[name] = {}
    myarrays[name] = string.explode(s,"\\+")
    for i = 1,#myarrays[name] do
        myarrays[name][i] = string.explode(myarrays[name][i],"&")
    end
    myarrays[name].table = s
end
\end{luacode*}

\def\myarray#1#2{\directlua{myarray("#1","\luatexluaescapestring{\unexpanded{#2}}")}}
\def\usearray#1{\directlua{tex.sprint(myarrays.#1.table)}}
\def\arrayitem#1#2#3{\directlua{tex.sprint(myarrays.#1[#2][#3])}}

\ExplSyntaxOn
\NewDocumentCommand{\evaluate}{ m }
    {
        \fp_eval:n {#1}
    }
\ExplSyntaxOff
\begin{document}

Define table "tab1" \verb=\myarray{tab1}{1&2&3\\4&5&6\\}=.\par\medskip
\myarray{tab1}{1&2&3\\4&5&6\\}

Define table "tab2" \verb=\myarray{fish}{10&11&12\\-1&-2&-3\\}=\par\medskip
\myarray{tab2}{10&11&12\\-1&-2&-3\\}

The table "tab1" is:
\[
\begin{array}{ccc}
    \usearray{tab1}
\end{array}
\]

The table "tab2" is: 
\[
\begin{array}{ccc}
    \usearray{tab2}
\end{array}
\]

The product of entry [2,2] in "tab1" and [2,2] in "tab2" is \evaluate{\arrayitem{tab1}{2}{2}*\arrayitem{tab2}{2}{2}}

\end{document}

Tags:

Macros

Tables