1

I am making guided notes for students. In the header for the notes on each section, I want to display "page 1 of 3", "page 2 of 3", etc. I can get the final page number by placing a label at the end of the section and then using pageref with that label in the header. This generates exactly what I want. Of course, then I add a second section and that's where things go wrong. I want to use a command to set up the beginning and end of each section (clear pages, set the numbering, etc). Since the label named pageCnt is always named pageCnt, every section shows the total page count of the final section.

I've tried a variety of ways to concatenate on the sectionmark to the label but haven't gotten that to work. This may be because it is a number. Does anyone have any ideas?

Minimal reproducible example below:

\documentclass[12pt]{article}

\usepackage{lipsum}  

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\rhead{page \thepage \ of \pageref{pageCnt}}
\usepackage{emptypage}

\newcommand{\opensection}[1]{
    \pagenumbering{arabic}
    \section{#1}
}
\newcommand{\closesection}{
    \label{pageCnt}
    \cleardoublepage
}

\begin{document}

\opensection{First Section}
\lipsum[1-2]
\closesection

\opensection{Second Section}
\lipsum[3-12]
\closesection

\end{document}

1 Answer 1

0

Indeed, you can't use the same label multiple times, but you can index your labels with a counter for the sections. Like so:

\documentclass[12pt]{article}

\usepackage{lipsum}

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\rhead{page \thepage \ of \pageref{pageCnt.\arabic{sectionCnt}}}
\usepackage{emptypage}

\newcommand{\opensection}[1]{%
  \stepcounter{sectionCnt}%
  \pagenumbering{arabic}%
  \section{#1}%
}
\newcounter{sectionCnt}
\newcommand{\closesection}{%
  \label{pageCnt.\arabic{sectionCnt}}%
  \cleardoublepage%
}

\begin{document}

\opensection{First Section}
\lipsum[1-2]
\closesection

\opensection{Second Section}
\lipsum[3-12]
\closesection

\end{document}

Btw, well done first question. Welcome!

1
  • That looks so much simpler than literally everything I tried. Thank you!!! Feb 26, 2023 at 1:49

You must log in to answer this question.

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