[Tex/LaTex] classification labels in headers and footers

header-footerpage-breaking

I'm trying to write Latex documents that will automatically satisfy government requirements for labeling certain classes of information:
– Each paragraph starts with a label indicating its importance.
– The header and footer of each page include a label indicating the
most important paragraph on that page (even if it started on the
previous page).

This is the closest I have come:

\documentclass[12pt]{report}
\usepackage{fancyhdr}
\usepackage{ifthen}
\usepackage{afterpage}
\usepackage{color}
\usepackage{lipsum}

\setlength\parskip{.6\baselineskip}
\setlength\parindent{0pt}
\setlength\headheight{15pt}
\setlength\textheight{21\baselineskip}
\newcommand\pageimport{TRIVIAL}
\newcommand\paraimport{TRIVIAL}

\pagestyle{fancy}
\lhead{}\chead{\leftmark}\rhead{}
\lfoot{}\cfoot{\leftmark}\rfoot{\thepage}

\newcommand{\IMP}[1]{\renewcommand\paraimport{IMPORTANT}
  \renewcommand\pageimport{IMPORTANT}
  \markboth{\pageimport}{\paraimport}
  \afterpage{\renewcommand\pageimport{\protect\paraimport}\markboth{\protect\paraimport}{}}
\textcolor{red}{(I)  #1}\renewcommand\paraimport{TRIVIAL}}

\newcommand{\TRI}[1]{\renewcommand\paraimport{TRIVIAL}
  \markboth{\pageimport}{\paraimport}
  \afterpage{\renewcommand\pageimport{\protect\paraimport}\markboth{\protect\paraimport}{}}
(U)  #1\renewcommand\paraimport{TRIVIAL}}

\begin{document}
\immediate\typeout{a. pageimport = \pageimport\space  paraimport = \paraimport}
\TRI{\lipsum[1]}
\TRI{\lipsum[2]}
\TRI{\lipsum[3]}
\immediate\typeout{b. pageimport = \pageimport\space  paraimport = \paraimport}
\IMP{\lipsum[4]}
\immediate\typeout{c. pageimport = \pageimport\space  paraimport = \paraimport}
\TRI{\lipsum[5]}
\TRI{\lipsum[6]}
\TRI{\lipsum[7]}
\TRI{\lipsum[8]}
\immediate\typeout{d. pageimport = \pageimport\space  paraimport = \paraimport}
\IMP{\lipsum[9]}
\immediate\typeout{e. pageimport = \pageimport\space  paraimport = \paraimport}
\TRI{\lipsum[10]}
\TRI{\lipsum[11]}
\TRI{\lipsum[12]}
\end{document}

(I've changed the labels here to avoid causing needless alarm. For
convenience, I have also set the "important" text in red.) My idea is
to keep the "importance" of each paragraph in \paraimport. That gets
reset at the end of each paragraph. The header and footer get their
labels from \pageimport, which records the highest importance on the
current page. At the end of each page, \pageimport gets reset to the
value of \paraimport.

This document has one "important" paragraph on page 2, and another
that starts on page 4 and ends on page 5. All the paragraphs are
correctly labeled. The first two pages have the right labels, but
pages 3 and 6 should be marked "trivial". The tracing statements
before and after each "important" paragraph show that \pageimport is
not getting reset at the pagebreaks.

I suspect I'm moving something fragile, but rewriting the \afterpage
commands as

\afterpage{\renewcommand\pageimport{\protect\paraimport}\markboth{\protect\paraimport}{}}

makes no difference.

I've seen secret.sty, but it's attacking a different problem. I have
not been able to adapt it for page marking.

I'd appreciate any suggestions.

Best Answer

Here is a solution.

The key point is to reset left and right marks

\afterpage{%
\renewcommand\paraimport{TRIVIAL}%
\markboth{\paraimport}{\paraimport}

Note We need only one command \paraimport

Update the idea is to redefine \parimport and \markboth inside paragraphs i.e. just after (I).

Updated MWE

\documentclass[12pt]{report}
\usepackage{fancyhdr}
\usepackage{afterpage}
\usepackage{color}
\usepackage{lipsum}

\setlength\parskip{.6\baselineskip}
\setlength\parindent{0pt}
\setlength\headheight{15pt}
\setlength\textheight{21\baselineskip}


\pagestyle{fancy}
\lhead{}\chead{\leftmark}\rhead{}
\lfoot{}\cfoot{\leftmark}\rfoot{\thepage}

\newcommand\paraimport{TRIVIAL}
\markboth{\paraimport}{\paraimport}

\newcommand{\IMP}[1]{%
\textcolor{red}{(I)
\renewcommand\paraimport{IMPORTANT}%
\markboth{\paraimport}{\paraimport}%
 #1}%
\afterpage{%
\renewcommand\paraimport{TRIVIAL}%
\markboth{\paraimport}{\paraimport}}%
}

\newcommand{\TRI}[1]{(U)  #1}

\begin{document}
\TRI{\lipsum[1]}
\TRI{\lipsum[2]}
\TRI{\lipsum[3]}
\IMP{\lipsum[4]}
\TRI{\lipsum[5]}
\TRI{\lipsum[6]}
\TRI{\lipsum[7]}
\TRI{\lipsum[8]}
\IMP{\lipsum[9]}
\TRI{\lipsum[10]}
\TRI{\lipsum[11]}
\TRI{\lipsum[12]}
\end{document}
Related Question