Pagestyle – Changing Page Number in Pagestyle{myheadings} for Custom Headers

pagestyle

I am working with a .sty file, that defines a myheadings style like this:

\pagestyle{myheadings}
            \markboth{}{header part 1~header part 2~\textendash~header part 3}

The pagestyle inserts a page number right of the header. I now would like to change the number from a plain number X to X of N. The other answers how to change the page numbers seem to define them in \cfoot, what doesn't match the myheadings style but only works with page numbers inside the footer.

I guess there is a simple command that that needs to be renewed such that the pagestyle automatically uses the correct format, but I cannot find which one.

Best Answer

Without additional packages (except lastpage to capture the last page number): b

c

\documentclass[12pt, openany]{book} 

\usepackage{lastpage}% for last page

\pagestyle{myheadings}
\markboth{}{header part 1~header part 2~\textendash~header part 3}

\makeatletter
\def \@evenhead {\thepage\ of \pageref{LastPage} \hfil \slshape \leftmark } % added <<<<
\def \@oddhead {{\slshape \rightmark }\hfil \thepage\ of \pageref{LastPage}} % added <<<<
\makeatother

\usepackage{kantlipsum}% only for dummy text
\begin{document}
    
    \kant[1-15]
    
 \end{document}

The popular package fancyhdr offers more versatility to control the headers and footers.

\documentclass[12pt, openany]{book} 

\usepackage{lastpage}% for last page

\usepackage{fancyhdr}% added <<<<<<<<
\fancyhf{} % clear header and footer
\renewcommand{\headrulewidth}{0pt}% suppress line after header
\fancyhead[LO]{\slshape header part 1~header part 2~\textendash~header part 3}% Left Odd pages
\fancyhead[LE,RO]{\thepage\ of \pageref{LastPage}} % Left Even and Right Odd pages 

\pagestyle{fancy} % choose the style

\usepackage{kantlipsum}% only for dummy text
\begin{document}
    
    \kant[1-15]
    
\end{document}
Related Question