动态修改\tablehead中的内容

动态修改\tablehead中的内容

我有一个非常简单的 LaTeX 文档:

\documentclass{article}
\usepackage{booktabs}
\usepackage{array}
\usepackage{fancyhdr}
\usepackage[us,12hr]{datetime}
\usepackage{lastpage}
\pagestyle{fancy}
\fancyhf{}
\setlength\headheight{0.5pt}
\chead{\textbf{\huge{Hello}}}
\lhead{\textbf{Left}}
\rhead{\textbf{Right}}
\rfoot{\today \hspace{0.5pt} \currenttime}
\cfoot{Page \thepage\ of \pageref{LastPage}}
\lfoot{Lower Left}

\renewcommand{\headrulewidth}{1pt}

\usepackage{supertabular}
\newcommand{\SpecialText}{Hello\ldots}
\newcommand{\testIt}{The value of \texttt{SpecialText} is : "\SpecialText''.}  
\begin{document}
\tablehead
{
\toprule
& \multicolumn{1}{p{24.5mm}}{\textbf{Gross Volume}}
& \multicolumn{1}{p{21mm}}{\textbf{Net Volume}}
& \multicolumn{1}{p{12mm}}{\textbf{Level}}
& \multicolumn{1}{p{21mm}}{\textbf{Temperature}}
& \multicolumn{1}{p{16mm}}{\centering{\textbf{Ullage}}}
& \multicolumn{1}{p{42mm}}{\centering{\textbf{Last Updated}}}\\

& \multicolumn{1}{p{24.5mm}}{\centering{\textbf{Gallons}}}
& \multicolumn{1}{p{21mm}}{\centering{\textbf{Gallons}}}
& \multicolumn{1}{p{12mm}}{\textbf{inches}}
& \multicolumn{1}{p{21mm}}{\centering{\textbf{Farenheit}}}
& \multicolumn{1}{p{16mm}}{\centering{\textbf{Gallons}}} & \\
\toprule
\multicolumn{6}{l}{\testIt}\\\\
}

\begin{supertabular}{p{35mm}*{6}{r}}
\renewcommand{\SpecialText}{New Text\textit{was}\ldots} 
\\ \multicolumn{7}{l}{\textbf {Set 1}}\\\\
\textbf{Subset 1} & \multicolumn{6}{l}{Red}\\
Element 1& \multicolumn{6}{l}{(Disjoint)}\\
\multicolumn{1}{r}{\textbf{Results}} & 3797.08 & 3780.49 & 38.85 & 66.21 & 5721.31 & 4/8/2012 9:00:21AM \\
\multicolumn{1}{r}{\textbf{State}} & 7.24 & & 0.54 & & & \\\\ 
\toprule[2pt]\\
\end{supertabular}
\end{document}

我正在尝试动态更改 上的内容\tablehead,以便每页开头的文本都不同。我试过了\renewcommand,但没有成功。

任何帮助都将非常有帮助。


在以下示例中,文章已扩展至第二页。标题保持不变,但更新命令被忽略,因为在第二页上,右上角的文本应该从以下内容更改:

SpecialText 的值为:“原始文本”。

到:

SpecialText 的值是:“新文本”。

欢迎任何评论。

\documentclass{report}
\usepackage[letterpaper,top=2.0cm,bottom=2.0cm,left=0.5cm,right=0.5cm]{geometry} 
\usepackage{booktabs} 
\usepackage{array} 
\usepackage{fancyhdr} 
\usepackage[us,12hr]{datetime} 
\usepackage{lastpage} 
\pagestyle{fancy} 
\fancyhf{} 
\setlength\headheight{0.5pt} 
\chead{\textbf{\huge{Hello}}} 
\lhead{\textbf{Left}} 
\rhead{\textbf{Right}} 
\rfoot{\today \hspace{0.5pt} \currenttime} 
\cfoot{Page \thepage\ of \pageref{LastPage}} 
\lfoot{Lower Left}  
\renewcommand{\headrulewidth}{1pt}  
\usepackage{supertabular} 
\newcommand{\SpecialText}{Original Text} 
\newcommand{\testIt}{The value of \texttt{SpecialText} is : "\SpecialText''.}   \begin{document} 
\tablehead 
{ 
\toprule & 
\multicolumn{1}{p{17mm}}{\textbf{Column 1}} & 
\multicolumn{1}{p{17mm}}{\textbf{Column 2}} & 
\multicolumn{1}{p{17mm}}{\textbf{Column 3}} & 
\multicolumn{1}{p{17mm}}{\textbf{Column 4}} & 
\multicolumn{1}{p{17mm}}{\centering{\textbf{Column 5}}} & 
\multicolumn{1}{p{17mm}}{\centering{\textbf{Column 6}}}\\  
& \multicolumn{1}{p{24.5mm}}{\centering{\textbf{Column 1}}} & 
\multicolumn{1}{p{21mm}}{\centering{\textbf{Column 2}}} & 
\multicolumn{1}{p{12mm}}{\textbf{Column 3}} & 
\multicolumn{1}{p{21mm}}{\centering{\textbf{SubColumn 4}}} & 
\multicolumn{1}{p{16mm}}{\centering{\textbf{SubColumn 5}}} & \\ 
\toprule 
\multicolumn{6}{l}{\testIt}\\\\ 
}  
\begin{supertabular}{p{35mm}*{6}{r}} 
\renewcommand{\SpecialText}{New Text}
\\ 
\multicolumn{7}{l}{\textbf {Set 1}}\\\\ 
\textbf{Subset 1} & \multicolumn{6}{l}{Red}\\ 
Element 1 & \multicolumn{6}{l}{(Disjoint)}\\ 
\multicolumn{1}{r}{\textbf{Results}} & 3797.08 & 3780.49 & 38.85 & 66.21 & 5721.31 & 4/8/2012 9:00:21AM \\ 
\multicolumn{1}{r}{\textbf{State}} & 7.24 & & 0.54 & & & \\\\  
\\ 
\multicolumn{7}{l}{\textbf {Set 1}}\\\\ 
\textbf{Subset 1} & \multicolumn{6}{l}{Red}\\ 
Element 1 & \multicolumn{6}{l}{(Disjoint)}\\ 
\multicolumn{1}{r}{\textbf{Results}} & 3797.08 & 3780.49 & 38.85 & 66.21 & 5721.31 & 4/8/2012 9:00:21AM \\ 
\multicolumn{1}{r}{\textbf{State}} & 7.24 & & 0.54 & & & \\\\  
\\ 
\multicolumn{7}{l}{\textbf {Set 1}}\\\\ 
\textbf{Subset 1} & \multicolumn{6}{l}{Red}\\ 
Element 1 & \multicolumn{6}{l}{(Disjoint)}\\ 
\multicolumn{1}{r}{\textbf{Results}} & 3797.08 & 3780.49 & 38.85 & 66.21 & 5721.31 & 4/8/2012 9:00:21AM \\ 
\multicolumn{1}{r}{\textbf{State}} & 7.24 & & 0.54 & & & \\\\
\\ 
\multicolumn{7}{l}{\textbf {Set 1}}\\\\ 
\textbf{Subset 1} & \multicolumn{6}{l}{Red}\\ 
Element 1 & \multicolumn{6}{l}{(Disjoint)}\\ 
\multicolumn{1}{r}{\textbf{Results}} & 3797.08 & 3780.49 & 38.85 & 66.21 & 5721.31 & 4/8/2012 9:00:21AM \\ 
\multicolumn{1}{r}{\textbf{State}} & 7.24 & & 0.54 & & & \\\\  
\\ 
\multicolumn{7}{l}{\textbf {Set 1}}\\\\ 
\textbf{Subset 1} & \multicolumn{6}{l}{Red}\\ 
Element 1 & \multicolumn{6}{l}{(Disjoint)}\\ 
\multicolumn{1}{r}{\textbf{Results}} & 3797.08 & 3780.49 & 38.85 & 66.21 & 5721.31 & 4/8/2012 9:00:21AM \\ 
\multicolumn{1}{r}{\textbf{State}} & 7.24 & & 0.54 & & & \\\\  
\\ 
\multicolumn{7}{l}{\textbf {Set 1}}\\\\ 
\textbf{Subset 1} & \multicolumn{6}{l}{Red}\\ 
Element 1 & \multicolumn{6}{l}{(Disjoint)}\\ 
\multicolumn{1}{r}{\textbf{Results}} & 3797.08 & 3780.49 & 38.85 & 66.21 & 5721.31 & 4/8/2012 9:00:21AM \\ 
\multicolumn{1}{r}{\textbf{State}} & 7.24 & & 0.54 & & & \\\\  
\\ 
\renewcommand{\SpecialText}{New Text}\\
\multicolumn{7}{l}{\textbf {Set 1}}\\\\ 
\textbf{Subset 1} & \multicolumn{6}{l}{Red}\\ 
Element 1 & \multicolumn{6}{l}{(Disjoint)}\\ 
\multicolumn{1}{r}{\textbf{Results}} & 3797.08 & 3780.49 & 38.85 & 66.21 & 5721.31 & 4/8/2012 9:00:21AM \\ 
\multicolumn{1}{r}{\textbf{State}} & 7.24 & & 0.54 & & & \\\\   
\toprule[2pt]\\ 
\end{supertabular} 
\end{document}  

答案1

表格单元格中存在隐式分组级别,因此在处理发生重新定义的单元格之后,重新定义就会丢失。

\gdef\SpecialText{New text}

进行全局(重新)定义\SpecialTextsupertabular在下一页的头部使用该值,因为头部和脚部存储在宏中。

顺便说一句:这与包装不同longtable。桌头和桌脚是静态的,存放在盒子里。

小计/单元格捕获示例

\documentclass{article}

\usepackage{array}
\usepackage{trimspaces}
\usepackage{supertabular}

\makeatletter
\@ifdefinable\CatchCell{%
  \long\def\CatchCell#1\ignorespaces#2\unskip{%
    \def\CatchCell@obj{#2}%
    \trim@spaces@in\CatchCell@obj
    \expandafter\@CatchCell\expandafter{\CatchCell@obj}{#1}%
  }%
}
\newcommand{\@CatchCell}[2]{%
  #2{#1}%
}
\@ifdefinable\CatchLastCell{%
  \long\def\CatchLastCell#1\ignorespaces#2\\{%
    \def\CatchCell@obj{#2}%
    \trim@spaces@in\CatchCell@obj
    \expandafter\@CatchCell\expandafter{\CatchCell@obj}{#1}\\%
  }%
}
\makeatother

\setlength{\textheight}{5\baselineskip}% for testing

\begin{document}
\newcommand*{\mysum}{0}
\newcommand*{\entry}[1]{%
  #1\xdef\mysum{\the\numexpr\mysum+#1\relax}%
}
\tablefirsthead{}
\tablehead{%
  Previous sum: & \multicolumn{1}{l}{\mysum}\\
  \hline
}
\tabletail{%
  \hline
  Sum:& \multicolumn{1}{l}{\mysum}\\
}
\begin{supertabular}{l>{\CatchLastCell\entry}l}
a & 1 \\
b & 2 \\
c & 3 \\
d & 4 \\
e & 5 \\
f & 6 \\
g & 7 \\
\end{supertabular}
\end{document}

结果

评论:

  • 除最后一列之外的单元格均可通过 捕获\CatchCell。它接受一个宏的参数,该宏可对单元格的内容执行某些操作。

  • 最后一列取决于用于完成表格行的标记。在此示例中,\\使用的是 ,并且\CatchLastCell必须使用 而不是\CatchCell

  • 需要注意表格的头部和尾部。在这种情况下,\entry将数字添加到总和的命令不能重复。因此\multicolumn需要覆盖列规范。

  • 可以使用包进行更严肃的计算(财务数据)fp

相关内容