[texhax] First and last value of custom counter in header

Heiko Oberdiek heiko.oberdiek at googlemail.com
Mon May 10 13:03:15 CEST 2010


On Mon, May 10, 2010 at 07:18:43AM +0000, Aleksandar Zec wrote:

> 2010/5/9 Heiko Oberdiek <heiko.oberdiek at googlemail.com>:
> > Classical case for a marks register. Example that uses an additional
> > marks register provided by e-TeX:
> > [tex code]
> 
> Heiko, thank you very much, I didn't know for this ellegant e-TeX
> possibility regarding marks. However, this does not always work in
> multicols environment (it works without multicols). Example:
> [...]

> Headers should be "1--6", "7--12" and "13--14", but they appear as
> "1--7", "7--12" and "14--14".

\leavevmode before \marks\dictmark improves the situation, but not
always.

Therefore you need a solution based on references, eg.:

\documentclass[a4paper]{article}
\usepackage{zref-abspage}
\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage{multicol}
\usepackage{fancyhdr}
\fancyhead{}
\fancyhead[C]{\getdict\firstdict--\lastdict}
\fancyfoot{}
\fancyfoot[C]{page \thepage}
\pagestyle{fancy}

\makeatletter
\newcounter{cnt}
\newcommand*{\dictunknown}{??}
\zref at newprop{cnt}[\dictunknown]{\thecnt}
\newcommand*{\TheCnt}{%
  \stepcounter{cnt}%
  \thecnt. %
  \zref at labelbyprops{cnt\the\value{cnt}}{abspage,cnt}%
}
\newcommand*{\getdict}{%
  \begingroup
    \count@=1 %
    \edef\x{\zref at extractdefault{cnt\the\count@}{abspage}{0}}%
    \global\let\firstdict\dictunknown
    \global\let\lastdict\dictunknown
    \@whilenum\x>0 %
    \do{%
      \ifnum\x=\numexpr\value{abspage}+1\relax
        \protected at xdef\lastdict{%
          \zref at extract{cnt\the\count@}{cnt}%
        }%
        \ifx\firstdict\dictunknown
          \protected at xdef\firstdict{%
            \zref at extract{cnt\the\count@}{cnt}%
          }%
        \fi
      \fi
      \advance\count@ by 1 %
      \edef\x{\zref at extractdefault{cnt\the\count@}{abspage}{0}}%
    }%
  \endgroup
}
\makeatother

\begin{document}
\begin{multicols}{2}
\TheCnt \blindtext\par
\TheCnt \blindtext\par
\TheCnt \blindtext\par
\TheCnt \blindtext\par
\TheCnt \blindtext\par
\TheCnt \blindtext\par
\TheCnt \blindtext\par
\TheCnt \blindtext\par
\TheCnt \blindtext\par
\TheCnt \blindtext\par
\TheCnt \blindtext\par
\TheCnt \blindtext\par
\TheCnt \blindtext\par
\TheCnt \blindtext
\end{multicols}
\end{document}

Yours sincerely
  Heiko Oberdiek


More information about the texhax mailing list