Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have a pretty simple LaTeX document:

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

I am trying to dynamically change the content on the \tablehead, so that at the beginning of each page the text is different. I tried \renewcommand but it did not work.

Any help will be great.


In the following example, the article has been expanded to the second page. The heading remains the same, however the renew command is ignored, since on the second page, the upper right text should be changed from:

The Value of SpecialText is: "Original Text".

to:

The Value of SpecialText is: "New Text".

Any comments are welcome.

\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}  
share|improve this question
add comment

1 Answer

There is an implicit grouping level in table cells, thus the redefinition gets lost after the cell is processed, where the redefinition occurs.

\gdef\SpecialText{New text}

makes a global (re)definition of \SpecialText and the supertabular uses this value in the head of the next page, because the head and foot are stored in macros.

BTW: This is a difference to package longtable. Table heads and foots are static and stored in boxes there.

Example for sub-totals/cell catching

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

Result

Remarks:

  • Cells except in the last column can be catched by \CatchCell. It takes an argument for a macro that can do something with the cell's content.

  • The last column depends on the token that is used to finish the table row. In this example \\ is used and \CatchLastCell must be used instead of \CatchCell.

  • Care is needed for the head and tail part of the table. In this case, the command \entry, that adds the number to the sum, must not be repeated. Therefore \multicolumn is needed to overwrite the column specification.

  • More serious calculations (financial data) can be done with package fp.

share|improve this answer
    
So you know that. Hm. Did you ever think about a package to calculate and print a sub-total of a certain column in the last row at the bottom of the page of a multipage tabular? –  Keks Dose Oct 23 '12 at 15:51
    
@KeksDose Example added. –  Heiko Oberdiek Oct 23 '12 at 16:22
    
Incredible. But I tend to be impertinent: Could you add a column definition: »all entries in this columns are numbers«, to get rid of the \entry command inside the tabular? Maybe something like >{\Entry}l! I'll pay your beer and meal at the next "Stammtisch" in KA, anyway . . . –  Keks Dose Oct 23 '12 at 17:25
    
Thank you for your comment Heiko. I ran your latex with more values and could not see the effect of the \CatchCell Would you please provide more description? –  Chayo Oct 23 '12 at 21:13
    
@KeksDose Problem solved (forgotten \multicolumn in table head and tail). Thus the example now combines sub-totals and cell catching. I hope, your offer for the next "Stammtisch" stands. –  Heiko Oberdiek Oct 24 '12 at 19:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.