如何动态测量长表的宽度并在页脚中使用该宽度?

如何动态测量长表的宽度并在页脚中使用该宽度?

我在计算 a 的宽度longtable并在表格中使用它时遇到了麻烦。这个网站提供了几个建议,我已经实施了其中一个。但是,由于范围或由于 的一些细微差别longtable,这个\global\advance\variable技巧无法正常工作。

以下是我当前的输出。注释应该延伸到最后一列。

示例表

这是生成上图的代码。我意识到使用newenvironment和有几种技巧可以变得更加复杂newcolumntype,但出于调试的目的,我接近裸机。此侧的先前问题和答案帮助清理了表格,命令\rulefiller就是这样做的结果。

\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{\tableshadingcolor}{pantone120}  % set this to change background shading of tables
\newcommand{\tablerulecolor}{black}

\newcolumntype{L}{>{\columncolor{\tableshadingcolor}}l}
\newcolumntype{C}{>{\columncolor{\tableshadingcolor}}c}
\newcolumntype{R}{>{\columncolor{\tableshadingcolor}}r}

\newcommand*{\rulefiller}{%
  \arrayrulecolor{\tableshadingcolor}% change to cell colour
  \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
>{\begin{lrbox}{\mycolwidthbox}\setlength{\global\noteswidth}{-\tabcolsep}}L<{\end{lrbox}\global\advance\noteswidth by \wd\mycolwidthbox\global\advance\noteswidth by \tabcolsep\usebox{\mycolwidthbox}}
>{\begin{lrbox}{\mycolwidthbox}}R<{\end{lrbox}\global\advance\noteswidth by \wd\mycolwidthbox\global\advance\noteswidth by \tabcolsep\usebox{\mycolwidthbox}}
>{\begin{lrbox}{\mycolwidthbox}}R<{\end{lrbox}\global\advance\noteswidth by \wd\mycolwidthbox\global\advance\noteswidth by \tabcolsep\usebox{\mycolwidthbox}}
>{\begin{lrbox}{\mycolwidthbox}}R<{\end{lrbox}\global\advance\noteswidth by \wd\mycolwidthbox\global\advance\noteswidth by \tabcolsep\usebox{\mycolwidthbox}}
>{\begin{lrbox}{\mycolwidthbox}}R<{\end{lrbox}\global\advance\noteswidth by \wd\mycolwidthbox\global\advance\noteswidth by \tabcolsep\usebox{\mycolwidthbox}}
>{\begin{lrbox}{\mycolwidthbox}}R<{\end{lrbox}\global\advance\noteswidth by \wd\mycolwidthbox\global\advance\noteswidth by \tabcolsep\usebox{\mycolwidthbox}}
>{\begin{lrbox}{\mycolwidthbox}}R<{\end{lrbox}\global\advance\noteswidth by \wd\mycolwidthbox\global\advance\noteswidth by \tabcolsep\usebox{\mycolwidthbox}}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} 
\endfirsthead
 \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} 
\endhead
 \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}

我遗漏了什么?我相信变量的作用域处理得当\global。我唯一的猜测是环境没有以与和技巧longtable兼容的方式进行处理。\newsavebox\advance

答案1

嗯,您的示例已经“接近裸机”了。您为什么要添加所有颜色命令以及大量的列和行?除此之外:longtable 将列的宽度保存在辅助文件中。因此,您可以简单地从那里检索它们(在第二次编译时)。

(下面的代码已被修改 - 添加了两个更复杂的例子,一个演示了所需的输出,另一个实现了提出的解决方案,但不起作用。)

\documentclass[11pt]{article}

\usepackage{longtable,booktabs}  % added booktabs

\makeatletter
\newlength\longtablewidth
\setlength{\longtablewidth}{1in}
\newcommand\getlongtablewidth{%
 \begingroup
  \ifcsname LT@\roman{LT@tables}\endcsname
  \global\longtablewidth=0pt
  \renewcommand\LT@entry[2]{\global\advance\longtablewidth by ##2}%
  \@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
\endfirsthead
\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
\endfirsthead
\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}

答案2

使用\begin{minipage}[t]{\dimexpr\linewidth+6\tabcolsep\relax}并默认 L/R 列:

\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} 
\endfirsthead
 \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} 
\endhead
 \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}

在此处输入图片描述

你无穷无尽的好处都用来做什么\nopagebreak

相关内容