# How to measure the width of a longtable dynamically and use this width in footer?

I'm having trouble computing the width of a longtable and using it later in the table. This site offers several suggestions and I have implemented one of them. However, because of scope, or because of some of the nuances of longtable, the \global\advance\variable trick isn't working properly.

Below is my current output. The NOTES are supposed to extend to the last column.

Here is the code that produces the figure above. I realize that there are several tricks to get more sophisticated with the use of newenvironment and newcolumntype, but for purposes of debugging, I got close to bare metal. A previous question and answer on this side helped clean up the table and the command \rulefiller is a result of this.

\documentclass[11pt]{article}

\usepackage{array,multirow,ctable,longtable}
\usepackage{color,colortbl}

\setlength{\aboverulesep}{0pt}
\setlength{\belowrulesep}{0pt}
\setlength{\extrarowheight}{0.25ex}

\newlength{\spacebeforetotals}
\setlength{\spacebeforetotals}{0.5ex}
\newlength{\noteswidth}

\newlength{\spacebeforenotes}
\setlength{\spacebeforenotes}{0.0pt}

\definecolor{pantone120}{cmyk}{0.00,0.06,0.60,0.00}  % Pantone 120
\definecolor{buzzgold}{cmyk}{0.00,0.34,1.00,0.12}   % Buzz Gold

\newcommand{\tablerulecolor}{black}

\newcommand*{\rulefiller}{%
\specialrule{\heavyrulewidth}{0pt}{-\heavyrulewidth}% "invisible" rule
\arrayrulecolor{\tablerulecolor}% revert to regular line colour
}
\newsavebox{\mycolwidthbox}% box to save cells in

\begin{document}

\section{Research}
Table \ref{tab:research1} presents a summary of sponsored research by funds source over time.
\setlength{\global\noteswidth}{-\tabcolsep}%
\textsf{%
% latex table generated in R 2.13.1 by xtable 1.6-1 package
% Mon Oct 24 07:31:24 2011
{\footnotesize
\begin{longtable}{L
\nopagebreak & \multicolumn{7}{@{}L}{\scriptsize } & \\%
\nopagebreak & \multicolumn{7}{@{}L}{\small {\tablename\ \thetable{}}} & \\%
\nopagebreak & \multicolumn{7}{@{}L}{\small {\textbf{Research expenditures by funds source over time}}} & \\%
\nopagebreak & \multicolumn{7}{@{}L}{\scriptsize{dollars}} & \\%
\nopagebreak\rulefiller\cmidrule[\heavyrulewidth]{2-8}
\nopagebreak & variable & FY05 & FY06 & FY07 & FY08 & FY09 & FY10 & \\%
\nopagebreak\rulefiller\cmidrule[\lightrulewidth]{2-8}
\nopagebreak & \multicolumn{7}{C}%
{\tablename\ \thetable{} -- continued from previous page} & \\%
\nopagebreak\rulefiller\cmidrule[\heavyrulewidth]{2-8}
\nopagebreak & variable & FY05 & FY06 & FY07 & FY08 & FY09 & FY10 & \\%
\nopagebreak\rulefiller\cmidrule[\lightrulewidth]{2-8}
\nopagebreak\rulefiller\cmidrule[\heavyrulewidth]{2-8}
\nopagebreak & \multicolumn{7}{R@{}}{{Continued on next page}} & \\%
\endfoot%
\nopagebreak\rulefiller\cmidrule[\heavyrulewidth]{2-8}%
\nopagebreak & \multicolumn{7}{@{}L}{%
\begin{minipage}[t]{\noteswidth}\vspace{\spacebeforenotes}\scriptsize\noindent
NOTES: Research Expenditures represents the numbers of contracts and the amounts of expenditures for each department or research center for the given fiscal year.  Research Expenditures include cost sharing and matching funds; administrative, indirect, and other overhead expenses to the extent that they have been charged to a particular grant or other funding source; teaching, if instruction has formed a part of a separately funded research program or is significantly experimental in nature; and engineering-related research.
\end{minipage}
} & \\%
\nopagebreak & \multicolumn{7}{R@{}}{\scriptsize\textit{GIT analysis of ASEE Profiles 2010}} & \\%
\endlastfoot%
\nopagebreak  & Federal & 110,772,000 & 103,329,000 & 107,915,000 & 110,262,000 & 139,247,000 & 127,611,000 &  \\
\nopagebreak  & Foreign & 1,024,000 & 988,000 & 1,383,000 & 1,929,000 & 1,768,000 & 4,730,000 &  \\
& Individual & 5,869,000 & 6,415,000 & 7,167,000 & 8,581,000 & 9,545,000 & 7,886,000 &  \\
& Industry & 5,786,000 & 30,474,000 & 32,962,000 & 32,208,000 & 39,622,000 & 38,762,000 &  \\
& Local & 118,000 & 62,000 & 24,000 & 92,000 & 50,000 & 82,000 &  \\
& Nonprofit & 26,875,000 & 6,109,000 & 6,608,000 & 10,310,000 & 12,741,000 & 9,179,000 &  \\
\nopagebreak  & State & 51,750,000 & 56,300,000 & 63,345,000 & 61,954,000 & 54,310,000 & 51,757,000 &  \\
[\spacebeforetotals]%
\nopagebreak  & (all) & 202,194,000 & 203,677,000 & 219,404,000 & 225,336,000 & 257,283,000 & 240,007,000 & %
\label{tab:research1}
\end{longtable}
}
}
\end{document}


What am I missing? I believe that scoping is handled properly with the \global variable. My only guess is that the longtable environment isn't processing in a manner that works with the \newsavebox and \advance tricks.

• Welcome to TeX.sx! A tip: You can use backticks  to mark your inline code as I did in my edit. Oct 24 '11 at 12:48
• I'd like to replace my example with a different one, a narrower table that will more clearly highlight a working solution. Is there a recommended way to do this? Oct 27 '11 at 1:29

Well your example is way away "close to bare metal". Why did you add all the color command and the multitude of columns and rows? Beside this: longtable save the widths of the columns in the aux-file. So you can simply retrieve them from there (at the second compilation).

(The code below was modified - two more complex examples were added, one that demonstrates the desired output, and a second that implements the proposed solution, but doesn't work. )

\documentclass[11pt]{article}

\makeatletter
\newlength\longtablewidth
\setlength{\longtablewidth}{1in}
\newcommand\getlongtablewidth{%
\begingroup
\ifcsname LT@\roman{LT@tables}\endcsname
\global\longtablewidth=0pt
\@nameuse{LT@\roman{LT@tables}}%
\fi
\endgroup}

\makeatother
\begin{document}

\begin{longtable}{llll}
abccccc & aaaaaa & aaaaaa & aaaaaa \\
\getlongtablewidth
\makebox[0pt][l]{\rule{\dimexpr\longtablewidth-2\tabcolsep}{2pt}}
\end{longtable}

\begin{longtable}{llll}
a&b&c&d \\
\getlongtablewidth
\makebox[0pt][l]{\rule{\dimexpr\longtablewidth-2\tabcolsep}{2pt}}
\end{longtable}

% Added the two examples below
\begin{longtable}{llrrrrrl}
\multicolumn{8}{l}{Example 1 - this is what the table should look like} \\
\toprule%
& School & Cnt & Deg & M & F & Ratio &  \\
\midrule
\bottomrule%
\multicolumn{8}{l}{\begin{minipage}{4.5in}
NOTES: In this example, the width of this minipage in the endlastpage is
hard coded at exactly 4.5in.  The goal is to have this footer compute the width of the
table above it, then use this width in the definition of the minipage.
\end{minipage}} %
\endlastfoot
& Smith College & 8 & 24 & 0 & 1 & 3 &  \\
& Philadelphia University & 6 & 3 & 4 & 0.429 & 0.5 &  \\
& Tuskegee University & 34 & 28 & 39 & 0.418 & 0.82 &  \\
& Franklin W. Olin College of En & 22 & 26 & 38 & 0.406 & 1.2 &  \\
\end{longtable}

\begin{longtable}{llrrrrrl}
\multicolumn{8}{l}{Example 2 - this doesn't work yet} \\
\toprule%
& School & Cnt & Deg & M & F & Ratio &  \\
\midrule
\bottomrule %
\multicolumn{8}{l}{%
\getlongtablewidth %<---------------------------------- this makes it work
\begin{minipage}{\dimexpr\longtablewidth-2\tabcolsep}
NOTES: In this second example, the width of this minipage is variable, and should be calculated
based on the width of the table above.  The goal is to have this footer compute the width of the
table above it, then use this width in the definition of the minipage.
\end{minipage}} %
\endlastfoot
& Smith College & 8 & 24 & 0 & 1 & 3 &  \\
& Philadelphia University & 6 & 3 & 4 & 0.429 & 0.5 &  \\
& Tuskegee University & 34 & 28 & 39 & 0.418 & 0.82 &  \\
\getlongtablewidth   %<-------------------------------------- wrong place
& Franklin W. Olin College of En & 22 & 26 & 38 & 0.406 & 1.2 &  \\
\end{longtable}

\end{document}

• Thanks! For these tables I wanted to highlight them from the surrounding text with background shading. Further, I wanted to create a border around the \longtable. Because they were \longtables that might extend across multiple pages, I didn't think a \float or \fbox would work. My solution was to add dummy columns to the left and right of the data, and use \multicolumn rules that didn't extend the entire width of the new table. Finally, I wasn't sure where my problem was, so I thought it best to include a complete example (including the colors.) Thanks for your solution! Oct 24 '11 at 13:54
• OK - I tried this solution and it didn't work in the original code. I believe that it may be related to the length of the string I'm sticking inside the minipage. Oct 27 '11 at 1:24
• I wasn't sure how to do this, so I added 2 new \longtable examples directly to your code - one that demonstrates a "hard coded" solution, and a second that implement your proposed solution but doesn't yet work. Perhaps we can make this example work? Oct 27 '11 at 11:19
• Sorry I don't think that changing the code of an answer in this way is a good idea - people who search for answer could rely on it (regarding my reputation) and then get confused which part is mine. (correction of typos etc are ok). Better edit your question instead. Beside this: You are calling \getlongtablewidth at the wrong place. Put the command directly before the minipage which needs the width. Oct 27 '11 at 12:14
• Thanks - this got it to work. I did encounter trouble implementing this approach in my real application, but this will be left for another question. Oct 28 '11 at 1:08

use \begin{minipage}[t]{\dimexpr\linewidth+6\tabcolsep\relax} and also default L/R columns:

\begin{longtable}{LL *6RL}
\nopagebreak & \multicolumn{7}{@{}L}{\scriptsize } & \\%
\nopagebreak & \multicolumn{7}{@{}L}{\small {\tablename\ \thetable{}}} & \\%
\nopagebreak & \multicolumn{7}{@{}L}{\small {\textbf{Research expenditures by funds source over time}}} & \\%
\nopagebreak & \multicolumn{7}{@{}L}{\scriptsize{dollars}} & \\%
\nopagebreak\rulefiller\cmidrule[\heavyrulewidth]{2-8}
\nopagebreak & variable & FY05 & FY06 & FY07 & FY08 & FY09 & FY10 & \\%
\nopagebreak\rulefiller\cmidrule[\lightrulewidth]{2-8}
\nopagebreak & \multicolumn{7}{C}%
{\tablename\ \thetable{} -- continued from previous page} & \\%
\nopagebreak\rulefiller\cmidrule[\heavyrulewidth]{2-8}
\nopagebreak & variable & FY05 & FY06 & FY07 & FY08 & FY09 & FY10 & \\%
\nopagebreak\rulefiller\cmidrule[\lightrulewidth]{2-8}
\nopagebreak\rulefiller\cmidrule[\heavyrulewidth]{2-8}
\nopagebreak & \multicolumn{7}{R@{}}{{Continued on next page}} & \\%
\endfoot%
\nopagebreak\rulefiller\cmidrule[\heavyrulewidth]{2-8}%
\nopagebreak & \multicolumn{7}{@{}L}{%
\begin{minipage}[t]{\dimexpr\linewidth+6\tabcolsep\relax}\vspace{\spacebeforenotes}\scriptsize\noindent
NOTES: Research Expenditures represents the numbers of contracts and the amounts of expenditures for each department or research center for the given fiscal year.  Research Expenditures include cost sharing and matching funds; administrative, indirect, and other overhead expenses to the extent that they have been charged to a particular grant or other funding source; teaching, if instruction has formed a part of a separately funded research program or is significantly experimental in nature; and engineering-related research.
\end{minipage}
} & \\%
\nopagebreak & \multicolumn{7}{R@{}}{\scriptsize\textit{GIT analysis of ASEE Profiles 2010}} & \\%
\endlastfoot%
\nopagebreak  & Federal & 110,772,000 & 103,329,000 & 107,915,000 & 110,262,000 & 139,247,000 & 127,611,000 &  \\
\nopagebreak  & Foreign & 1,024,000 & 988,000 & 1,383,000 & 1,929,000 & 1,768,000 & 4,730,000 &  \\
& Individual & 5,869,000 & 6,415,000 & 7,167,000 & 8,581,000 & 9,545,000 & 7,886,000 &  \\
& Industry & 5,786,000 & 30,474,000 & 32,962,000 & 32,208,000 & 39,622,000 & 38,762,000 &  \\
& Local & 118,000 & 62,000 & 24,000 & 92,000 & 50,000 & 82,000 &  \\
& Nonprofit & 26,875,000 & 6,109,000 & 6,608,000 & 10,310,000 & 12,741,000 & 9,179,000 &  \\
\nopagebreak  & State & 51,750,000 & 56,300,000 & 63,345,000 & 61,954,000 & 54,310,000 & 51,757,000 &  \\
[\spacebeforetotals]%
\nopagebreak  & (all) & 202,194,000 & 203,677,000 & 219,404,000 & 225,336,000 & 257,283,000 & 240,007,000 & %
\label{tab:research1}
\end{longtable}


What are your endless \nopagebreak good for??

• Thanks! This is perfect! I use the \nopagebreak commands to ensure that the header and footer blocks don't get split when crossing a page boundaries, and to ensure that at least two rows of data are included across any page boundary. Without them, the tables sometime get split ugly. Oct 24 '11 at 13:37
• OK - I've worked with the solution proposed above by @Herbert. While it worked for my posted example because it was a full page wide, the use of the '\linewidth command doesn't work when the table is narrow. Oct 27 '11 at 1:01