# Automatically generate the value table of a mathematical function

Is there an easy way to get the value table of a mathematical function?

Consider the following example and pseudo-code:

\documentclass{article}

\usepackage{pgfplots}

\usepackage[nomessages]{fp}% http://ctan.org/pkg/fp
\usepackage{xstring}% http://ctan.org/pkg/xstring
\usepackage{xparse}% http://ctan.org/pkg/xparse

\usepackage[locale=DE]{siunitx}

\begin{document}

\def\f(#1){#1^2} %%It should also be possible to use gnuplots x**2 syntax for the value table

\sisetup{zero-decimal-to-integer}
\def\pf(#1){\pgfmathparse{\f(#1)}\num{\pgfmathresult}}

\begin{center}
\begin{tikzpicture}
\begin{axis}[domain=-2:10]
\end{axis}
\end{tikzpicture}
\end{center}

Then I want a value table, something like generated automatically.

%% \gen_value_table[samples at={1,...,10},orientation=horizontal]{\f}

\begin{tabular}{*{10}{c|}c}
$x$&   1      & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10  \\\hline
$f(x)$ & \pf(1) & \pf(2)  & \pf(3)  & \pf(4)  &  \pf(5) & \pf(6)  &\pf(7)   & \pf(8)  & \pf(9)  & \pf(10)
\end{tabular}

%% \gen_value_table[samples at={1,2,3.5,10},orientation=horizontal]{\f}

\begin{tabular}{*{4}{c|}c}
$x$ & 1      & 2 & 3.5  & 10  \\\hline
$f(x)$ & \pf(1) & \pf(2)  & \pf(3.5)  & \pf(10)
\end{tabular}

%% \gen_value_table[samples at={1,2,3.5,10},orientation=]{\f}

\begin{tabular}{c|c}
$x$ & $f(x)$ \\\hline
1 & \pf(1) \\
2 & \pf(2) \\
3,5 & \pf(3.5) \\
10 & \pf(10)
\end{tabular}

\end{document}

• If you instead use declare function then you can use pgfplotstable to create whatever function you wish to have with a create on use/.expr – percusse Jan 29 '16 at 14:17
• possible starting point? tex.stackexchange.com/questions/126456/… – cmhughes Jan 29 '16 at 15:34
• My answer to this question used the sagetex package to create a trig table. Sage has lots of built in functions and the for-loops mean the code is short. – DJP Jan 29 '16 at 23:58

As percusse and cmhughes have pointed out already in their comments, one can try to accomplish your needs with the pgfplotstable package. Here some code for a starting point, which I think works quite well for vertical tables, but lack for horizontal tables. Although the solution isn't perfect, it hopefully is a good starting point to experiment with.

Please note, that you the key samples at cannot be used with \addplot gnuplot. This always uses domain and samples to produce the values.

\documentclass[
ngerman,
english,    % <-- comment me to use German language
DIV=12,
]{scrartcl}
\usepackage{babel}
\usepackage{caption}
\usepackage{booktabs}
\usepackage{siunitx}
\sisetup{
% just to show where the \num' command is used
color=green,
}
% this adds the German and UK number format styles to the siunitx'
% as well as to the tikz/pgf settings and uses them according to
% the actual active language
\usepackage{pgfplots}
\usepackage{pgfplotstable}
% define maximum number of entries in the table
\def\MaxItems{13}
% define which samples should be used (in the plot and table)
\def\Samples{1,1.5,2,3,...,10}

% define stuff that should be used all over the document
\pgfplotstableset{
% to show when there are defined too much cells
empty cells with={NaN},
% -----
% define how the new' column shall be filled:
create on use/x/.style={
%            % unfortunately using \Samples' here causes an error;
%            % don't know why
%            create col/set list=\Samples
% so the list entries have to be given here explicitly
create col/set list={1,1.5,2,3,...,10}
},
% create columns that uses the function you need the values for
create on use/x square/.style={
% this fills cells with 0' (zero), when \MaxItems' is
% larger than the set list' (or \Samples')
create col/expr={\thisrow{x}^2}
},
create on use/minus x cube/.style={
create col/expr={-1*\thisrow{x}^3}
},
% -----
% define headers for the corresponding columns
columns/x/.style={
column name=$x$,
dec sep align,
},
columns/x square/.style={
column name=$x^2$,
},
columns/minus x cube/.style={
column name=$-x^3$,
dec sep align,
},
% ----------------------
% I cannot find the following key in the manual
% it doesn't cause an error but also does not  do anything
% --> is it intended that one isn't able to set a style for all columns
%     or am I missing something in the manual that this can be done?
every column/.append style={
dec sep align,
},
% ----------------------
%        % if only vertical tables would be used,
%        % one could fix the column name
%        every first column/.style={
%            column name=$x$,
%        },
% -----
% use booktabs to make tables look nicer
before row=\toprule,
after row=\midrule,
},
every last row/.style={
after row=\bottomrule,
},
}
% create a new table with \MaxItem' rows and column 'new':
\pgfplotstablenew[
columns={
x,
x square
},
%    % ---------------------------------
%    % save table to disc, if you want
%    \pgfplotstablesave[
%        columns={
%            x,
%            x square
%        },
%    % ---------------------------------

\sisetup{zero-decimal-to-integer}
\def\pf(#1){\pgfmathparse{\f(#1)}\num{\pgfmathresult}}
\def\f(#1){#1^2} %%It should also be possible to use gnuplots x**2 syntax for the value table

\begin{document}

\section{Create plot}

\begin{center}
\begin{tikzpicture}
\begin{axis}[
domain=-2:10,
]

% define the function to plot
% (it can also be included directly to the argument after
%  expression'. It is only defined here so you don't have to
%  change it at several places when the function changes.)
\def\Function{x^2}

% plot the (continuous) function
% (in the domain' with samples')
expression {\Function};
% plot points at samples at'
expression {\Function};

% plot points from table
% (the points were stated at set list')
%            % you can also plot the points from saved table.
%                table {table-out.txt};

\end{axis}
\end{tikzpicture}
\end{center}

\section{Create tables automatically''}

Then I want a value table, something like generated automatically.

\begin{center}
\captionof{table}{Original manual vertical table (\emph{left}) and new
automatic'' table (\emph{right})}
%% \gen_value_table[samples at={1,2,3.5,10},orientation=]{\f}
\begin{tabular}{c|c}
$x$ & $f(x)$   \\\hline
1   & \pf(1)   \\
2   & \pf(2)   \\
3,5 & \pf(3.5) \\
10  & \pf(10)  \\
\end{tabular}
%
% show (automatic'') table
%
\pgfplotstabletypeset[
columns={
x,
x square
},
]{
x
1
2
3.5
10
}
\end{center}

\begin{center}
\captionof{table}{Original manual horizontal table (\emph{top}) and new
automatic'' table (\emph{down})}
%% \gen_value_table[samples at={1,...,10},orientation=horizontal]{\f}
\begin{tabular}{*{10}{c|}c}
$x$&   1      & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10  \\\hline
$f(x)$ & \pf(1) & \pf(2)  & \pf(3)  & \pf(4)  &  \pf(5) & \pf(6)  &\pf(7)   & \pf(8)  & \pf(9)  & \pf(10)
\end{tabular}
\vspace{2ex}

% -----
% up to here everything is fine, but when transposing the table an
% additional header row is created (--> don't know how it can be suppressed).
% Also I don't know why the LaTeX'' column headers get lost and only the
% column header from the table is printed
\pgfplotstabletypeset[
every first column/.append style={
string type,
},
input colnames to=,
\end{center}

\end{document}


• The plot isn't accepting a Function that contains float numbers. Would you mind fixing that? – Joel Duscha May 12 '16 at 18:44
• @JoelDuscha, could you be more precise by stating such a function? A quick test revealed no problems on my side. – Stefan Pinnow May 12 '16 at 19:33
• Q(t) = 2e^{-6/75}t} cos(2\pi / 24.65 * t) ! Package PGF Math Error: Sorry, an internal routine of the floating point unit got an ill-formatted floating point number 1Y1.59997986e-1]'. The unreadable part was near '1Y1.59997986e-1]'. – Joel Duscha May 12 '16 at 19:46
• @JoelDuscha, sorry, but that is not a function I can work with, because it shouldn't work at all containing curly braces and no multiplication sign between "elements". I suggest you ask a follow up question with a MWE where at least the plot is working. Paste the question here as a comment so I don't have to look all through the list. Then I'll have a look at it. – Stefan Pinnow May 12 '16 at 20:55

I found this package solving my problem:

\documentclass{article}
\usepackage{array}
\usepackage{amsmath}
\usepackage{tabularcalc} % tabellen van functiewaarden
\begin{document}
\tcsetcoltype{|r|}{>{\centering\arraybackslash}m{1.5cm}|}
\htablecalc [1]{$x$}{x=-3,-2,-1,0,1,2,3} {$f(x)$}{2*x+3}
\end{document}
`