2

The following tex file is trying to set a text (in this case a number, but it could be a string as well) in the footer of one page and the same value in the header on the following page. The idea is to set the value needed in the footer with each data item, then after each page update the header value for the following page.

The footer works ok, the header does not. The value in the header on the first page is ok, the value on the following pages are not.

\documentclass[10pt,a4paper]{article}

\usepackage[utf8]{inputenc}
\usepackage{fancyhdr}
\usepackage{afterpage}
\author{Bent Nielsen}
\title{page change testing}

\gdef\Total{0.00}
\gdef\TopTotal{0.00}

\afterpage{\gdef\TopTotal{\Total}}

\pagestyle{fancy}
\fancyhf{}
\rhead{Top Total \TopTotal }
\rfoot{Current total \Total}

\begin{document}
Page 1
\gdef\Total{5.00}
\gdef\Total{10.00}
\gdef\Total{15.00}

\newpage

Page 2
\gdef\Total{20.00}
\gdef\Total{25.00}
\gdef\Total{30.00}

\newpage

Page 3
\gdef\Total{35.00}
\gdef\Total{40.00}
\gdef\Total{45.00}
\end{document}

1 Answer 1

1

Here is a solution

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{fancyhdr}

\usepackage{afterpage}

\author{Bent Nielsen}
\title{page change testing}

\gdef\Total{0.00}
\gdef\TopTotal{0.00}

\pagestyle{fancy}
\fancyhf{}
\rhead{Top Total \TopTotal}
\rfoot{Current total \Total}

\newcommand{\mtTotal}[1]{%
\gdef\Total{#1}%
\afterpage{%
\gdef\TopTotal{#1}}}

\begin{document}
Page 1
\mtTotal{5.00}
\mtTotal{10.00}
\mtTotal{15.00}

\newpage

Page 2
\mtTotal{20.00}
\mtTotal{25.00}
\mtTotal{30.00}

\newpage

Page 3
\mtTotal{35.00}
\mtTotal{40.00}
\mtTotal{45.00}
\end{document}
4
  • Thanks. That works nicely. (I will wait a little for possible other answers before accepting).
    – Bent
    Apr 24, 2015 at 19:09
  • @Bent but you can upvote anyway
    – touhami
    Apr 24, 2015 at 19:23
  • I will when I can. Still too little rep I'm afraid. But definitely worth a +1.
    – Bent
    Apr 24, 2015 at 19:48
  • @Bent welcome to Tex.sx
    – touhami
    Apr 24, 2015 at 21:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .