I'm trying to create a general pgfplotstable
style for displaying contingency tables (the below example is taken from Wikipedia) of any sort but I am having trouble computing the totals for each row and column and adding them to the table on the fly.
I am aware that totaling up the row will involve creating a column and using \pgfmathaccuma
, while the totals row should be done via \pgfplotstablevertcat
but the particulars seem to be beyond me even after repeated reading of the documentation.
The solution should work for different tables with different number of columns/rows and different labels (though the totaled column and row can both be called Totals, they're just named differently here for reasons of clarity).
NB: I've included a hand-formatted table of the desired output below and my efforts so far (which use the actual table format I want to work with).
\documentclass[11pt,a4paper,oneside]{report}
\usepackage{booktabs}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\begin{document}
\section*{Desired output}
\pgfplotstabletypeset[
every head row/.style={%
before row={\toprule
& \multicolumn{2}{c}{Handedness}\\ \cmidrule{2-3}},
after row=\midrule},
every last row/.style={after row=\bottomrule},
columns/Gender/.style={string type},
]{
Gender Right-handed Left-handed {Total by Gender}
Male 43 9 52
Female 44 4 48
{Total by Handedness} 87 13 100
}
\section*{Current efforts}
\pgfplotstableread{
Gender Right-handed Left-handed
Male 43 9
Female 44 4
}\chisquaredata
\pgfplotstabletypeset[
% Does not total up correctly
create on use/{Total by Gender}/.style={
create col/expr={\pgfmathaccuma + \thisrow{\pgfplotstablecolname}}},
every head row/.style={%
before row={\toprule
& \multicolumn{2}{c}{Handedness}\\
\cmidrule{2-3}},
after row=\midrule},
every last row/.style={after row=\bottomrule},
columns/Gender/.style={string type},
columns={Gender, Right-handed, Left-handed, {Total by Gender}},
]\chisquaredata
\end{document}