3

I have a longtabu that spans multiple pages. I would like to be able to provide a page total that sums and/or counts the values in one or more columns.

Does anyone have an idea how this might be achieved?

Hugh
  • 31
  • My opinion: It would be much easier to assemble the table in something like Excel and calculate the sum and hard-code it into the document. – Werner Jun 14 '16 at 17:31
  • I plan to do that for grand totals, category subtotals and such. However I need to dynamically added data to my document and I have a requirement to provide page totals. Is that possible? – Hugh Jun 14 '16 at 19:10
  • I was thinking to just add the subtotals to one of the rows in the table. – Hugh Jun 14 '16 at 19:39
  • The main problem is that the entire table is formatted at once then broken into pages. Your only option is to record which are the last rows for each page and enter the corresponding subtotals manually. – John Kormylo Jun 15 '16 at 16:55

1 Answers1

1

You still have to know which is the last row on each page, but this should make things easier. If the column uses reals instead of integers, you can use pgfmath.

Note: the dummy column comes from Is there an incompatibility between collcell, longtable, and/or arydshln?

\documentclass{article}
\usepackage{array}
\usepackage{collcell}
\usepackage{longtable}

\newcounter{runningsum}

\newcommand{\Smacro}[1]% see column type S
{#1\addtocounter{runningsum}{#1}}%

\newcolumntype{S}{>{\collectcell\Smacro}{r}<{\endcollectcell}}

\begin{document}
\Large
\begin{longtable}{S@{}c}
1&\\
2&\\
3&\\
4&\\
5&\\
6&\\
7&\\
8&\\
9&\\
10&\\
11&\\
12&\\
13&\\
14&\\
15&\\
16&\\
17&\\
18&\\
19&\\
20&\\
21&\\
22&\\
23&\\
24&\\
25&\\
26&\\
27&\\
28&\\
29&\\
\hline
\multicolumn{1}{r@{}}{\therunningsum\setcounter{runningsum}{0}}&\\
30&\\
31&\\
32&\\
33&\\
34&\\
35&\\
36&\\
37&\\
38&\\
39&\\
40&\\
\hline
\multicolumn{1}{r@{}}{\therunningsum\setcounter{runningsum}{0}}&
\end{longtable}
\end{document}
John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • You're passing \bgroup as argument to \Smacro. You want to look at the collcell package, but the table foot is ”frozen", so I don't think it works anyway. – egreg Jun 15 '16 at 07:43
  • @egreg - thanks, i needed that. I can always just reserve space with the table foot and overlay using everypage. – John Kormylo Jun 15 '16 at 15:12
  • Thanks for the help. Since this seemed to be challenging we decided to drop the page totals. – Hugh Jul 05 '16 at 20:37