# Numerical calculations inside table environment

I am doing a coursework in mechanics and would like to write both the theory and calculations in LaTeX. However, as design is iterative process, I would like to escape the tedious cycle of:

1. Select starting values for diameters, lengths etc.
2. Go through numerous steps, calculating derived values.
3. The last step is stress-strength analysis and if that fails, go to step one.

Here is what I have got so far.

\documentclass{article}
\usepackage[nomessages]{fp}  % http://ctan.org/pkg/fp
\usepackage[table]{xcolor}     % For coloured rows within tables.

\begin{document}

\FPset{vAlpha}{20}
\rowcolors{1}{white}{lightgray}
\begin{tabular}{l | c | c | c | c | c | c | c | c}
Gear & $\alpha$ & $h_a$, [mm]             & c, [mm] & $h_f$, [mm] & $\rho$, [mm] & $h_w$, [mm] & h, [mm] & s, [mm] \\
G11  & \vAlpha  & \FPprint{vAlpha * 0.25} &         &             &              &             &         &          \\
\end{tabular}

\end{document}


As is evident, I am attempting to make calculations inside the table and directly display the result. It is not working (the third column is outputting nothing).

I know that \FPeval or \FPmul would work in this case, but I find them too cumbersome for the following reason. They both require me to assign the result to a variable, which I can later \FPprint. This is not very useful to me, as I would then need to compute a large number of variables and then invoke \FPprint inside the table.

I am not going to be using the computed result for anything else than displaying it in the table. All entries are a variable (different variables) multiplied by a constant. What is the best solution?

• Using LaTeX to do calculations is like a dancing bear. What is impressing is not how well the bear dances, but that it dances at all. – John Kormylo Sep 30 '15 at 14:23
• @JohnKormylo, could you propose a more efficient workflow. I have tried matlab and python scripts, to mix text and calculations . In those cases the bear dances masterfully, but is ugly as hell. – Vorac Sep 30 '15 at 17:57
• I tend to use C programs to calculate and create comma delimited files which I read using pgfplotstable. – John Kormylo Sep 30 '15 at 21:20
• @JohnKormylo, thanks, I looked at the documentation of pgfplotstable and it seems a rather comprehensive package. You can expect a flurry of questions about it in a couple of weeks, when I find myself writing my graduation paper. Any reason why you prefer C to a higher-level language, e.g. python? – Vorac Nov 3 '15 at 9:37

% Command to multiply a variale by a floating point number.
\newcommand*{\Mul}
{
\FPeval{vTemp}{#1 * #2}
\FPround\vTemp{\vTemp}{4}  % Round to four digits after decimal point.
\FPprint{vTemp}
}


Not really a general solution, but work for the concrete question.

If you have R installed, you can use Sweave or knitr to put all mathematical operations from R in LaTeX. Save the MWE below with a .Rnw extension and compile with:

R CMD Sweave --pdf <filename>.Rnw

This make directly a <filename>.pdf. For more complex documents use Rstudio or in a terminal:

• R CMD Sweave <filename>.Rnw
• Then compile <filename>.tex as usual

The MWE:

 \documentclass{article}
\begin{document}
\begin{tabular}{lll}
G11  & \Sexpr{vAlpha=20;vAlpha} & \Sexpr{vAlpha * 0.25}  \\
\end{tabular}
\end{document}