# logarithm, sine, cosine etc evaluated in tabular

I know that I can use spreadtab to make automatic calculations in a tabular. However, I only managed to do simple operations like +-*/, but I have no idea how to make more complex ones like log, ln, sin, cos, tan, exp.

I tried to insert some fp functions in the spreadtab, but only receive errors.

Here is a minimal example:

\documentclass{standalone}
\usepackage{fp}

\begin{document}

@number & @$\ln$(number)\\
10 & \FPln{\result}{a2} \result

\end{document}


It gives the error: Missing = inserted for \ifnum. \end{spreadtab}.

• – Peter Grill Nov 22 '14 at 20:52
• You are right, standalone class was the wanted result. – Togh Nov 22 '14 at 20:57

It seems you can use the ln and sin functions directly:

## Code:

\documentclass{article}
\usepackage{booktabs}
\usepackage{fp}

\usepackage{numprint}
\npdecimalsign{.}
\renewcommand\STprintnum[1]{\numprint{#1}}

\begin{document}
@$x$ & @$\ln(x)$ & @$\sin(x)$ [$x$ in degrees] \\
\cmidrule(lr){1-1}
\cmidrule(lr){2-2}
\cmidrule(lr){3-3}
15  & ln(a2)    & sin(a2*pi/180) \\
30  & ln(a3)    & sin(a3*pi/180) \\
45  & ln(a4)    & sin(a4*pi/180) \\
90  & ln(a5)    & sin(a5*pi/180) \\
\bottomrule

\end{document}

• You are right, and sin(), cos() also. My bad I did not check that correctly, I tried with log() and didn't found informations about theses functions in the spreadtab manual. Should I delete my question, or would it be better to keep it so other distracted people may find the answer? – Togh Nov 22 '14 at 20:23
• @Togh: My usual opinion is to not delete a question -- What you did is exactly what I would have tried, so am sure that others will run into the similar problems in the future. – Peter Grill Nov 22 '14 at 20:26
• Oops. Well you might be interested in a more powerful way of dealing with mathematical calculations which I've already posted below. – DJP Nov 22 '14 at 20:37
• ln(), exp(), ^, sin(), cos() and tan() works too. – Togh Nov 23 '14 at 9:45

A solution using the powerful xfp package:

\documentclass{article}

\def\Precision{4} % calculation precision

\usepackage{multido}
\usepackage{booktabs}
\usepackage[
round-mode = places,
round-precision = \Precision,
group-digits = false
]{siunitx}
\usepackage{xfp}

\begin{document}

% http://tex.stackexchange.com/a/201554/15874
\makeatletter
\newcommand*\skyIDtable{}
\multido{\i = 1+1}{10}{\protected@xdef\skyIDtable{\skyIDtable
\i
& \fpeval{round(ln(\i),\Precision)}   % logarithmic
& \fpeval{round(sind(\i),\Precision)} % sine (degrees)
& \fpeval{round(tand(\i),\Precision)} % tangent (degrees)
& \fpeval{round(exp(\i),\Precision)}  % exponential
\\}}
\makeatother

\begin{table}
\begin{tabular}{
S[table-format =  2]
S[table-format =  1.\Precision]
S[table-format = -1.\Precision]
S[table-format =  1.\Precision]
S[table-format = -1.\Precision]
S[table-format =  1.\Precision]
S[table-format =  5.\Precision]
}
\toprule
{$x$} & {$\ln x$} & {$\sin x$}     & {$\sin x$}     & {$\tan x$}     & {$\tan x$}     & {$\exp x$} \\
{---} & {---}     & {\si{\radian}} & {\si{\degree}} & {\si{\radian}} & {\si{\degree}} & {---}      \\
\midrule
\skyIDtable
\bottomrule
\end{tabular}
\end{table}

\end{document}


Update

Note that log_[b], i.e. the logarithm in base b, is not implemented yet.

LaTeX was designed for typesetting. If you're going to use it for mathematical calculation too then you should consider the sagetex package. It's using the power of a computer algebra system SAGE allows you to calculations and plot and place them anywhere you want. In the case of tables you can have Sage typeset them for you (as in the example below) which saves a lot of time and eliminates mistakes. The sagetex package requires you to have access to Sage. You can get that through a free Sagemath Cloud account or you can install Sage locally on your computer.

\documentclass[12pt]{article}
\usepackage{sagetex}
\usepackage[margin=.5in]{geometry}
\begin{document}
\pagestyle{empty}
\begin{sagesilent}
var('x')
f(x) = sin(x*pi/180.)
g(x) = cos(x*pi/180.)
h(x) = tan(x*pi/180.)
m(x) = tan(x)

output = ""
output += r"\begin{tabular}{ccccccccc} "
output += r" degrees & sine & cosine & tangent & & degrees & sine & cosine &   tangent \\ \hline "
for i in range(1, 5):
output += r"%d & %8.4f & %8.4f & %8.4f & & %d & %8.4f & % 8.4f & %12.4f  \\ "%(i, f(i), g(i), h(i), i+45, f(i+45), g(i+45), h(i+45))
output += r"\end{tabular}"
\end{sagesilent}
\begin{center}
\sagestr{output}
\end{center}
You can do the traditional table entering data cell by cell:\\
\begin{tabular}{c|c|c}
$\sage{f(45).n(digits=5)}$ & $\sage{cos(pi/3).n(digits=4)}$ &   $\sage{log(8,2).n(digits=4)}$\\
\hline
$\sage{ln(7).n(digits=4)}$ & $\sage{m(pi/3).n(digits=4)}$ &      $\sage{exp(2).n(digits=5)}$\\
\end{tabular}

Or you can do calculations inline:
What's the value of $e^{5}$? It's $\sage{(e^5)}$. That's not helpful.
What is it as a decimal? It's $\sage{(e^5).n(digits=8)}$.

What's $\log_2(8)$? It's $\sage{log(8,2)}$.
\end{document}


Here is a screenshot of the code running in Sagemath Cloud:

Note that the for loop beginning with for i in range(1, 5): produces 4 lines of the table because i must be less than the last number. You can change 5 to a bigger number and have Sage create the table for you.

• Using python could be a more familiar programming way to me, good to have pointed this possibility to use sage. Does it require -shell-escape ? – Togh Nov 22 '14 at 21:00
• Sagetex gives you both Python and Sage commands. It requires -shell-escape but if you're using Sagemath Cloud then everything is taken care of automatically. – DJP Nov 22 '14 at 22:21