I use a counter for my Questions. I use fancyhdr. I need to show the first and last values of the value of the counter on a page at its header.
In the following example, Page 2 header should read: Questions: 1-2
Page 3 header should read: Questions: 2-3
Page 4 header should read: Questions: 4-5
Page 5 header should read: Question: 6
I tried several suggestions given on the internet. None worked for me! In particular on page 3, when LaTeX decides to put question 4 on the next page, the counter is already incremented and the header then reads Questions 2-4, whereas Question 4 is now totally on the next page. And of course I could never get Question: 6 alone on the header.
Here is my LaTeX file:
%%%%%%%%%%%%%%%%%%%%%%%
\documentclass[12pt]{amsbook}
\usepackage{fancyhdr}
\usepackage{lipsum}
\newcounter{beans}
\setcounter{beans}{1}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[RE]{Dante}
\fancyhead[LO]{Meaning of Life}
\fancyfoot[LE,RO]{\thepage}
\textheight 7cm
\begin{document}
\chapter{Gate}
{\bf \thebeans. Question: }
\lipsum[\thebeans]
\stepcounter{beans}
{\bf \thebeans. Question: }
\lipsum[\thebeans]
\stepcounter{beans}
{\bf \thebeans. Question: }
\lipsum[\thebeans]
\stepcounter{beans}
{\bf \thebeans. Question: }
\lipsum[\thebeans]
\stepcounter{beans}
{\bf \thebeans. Question: }
Fusce mauris. Vestibulum luctus nibh at lectus.
Sed bibendum, nulla a faucibus semper, leo velit ultricies tellus, ac
venenatis arcu wisi vel nisl. Vestibulum diam. Aliquam pellentesque,
augue quis sagittis posuere, turpis lacus congue quam, in hendrerit
risus eros eget felis. Maecenas eget erat in sapien mattis porttitor.
Vestibulum porttitor. Nulla facilisi. Sed a turpis eu lacus commodo
\stepcounter{beans}
{\bf \thebeans. Question: }
\lipsum[\thebeans]
Fusce mauris. Vestibulum luctus nibh at lectus.
Sed bibendum, nulla a faucibus semper, leo velit ultricies tellus, ac
venenatis arcu wisi vel nisl. Vestibulum diam. Aliquam pellentesque,
augue quis sagittis posuere, turpis lacus congue quam, in hendrerit
risus eros eget felis. Maecenas eget erat in sapien mattis porttitor.
Vestibulum porttitor. Nulla facilisi. Sed a turpis eu lacus commodo
\stepcounter{beans}
\end{document}
%%%%%%%%%%%%%%%%%%%%%%%%
Thank you.
\mark. then, in the header, the\firstmarkand\lastmarkare called (these marks are coordinated by the output routine, which "knows" what is on each page). the best way to do this is probably to create an environment that steps the counter at the beginning, not at the end, and immediately marks it. i will try to work on this later, unless someone else gets there first. – barbara beeton Apr 10 '18 at 17:09