# How to do simple calculation in latex tables

Here I wish to do three things:

1) I want my table to be like this one below

whereas mine is

2) I want numbers to appear in the same format as their heading p (in bold)

3) I want latex to do some calculation. p in my table represents the proportion of females (females/(males + females)). I am wondering whether latex can do that for me because I have 16 groups and don't wan to calculate each?

% !TEX encoding = UTF-8 Unicode
\documentclass[a4paper,12pt]{report}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[turkish]{babel}
\usepackage{amsmath}
\usepackage{amssymb}

\begin{document}

\begin{table}[h!]
\centering
\begin{tabular}{c|c|c|r}

Group No & Females  & Males & \textbf{p} \\ \hline
1 & 18 & 11 &  \\
2 & 31 & 22 &  \\
3 & 34 & 27 &  \\
4 & 33 & 29 &  \\
5 & 27 & 24 &  \\
6 & 33 & 29 &  \\
7 & 28 & 25 &  \\
8 & 23 & 26 &  \\
9 & 33 & 38 &  \\
10 & 12 & 14 &  \\
11 & 19 & 23 &  \\
12 & 25 & 31 &  \\
13 & 14 & 20 &  \\
14 & 2 & 20 &  \\
15 & 22 & 6 &  \\
16 & 7 & 34 &  \\
\end{tabular}
\end{table}
\end{document}


Here's a solution with collcell and xparse (for \fp_eval:n)

\documentclass{article}
\usepackage{collcell,booktabs,siunitx}
\usepackage{xparse}

\newcolumntype{F}{>{\collectcell\rememberF}c<{\endcollectcell}}
\newcolumntype{M}{>{\collectcell\rememberM}c<{\endcollectcell}}
\newcolumntype{P}{>{\collectcell\computeP}r<{\endcollectcell}}

\newcommand{\rememberF}[1]{%
\gdef\FEMALE{#1}#1%
}
\newcommand{\rememberM}[1]{%
\gdef\MALE{#1}#1%
}

\ExplSyntaxOn
\NewDocumentCommand{\computeP}{}
{
\bfseries\fp_eval:n { round ( \FEMALE/(\FEMALE+\MALE) , 2 ) }
}
\ExplSyntaxOff

\begin{document}

\begin{tabular}{c F M P}
\toprule
Group No &
\multicolumn{1}{c}{Females} &
\multicolumn{1}{c}{Males} &
\multicolumn{1}{c}{\textbf{p}} \\
\midrule
1 & 18 & 11 &  \\
2 & 31 & 22 &  \\
3 & 34 & 27 &  \\
4 & 33 & 29 &  \\
5 & 27 & 24 &  \\
6 & 33 & 29 &  \\
7 & 28 & 25 &  \\
8 & 23 & 26 &  \\
9 & 33 & 38 &  \\
10 & 12 & 14 &  \\
11 & 19 & 23 &  \\
12 & 25 & 31 &  \\
13 & 14 & 20 &  \\
14 & 2 & 20 &  \\
15 & 22 & 6 &  \\
16 & 7 & 34 &  \\
\bottomrule
\end{tabular}

\end{document}


Usage of booktabs features is not mandatory, but the table layout is better without vertical rules.

The F and M column types remember the entry in \FEMALE and \MALE respectively and print them, the P column type computes the ratio and prints it. In the first row it's necessary to use \multicolumn so as to avoid these macros to kick in.

The number of decimal digits can be changed in the \computeP macro, the value I used is 2.

• A quick check might be necessary to make sure that \FEMALE+\MALE is not zero. But that would be an added bonus. – Werner Oct 31 '14 at 20:58
• @Werner Well, I guess it wouldn't be a meaningful statistics if both groups have zero members. – egreg Oct 31 '14 at 21:00

Another solution, using the spreadtab package, and also siunitx, booktabs and makecell, to improve the look of the table. The spreadtab package allows to define computing formulae, among which I use \STcopy{v}{formula}, where the v argument makes the formula be translated for all cells of the same column.

Another feature is I defined a rowno counter, so the row numbers in the first column be automatically displayed without having to type them.

\documentclass[a4paper,12pt]{report}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
%\usepackage[turkish]{babel}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{makecell}
\usepackage{siunitx}
\sisetup{table-format = 2.0, table-number-alignment=center}
\newcounter{rowno}

\begin{document}

\begin{table}[h!]
\centering\setcounter{rowno}{0}
\STautoround{2}
\toprule
@ \multicolumn{1}{c}{\thead{Group\\ No}} & @ \multicolumn{1}{c}{Females} & @ \multicolumn{1}{c}{Males} & @p\\
\midrule
& 18 & 11 & \STcopy{v}{b2 /(b2+ c2)}\\
& 31 & 22 & \\
& 34 & 27 & \\
& 33 & 29 & \\
& 27 & 24 & \\
& 33 & 29 & \\
& 28 & 25 & \\
& 23 & 26 & \\
& 33 & 38 & \\
& 12 & 14 & \\
& 19 & 23 & \\
& 25 & 31 & \\
& 14 & 20 & \\
& 2 & 20 & \\
& 22 & 6 & \\
& 7 & 34 & \\
\bottomrule
\end{table}

\end{document}